-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathRuntimeMetrics.cs
216 lines (182 loc) · 9.82 KB
/
RuntimeMetrics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics.Metrics;
using System.Reflection;
using OpenTelemetry.Internal;
#if NET
using JitInfo = System.Runtime.JitInfo;
#endif
namespace OpenTelemetry.Instrumentation.Runtime;
/// <summary>
/// .NET runtime instrumentation.
/// </summary>
internal sealed class RuntimeMetrics
{
internal static readonly Assembly Assembly = typeof(RuntimeMetrics).Assembly;
internal static readonly AssemblyName AssemblyName = Assembly.GetName();
internal static readonly Meter MeterInstance = new(AssemblyName.Name!, Assembly.GetPackageVersion());
#if NET
private const long NanosecondsPerTick = 100;
#endif
private const int NumberOfGenerations = 3;
private static readonly string[] GenNames = ["gen0", "gen1", "gen2", "loh", "poh"];
#if NET
private static bool isGcInfoAvailable;
#endif
static RuntimeMetrics()
{
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.gc.collections.count",
GetGarbageCollectionCounts,
description: "Number of garbage collections that have occurred since process start.");
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.objects.size",
() => GC.GetTotalMemory(false),
unit: "bytes",
description: "Count of bytes currently in use by objects in the GC heap that haven't been collected yet. Fragmentation and other GC committed memory pools are excluded.");
#if NET
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.gc.allocations.size",
() => GC.GetTotalAllocatedBytes(),
unit: "bytes",
description: "Count of bytes allocated on the managed GC heap since the process start. .NET objects are allocated from this heap. Object allocations from unmanaged languages such as C/C++ do not use this heap.");
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.committed_memory.size",
() =>
{
return !IsGcInfoAvailable ? Array.Empty<Measurement<long>>() : [new(GC.GetGCMemoryInfo().TotalCommittedBytes)];
},
unit: "bytes",
description: "The amount of committed virtual memory for the managed GC heap, as observed during the latest garbage collection. Committed virtual memory may be larger than the heap size because it includes both memory for storing existing objects (the heap size) and some extra memory that is ready to handle newly allocated objects in the future. The value will be unavailable until at least one garbage collection has occurred.");
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.heap.size",
() =>
{
if (!IsGcInfoAvailable)
{
return [];
}
var generationInfo = GC.GetGCMemoryInfo().GenerationInfo;
var measurements = new Measurement<long>[generationInfo.Length];
var maxSupportedLength = Math.Min(generationInfo.Length, GenNames.Length);
for (var i = 0; i < maxSupportedLength; ++i)
{
measurements[i] = new(generationInfo[i].SizeAfterBytes, new KeyValuePair<string, object?>("generation", GenNames[i]));
}
return measurements;
},
unit: "bytes",
description: "The heap size (including fragmentation), as observed during the latest garbage collection. The value will be unavailable until at least one garbage collection has occurred.");
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.heap.fragmentation.size",
() =>
{
if (!IsGcInfoAvailable)
{
return [];
}
var generationInfo = GC.GetGCMemoryInfo().GenerationInfo;
var measurements = new Measurement<long>[generationInfo.Length];
var maxSupportedLength = Math.Min(generationInfo.Length, GenNames.Length);
for (var i = 0; i < maxSupportedLength; ++i)
{
measurements[i] = new(generationInfo[i].FragmentationAfterBytes, new KeyValuePair<string, object?>("generation", GenNames[i]));
}
return measurements;
},
unit: "bytes",
description: "The heap fragmentation, as observed during the latest garbage collection. The value will be unavailable until at least one garbage collection has occurred.");
var mi = typeof(GC).GetMethod("GetTotalPauseDuration", BindingFlags.Public | BindingFlags.Static);
var getTotalPauseDuration = mi?.CreateDelegate<Func<TimeSpan>>();
if (getTotalPauseDuration != null)
{
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.gc.duration",
() => getTotalPauseDuration().Ticks * NanosecondsPerTick,
unit: "ns",
description: "The total amount of time paused in GC since the process start.");
}
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.jit.il_compiled.size",
() => JitInfo.GetCompiledILBytes(),
unit: "bytes",
description: "Count of bytes of intermediate language that have been compiled since the process start.");
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.jit.methods_compiled.count",
() => JitInfo.GetCompiledMethodCount(),
description: "The number of times the JIT compiler compiled a method since the process start. The JIT compiler may be invoked multiple times for the same method to compile with different generic parameters, or because tiered compilation requested different optimization settings.");
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.jit.compilation_time",
() => JitInfo.GetCompilationTime().Ticks * NanosecondsPerTick,
unit: "ns",
description: "The amount of time the JIT compiler has spent compiling methods since the process start.");
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.monitor.lock_contention.count",
() => Monitor.LockContentionCount,
description: "The number of times there was contention when trying to acquire a monitor lock since the process start. Monitor locks are commonly acquired by using the lock keyword in C#, or by calling Monitor.Enter() and Monitor.TryEnter().");
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.thread_pool.threads.count",
() => (long)ThreadPool.ThreadCount,
description: "The number of thread pool threads that currently exist.");
MeterInstance.CreateObservableCounter(
"process.runtime.dotnet.thread_pool.completed_items.count",
() => ThreadPool.CompletedWorkItemCount,
description: "The number of work items that have been processed by the thread pool since the process start.");
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.thread_pool.queue.length",
() => ThreadPool.PendingWorkItemCount,
description: "The number of work items that are currently queued to be processed by the thread pool.");
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.timer.count",
() => Timer.ActiveCount,
description: "The number of timer instances that are currently active. Timers can be created by many sources such as System.Threading.Timer, Task.Delay, or the timeout in a CancellationSource. An active timer is registered to tick at some point in the future and has not yet been canceled.");
#endif
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.assemblies.count",
() => (long)AppDomain.CurrentDomain.GetAssemblies().Length,
description: "The number of .NET assemblies that are currently loaded.");
var exceptionCounter = MeterInstance.CreateCounter<long>(
"process.runtime.dotnet.exceptions.count",
description: "Count of exceptions that have been thrown in managed code, since the observation started. The value will be unavailable until an exception has been thrown after OpenTelemetry.Instrumentation.Runtime initialization.");
AppDomain.CurrentDomain.FirstChanceException += (source, e) =>
{
exceptionCounter.Add(1);
};
}
#pragma warning disable SA1313
/// <summary>
/// Initializes a new instance of the <see cref="RuntimeMetrics"/> class.
/// </summary>
/// <param name="_1">The options to define the metrics.</param>
public RuntimeMetrics(RuntimeInstrumentationOptions _1)
#pragma warning restore SA1313
{
}
#if NET
private static bool IsGcInfoAvailable
{
get
{
if (isGcInfoAvailable)
{
return true;
}
if (GC.CollectionCount(0) > 0)
{
isGcInfoAvailable = true;
}
return isGcInfoAvailable;
}
}
#endif
private static IEnumerable<Measurement<long>> GetGarbageCollectionCounts()
{
long collectionsFromHigherGeneration = 0;
for (var gen = NumberOfGenerations - 1; gen >= 0; --gen)
{
long collectionsFromThisGeneration = GC.CollectionCount(gen);
yield return new(collectionsFromThisGeneration - collectionsFromHigherGeneration, new KeyValuePair<string, object?>("generation", GenNames[gen]));
collectionsFromHigherGeneration = collectionsFromThisGeneration;
}
}
}