/
MeterListener.cs
306 lines (274 loc) · 12.8 KB
/
MeterListener.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Diagnostics.Metrics
{
/// <summary>
/// A delegate to represent the Meterlistener callbacks used in measurements recording operation.
/// </summary>
public delegate void MeasurementCallback<T>(Instrument instrument, T measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state) where T : struct;
/// <summary>
/// MeterListener is class used to listen to the metrics instrument measurements recording.
/// </summary>
#if ALLOW_PARTIALLY_TRUSTED_CALLERS
[System.Security.SecuritySafeCriticalAttribute]
#endif
public sealed class MeterListener : IDisposable
{
// We use LikedList here so we don't have to take any lock while iterating over the list as we always hold on a node which be either valid or null.
// DiagLinkedList is thread safe for Add, Remove, and Clear operations.
private static List<MeterListener> s_allStartedListeners = new List<MeterListener>();
// List of the instruments which the current listener is listening to.
private DiagLinkedList<Instrument> _enabledMeasurementInstruments = new DiagLinkedList<Instrument>();
private bool _disposed;
// We initialize all measurement callback with no-op operations so we'll avoid the null checks during the execution;
private MeasurementCallback<byte> _byteMeasurementCallback = (instrument, measurement, tags, state) => { /* no-op */ };
private MeasurementCallback<short> _shortMeasurementCallback = (instrument, measurement, tags, state) => { /* no-op */ };
private MeasurementCallback<int> _intMeasurementCallback = (instrument, measurement, tags, state) => { /* no-op */ };
private MeasurementCallback<long> _longMeasurementCallback = (instrument, measurement, tags, state) => { /* no-op */ };
private MeasurementCallback<float> _floatMeasurementCallback = (instrument, measurement, tags, state) => { /* no-op */ };
private MeasurementCallback<double> _doubleMeasurementCallback = (instrument, measurement, tags, state) => { /* no-op */ };
private MeasurementCallback<decimal> _decimalMeasurementCallback = (instrument, measurement, tags, state) => { /* no-op */ };
/// <summary>
/// Creates a MeterListener object.
/// </summary>
public MeterListener() { }
/// <summary>
/// Callbacks to get notification when an instrument is published.
/// </summary>
public Action<Instrument, MeterListener>? InstrumentPublished { get; set; }
/// <summary>
/// Callbacks to get notification when stopping the measurement on some instrument.
/// This can happen when the Meter or the Listener is disposed or calling <see cref="Dispose" /> on the listener.
/// </summary>
public Action<Instrument, object?>? MeasurementsCompleted { get; set; }
/// <summary>
/// Start listening to a specific instrument measurement recording.
/// </summary>
/// <param name="instrument">The instrument to listen to.</param>
/// <param name="state">A state object which will be passed back to the callback getting measurements events.</param>
public void EnableMeasurementEvents(Instrument instrument, object? state = null)
{
bool oldStateStored = false;
bool enabled = false;
object? oldState = null;
lock (Instrument.SyncObject)
{
if (instrument is not null && !_disposed && !instrument.Meter.Disposed)
{
_enabledMeasurementInstruments.AddIfNotExist(instrument, object.ReferenceEquals);
oldState = instrument.EnableMeasurement(new ListenerSubscription(this, state), out oldStateStored);
enabled = true;
}
}
if (enabled)
{
if (oldStateStored && MeasurementsCompleted is not null)
{
MeasurementsCompleted?.Invoke(instrument!, oldState);
}
}
else
{
// The caller trying to enable the measurements but it didn't happen because the meter or the listener is disposed.
// We need to send MeasurementsCompleted notification telling this instrument is not enabled for measuring.
MeasurementsCompleted?.Invoke(instrument!, state);
}
}
/// <summary>
/// Stop listening to a specific instrument measurement recording.
/// </summary>
/// <param name="instrument">The instrument to stop listening to.</param>
/// <returns>The state object originally passed to <see cref="EnableMeasurementEvents" /> method.</returns>
public object? DisableMeasurementEvents(Instrument instrument)
{
object? state = null;
lock (Instrument.SyncObject)
{
if (instrument is null || _enabledMeasurementInstruments.Remove(instrument, object.ReferenceEquals) == default)
{
return default;
}
state = instrument.DisableMeasurements(this);
}
MeasurementsCompleted?.Invoke(instrument, state);
return state;
}
/// <summary>
/// Sets a callback for a specific numeric type to get the measurement recording notification from all instruments which enabled listening and was created with the same specified numeric type.
/// If a measurement of type T is recorded and a callback of type T is registered, that callback will be used.
/// </summary>
/// <param name="measurementCallback">The callback which can be used to get measurement recording of numeric type T.</param>
public void SetMeasurementEventCallback<T>(MeasurementCallback<T>? measurementCallback) where T : struct
{
measurementCallback ??= (instrument, measurement, tags, state) => { /* no-op */};
if (typeof(T) == typeof(byte))
{
_byteMeasurementCallback = (MeasurementCallback<byte>)(object)measurementCallback;
}
else if (typeof(T) == typeof(int))
{
_intMeasurementCallback = (MeasurementCallback<int>)(object)measurementCallback;
}
else if (typeof(T) == typeof(float))
{
_floatMeasurementCallback = (MeasurementCallback<float>)(object)measurementCallback;
}
else if (typeof(T) == typeof(double))
{
_doubleMeasurementCallback = (MeasurementCallback<double>)(object)measurementCallback;
}
else if (typeof(T) == typeof(decimal))
{
_decimalMeasurementCallback = (MeasurementCallback<decimal>)(object)measurementCallback;
}
else if (typeof(T) == typeof(short))
{
_shortMeasurementCallback = (MeasurementCallback<short>)(object)measurementCallback;
}
else if (typeof(T) == typeof(long))
{
_longMeasurementCallback = (MeasurementCallback<long>)(object)measurementCallback;
}
else
{
throw new InvalidOperationException(SR.Format(SR.UnsupportedType, typeof(T)));
}
}
/// <summary>
/// Enable the listener to start listening to instruments measurement recording.
/// </summary>
public void Start()
{
List<Instrument>? publishedInstruments = null;
lock (Instrument.SyncObject)
{
if (_disposed)
{
return;
}
if (!s_allStartedListeners.Contains(this))
{
s_allStartedListeners.Add(this);
publishedInstruments = Meter.GetPublishedInstruments();
}
}
if (publishedInstruments is not null)
{
foreach (Instrument instrument in publishedInstruments)
{
InstrumentPublished?.Invoke(instrument, this);
}
}
}
/// <summary>
/// Calls all Observable instruments which the listener is listening to then calls <see cref="SetMeasurementEventCallback" /> with every collected measurement.
/// </summary>
public void RecordObservableInstruments()
{
List<Exception>? exceptionsList = null;
DiagNode<Instrument>? current = _enabledMeasurementInstruments.First;
while (current is not null)
{
if (current.Value.IsObservable)
{
try
{
current.Value.Observe(this);
}
catch (Exception e)
{
exceptionsList ??= new List<Exception>();
exceptionsList.Add(e);
}
}
current = current.Next;
}
if (exceptionsList is not null)
{
throw new AggregateException(exceptionsList);
}
}
/// <summary>
/// Disposes the listeners which will stop it from listening to any instrument.
/// </summary>
public void Dispose()
{
Dictionary<Instrument, object?>? callbacksArguments = null;
Action<Instrument, object?>? measurementsCompleted = MeasurementsCompleted;
lock (Instrument.SyncObject)
{
if (_disposed)
{
return;
}
_disposed = true;
s_allStartedListeners.Remove(this);
DiagNode<Instrument>? current = _enabledMeasurementInstruments.First;
if (current is not null && measurementsCompleted is not null)
{
callbacksArguments = new Dictionary<Instrument, object?>();
do
{
object? state = current.Value.DisableMeasurements(this);
callbacksArguments.Add(current.Value, state);
current = current.Next;
} while (current is not null);
_enabledMeasurementInstruments.Clear();
}
}
if (callbacksArguments is not null)
{
foreach (KeyValuePair<Instrument, object?> kvp in callbacksArguments)
{
measurementsCompleted?.Invoke(kvp.Key, kvp.Value);
}
}
}
// Publish is called from Instrument.Publish
internal static List<MeterListener>? GetAllListeners() => s_allStartedListeners.Count == 0 ? null : new List<MeterListener>(s_allStartedListeners);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void NotifyMeasurement<T>(Instrument instrument, T measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state) where T : struct
{
if (typeof(T) == typeof(byte))
{
_byteMeasurementCallback(instrument, (byte)(object)measurement, tags, state);
}
if (typeof(T) == typeof(short))
{
_shortMeasurementCallback(instrument, (short)(object)measurement, tags, state);
}
if (typeof(T) == typeof(int))
{
_intMeasurementCallback(instrument, (int)(object)measurement, tags, state);
}
if (typeof(T) == typeof(long))
{
_longMeasurementCallback(instrument, (long)(object)measurement, tags, state);
}
if (typeof(T) == typeof(float))
{
_floatMeasurementCallback(instrument, (float)(object)measurement, tags, state);
}
if (typeof(T) == typeof(double))
{
_doubleMeasurementCallback(instrument, (double)(object)measurement, tags, state);
}
if (typeof(T) == typeof(decimal))
{
_decimalMeasurementCallback(instrument, (decimal)(object)measurement, tags, state);
}
}
}
internal readonly struct ListenerSubscription
{
internal ListenerSubscription(MeterListener listener, object? state = null)
{
Listener = listener;
State = state;
}
internal MeterListener Listener { get; }
internal object? State { get; }
}
}