-
Notifications
You must be signed in to change notification settings - Fork 740
/
MetricCollector.cs
348 lines (304 loc) · 12.5 KB
/
MetricCollector.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.Telemetry.Testing.Metering;
/// <summary>
/// Collects the measurements published from an <see cref="Instrument{T}"/> or <see cref="ObservableInstrument{T}"/>.
/// </summary>
/// <typeparam name="T">The type of metric data being recorded.</typeparam>
[Experimental]
[DebuggerDisplay("{_measurements.Count} measurements")]
public sealed class MetricCollector<T> : IDisposable
where T : struct
{
private static readonly HashSet<Type> _supportedTs = new()
{
typeof(int),
typeof(byte),
typeof(short),
typeof(long),
typeof(float),
typeof(double),
typeof(decimal),
};
internal int WaitersCount => _waiters.Count; // Internal for testing
private readonly MeterListener _meterListener = new();
private readonly List<CollectedMeasurement<T>> _measurements = new();
private readonly List<Waiter> _waiters = new();
private readonly TimeProvider _timeProvider;
private bool _disposed;
private Instrument? _instrument;
/// <summary>
/// Initializes a new instance of the <see cref="MetricCollector{T}"/> class.
/// </summary>
/// <param name="instrument">The <see cref="Instrument{T}" /> to record measurements from.</param>
/// <param name="timeProvider">The time provider to use, or <see langword="null"/> to use the system time provider.</param>
public MetricCollector(Instrument<T> instrument, TimeProvider? timeProvider = null)
: this(timeProvider)
{
_instrument = Throw.IfNull(instrument);
_meterListener.SetMeasurementEventCallback<T>(OnMeasurementRecorded);
_meterListener.EnableMeasurementEvents(instrument);
_meterListener.Start();
}
/// <summary>
/// Initializes a new instance of the <see cref="MetricCollector{T}"/> class.
/// </summary>
/// <param name="instrument">The <see cref="ObservableInstrument{T}" /> to record measurements from.</param>
/// <param name="timeProvider">The time provider to use, or <see langword="null"/> to use the system time provider.</param>
public MetricCollector(ObservableInstrument<T> instrument, TimeProvider? timeProvider = null)
: this(timeProvider)
{
_instrument = Throw.IfNull(instrument);
_meterListener.SetMeasurementEventCallback<T>(OnMeasurementRecorded);
_meterListener.EnableMeasurementEvents(instrument);
_meterListener.Start();
}
/// <summary>
/// Initializes a new instance of the <see cref="MetricCollector{T}"/> class.
/// </summary>
/// <param name="meterScope">The scope of the meter that publishes the instrument to record.
/// Take caution when using Meters in the global scope (scope == null). This interacts with
/// static mutable data and tests doing this should not be run in parallel with each other.
/// </param>
/// <param name="meterName">The name of the meter that publishes the instrument to record.</param>
/// <param name="instrumentName">The name of the instrument to record.</param>
/// <param name="timeProvider">The time provider to use, or <see langword="null"/> to use the system time provider.</param>
/// <remarks>
/// Both the meter name and scope are used to identity the meter of interest.
/// </remarks>
public MetricCollector(object? meterScope, string meterName, string instrumentName, TimeProvider? timeProvider = null)
: this(timeProvider)
{
_ = Throw.IfNullOrEmpty(meterName);
_ = Throw.IfNullOrEmpty(instrumentName);
Initialize(instrument => Equals(instrument.Meter.Scope, meterScope) && instrument.Meter.Name == meterName && instrument.Name == instrumentName);
}
/// <summary>
/// Initializes a new instance of the <see cref="MetricCollector{T}"/> class.
/// </summary>
/// <param name="meter">The meter that publishes the instrument to record.</param>
/// <param name="instrumentName">The name of the instrument to record.</param>
/// <param name="timeProvider">The time provider to use, or <see langword="null"/> to use the system time provider.</param>
public MetricCollector(Meter meter, string instrumentName, TimeProvider? timeProvider = null)
: this(timeProvider)
{
_ = Throw.IfNull(meter);
_ = Throw.IfNullOrEmpty(instrumentName);
Initialize(instrument => ReferenceEquals(instrument.Meter, meter) && instrument.Name == instrumentName);
}
private MetricCollector(TimeProvider? timeProvider)
{
if (!_supportedTs.Contains(typeof(T)))
{
var str = string.Join(", ", _supportedTs.Select(t => t.Name).ToArray());
throw new InvalidOperationException($"MetricCollector can only be created for the following types: {str}.");
}
_timeProvider = timeProvider ?? TimeProvider.System;
}
/// <summary>
/// Disposes the <see cref="MetricCollector{T}"/> and stops recording measurements.
/// </summary>
public void Dispose()
{
lock (_measurements)
{
if (_disposed)
{
return;
}
_disposed = true;
_meterListener.Dispose();
_measurements.Clear();
// wake up anybody still waiting and inform them of the bad news: their horse is dead...
foreach (var w in _waiters)
{
_ = w.TaskSource.TrySetException(MakeObjectDisposedException());
}
_waiters.Clear();
}
}
/// <summary>
/// Gets the <see cref="Instrument"/> that is being recorded.
/// </summary>
/// <remarks>
/// This may be <see langword="null"/> until the instrument is published.
/// </remarks>
public Instrument? Instrument => _instrument;
/// <summary>
/// Removes all accumulated measurements from the collector.
/// </summary>
public void Clear()
{
lock (_measurements)
{
ThrowIfDisposed();
_measurements.Clear();
}
}
/// <summary>
/// Gets a snapshot of measurements collected by this collector.
/// </summary>
/// <param name="clear">Setting this to <see langword="true"/> will atomically clear the set of accumulated measurements.</param>
/// <returns>The measurements recorded by this collector, ordered by recording time.</returns>
public IReadOnlyList<CollectedMeasurement<T>> GetMeasurementSnapshot(bool clear = false)
{
lock (_measurements)
{
ThrowIfDisposed();
var measurements = _measurements.ToArray();
if (clear)
{
_measurements.Clear();
}
return measurements;
}
}
/// <summary>
/// Gets the latest measurement collected, if any.
/// </summary>
public CollectedMeasurement<T>? LastMeasurement
{
get
{
lock (_measurements)
{
ThrowIfDisposed();
return _measurements.Count > 0 ? _measurements[_measurements.Count - 1] : null;
}
}
}
/// <summary>
/// Returns a task that completes when the collector has collected a minimum number of measurements.
/// </summary>
/// <param name="minCount">The minimum number of measurements to wait for.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task that completes when the collector has collected the requisite number of measurements.</returns>
public Task WaitForMeasurementsAsync(int minCount, CancellationToken cancellationToken = default)
{
_ = Throw.IfLessThan(minCount, 1);
Waiter w;
lock (_measurements)
{
ThrowIfDisposed();
if (_measurements.Count >= minCount)
{
return Task.CompletedTask;
}
w = new Waiter(minCount);
_waiters.Add(w);
}
if (cancellationToken.CanBeCanceled)
{
_ = cancellationToken.Register(() =>
{
lock (_measurements)
{
#if NET6_0_OR_GREATER
w.TaskSource.TrySetCanceled(cancellationToken);
#else
w.TaskSource.TrySetCanceled();
#endif
_waiters.Remove(w);
}
});
}
return w.TaskSource.Task;
}
/// <summary>
/// Returns a task that completes when the collector has collected a minimum number of measurements.
/// </summary>
/// <param name="minCount">The minimum number of measurements to wait for.</param>
/// <param name="timeout">How long to wait.</param>
/// <returns>A task that completes when the collector has collected the requisite number of measurements.</returns>
[SuppressMessage("Resilience", "R9A061:The async method doesn't support cancellation", Justification = "Not relevant in this case")]
public async Task WaitForMeasurementsAsync(int minCount, TimeSpan timeout)
{
#if NET8_0_OR_GREATER
using var cancellationTokenSource = new CancellationTokenSource(timeout, _timeProvider);
#else
using var cancellationTokenSource = _timeProvider.CreateCancellationTokenSource(timeout);
#endif
await WaitForMeasurementsAsync(minCount, cancellationTokenSource.Token).ConfigureAwait(false);
}
/// <summary>
/// Scan all registered observable instruments.
/// </summary>
public void RecordObservableInstruments()
{
ThrowIfDisposed();
_meterListener.RecordObservableInstruments();
}
private void Initialize(Func<Instrument, bool> instrumentPredicate)
{
_meterListener.InstrumentPublished = (instrument, listener) =>
{
if ((instrument is ObservableInstrument<T> or Instrument<T>) && instrumentPredicate(instrument))
{
if (Interlocked.CompareExchange(ref _instrument, instrument, null) is null)
{
// no need to hear about new instruments being published
listener.InstrumentPublished = null;
// get ready to listen to measurement events
listener.SetMeasurementEventCallback<T>(OnMeasurementRecorded);
// let the flood gates open
listener.EnableMeasurementEvents(instrument);
}
}
};
// start listening to stuff...
_meterListener.Start();
}
private void OnMeasurementRecorded(Instrument instrument, T measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state)
{
var m = new CollectedMeasurement<T>(measurement, tags, _timeProvider.GetUtcNow());
lock (_measurements)
{
if (!_disposed)
{
// record the measurement
_measurements.Add(m);
// wake up any waiters that need it
for (int i = _waiters.Count - 1; i >= 0; i--)
{
if (_measurements.Count >= _waiters[i].MinCount)
{
// we use TrySetResult since the task may already be in the Cancelled state due to a timeout.
_ = _waiters[i].TaskSource.TrySetResult(true);
_waiters.RemoveAt(i);
}
}
}
}
}
private void ThrowIfDisposed()
{
if (_disposed)
{
throw MakeObjectDisposedException();
}
}
private ObjectDisposedException MakeObjectDisposedException()
=> _instrument != null
? new(nameof(MetricCollector<T>), $"The metric collector instance for instrument '{_instrument.Name}' of meter '{_instrument.Meter.Name}' has been disposed.")
: new(nameof(MetricCollector<T>));
private readonly struct Waiter
{
public Waiter(int minCount)
{
MinCount = minCount;
}
public int MinCount { get; }
// The TCS is modified in a lock.
// Use RunContinuationsAsynchronously to ensure continuations don't run when holding the lock.
public TaskCompletionSource<bool> TaskSource { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
}
}