-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: InstrumentRecorder - notification of new measurements #86783
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsBackground and motivation
var instrumentRecorder = new InstrumentRecorder<double>(meterRegistry, "Microsoft.AspNetCore.Hosting", "request-duration");
var response = await httpClient.GetAsync("/");
var measurements = instrumentRecorder.GetMeasurements();
// Assert contents of measurements However, there are some cases where there are race conditions because measurements happen on a different thread. For example, in the counter above, the server can return the failing response to the client before the metric is recorded. It would be useful if someone using API Proposalnamespace System.Diaganostics.Metrics;
public class InstumentRecorder<T>
{
public Task<IEnumerable<Measurement<T>> WaitForMeasurementsAsync(bool clear = false);
}
If someone wants to wait again then they would need to clear the measurements when they wait/get measurements. API Usagevar instrumentRecorder = new InstrumentRecorder<double>(meterRegistry, "Microsoft.AspNetCore.Hosting", "request-duration");
var response = await httpClient.GetAsync("/");
var measurements = await instrumentRecorder.WaitForMeasurementsAsync();
// Assert contents of measurements Alternative DesignsAn API to register a callback that executes for each measurement:
RisksNo response
|
I think we'd want some timeout or cancellation on the wait, otherwise tests that fail to generate the expected measurement might run a very long time. Other than that it seems fine to me. |
The alternative proposal utilizing the Register feature appears superior since it offers greater flexibility, not limited solely to the number of measurements passed to the Wait methods. var instrumentRecorder = new InstrumentRecorder<double>(meterRegistry, "Microsoft.AspNetCore.Hosting", "request-duration");
var tcs = new TaskCompletionSource<bool>();
instrumentRecorder.Register(recorder, measurement, state => {
// can check the current measurments or even get all measurments by calling recorder.GetMeasurements()
// can call this when done getting and checking all needed measurments
(state as TaskCompletionSource<bool>).TrySetResult(true);
}, tcs);
Assert.Equal(0, Task.WaitAny(tcs.Task, Task.Delay(10000))); // Ensure to check timing out the operation |
Tarek and I discussed this offline. I'll summarize my thoughts:
|
I am thinking in the following shape of the API public Task<IEnumerable<Measurement<T>> WaitForMeasurementsAsync(
int minimumCount = 1,
bool clear = false,
TimeSpan timeout = TimeSpan.InfiniteTimeSpan,
CancellationToken cancellationToken = default); We need to think about how this API is going to work with the Observable instruments (that work as pull instruments). There is some suggestion from @noahfalk to add a new API to |
We removed the InstrumentRecorder from the runtime. Users can use MetricCollector |
Background and motivation
InstrumentRecorder
is an API new in .NET 8 to make testing metrics easier:However, there are some cases where there are race conditions because measurements happen on a different thread. For example, with the
request-duration
counter above, the server can return the failing response to the client before the metric is recorded.GetMeasurements
may or may not have an item. The test is flaky.It would be useful if someone using
InstrumentRecorder
could get notifications of new measurements.API Proposal
WaitForMeasurementsAsync
returns when one or more measurements are available. If there are already measurements, the method returns immediately.Random thoughts:
WaitForMeasurementsAsync(minimumCount: 5)
. Edit: Added above.WaitForMeasurementsAsync
allowed? What happens if there are parallel calls toWaitForMeasurementsAsync
, and they haveclear: true
?API Usage
Alternative Designs
An API to register a callback that executes for each measurement:
Risks
No response
The text was updated successfully, but these errors were encountered: