-
Notifications
You must be signed in to change notification settings - Fork 25k
Add metrics unit testing #30068
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
Merged
Merged
Add metrics unit testing #30068
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ See [ASP.NET Core metrics](https://github.com/dotnet/aspnetcore/issues/47536) fo | |
|
|
||
| There are two parts to using metrics in a .NET app: | ||
|
|
||
| * **Instrumentation:** Code in .NET libraries takes measurements and associates these measurements with a metric name. | ||
| * **Instrumentation:** Code in .NET libraries takes measurements and associates these measurements with a metric name. .NET and ASP.NET Core include many built-in metrics. | ||
| * **Collection:** A .NET app configures named metrics to be transmitted from the app for external storage and analysis. Some tools may perform configuration outside the app using configuration files or a UI tool. | ||
|
|
||
| Instrumented code can record numeric measurements, but the measurements need to be aggregated, transmitted, and stored to create useful metrics for monitoring. The process of aggregating, transmitting, and storing data is called collection. This tutorial shows several examples of collecting metrics: | ||
|
|
@@ -175,6 +175,53 @@ Alternatively, enter counter category such as `kestrel` in the **Expression** in | |
|
|
||
|  | ||
|
|
||
| ## Test metrics in ASP.NET Core apps | ||
|
|
||
| It's possible to test metrics in ASP.NET Core apps. One way to do that is collect and assert metrics values in [ASP.NET Core integration tests](xref:test/integration-tests) using <xref:Microsoft.Extensions.Telemetry.Testing.Metering.MetricCollector%601>. | ||
|
|
||
| ```cs | ||
| public class BasicTests : IClassFixture<WebApplicationFactory<Program>> | ||
| { | ||
| private readonly WebApplicationFactory<Program> _factory; | ||
| public BasicTests(WebApplicationFactory<Program> factory) => _factory = factory; | ||
|
|
||
| [Fact] | ||
| public async Task Get_RequestCounterIncreased() | ||
| { | ||
| // Arrange | ||
| var client = _factory.CreateClient(); | ||
| var meterFactory = _factory.Services.GetRequiredService<IMeterFactory>(); | ||
| var collector = new MetricCollector<double>(meterFactory, | ||
| "Microsoft.AspNetCore.Hosting", "http.server.request.duration"); | ||
|
|
||
| // Act | ||
| var response = await client.GetAsync("/"); | ||
|
|
||
| // Assert | ||
| Assert.Equal("Hello World!", await response.Content.ReadAsStringAsync()); | ||
|
|
||
| await collector.WaitForMeasurementsAsync(minCount: 1); | ||
| Assert.Collection(collector.GetMeasurementSnapshot(), | ||
| measurement => | ||
| { | ||
| Assert.Equal("http", measurement.Tags["url.scheme"]); | ||
| Assert.Equal("GET", measurement.Tags["http.request.method"]); | ||
| Assert.Equal("/", measurement.Tags["http.route"]); | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The proceeding test: | ||
|
|
||
| * Bootstraps a web app in memory with <xref:Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory%601>. `Program` in the factory's generic argument specifies the web app. | ||
| * Collects metrics values with <xref:Microsoft.Extensions.Telemetry.Testing.Metering.MetricCollector%601>. | ||
| * Requires a package reference to `Microsoft.Extensions.Telemetry.Testing`. | ||
| * The `MetricCollector` is created using the web app's `IMeterFactory`. This allows the collector to only report metrics values recorded by test. | ||
| * Includes the meter name, `Microsoft.AspNetCore.Hosting`, and counter name, `http.server.request.duration` to collect. | ||
| * Makes a HTTP request to the app web. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. app web -> web app?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * Asserts the test using results from the metrics collector. | ||
|
|
||
| ## ASP.NET Core meters and counters | ||
|
|
||
| * [HostingMetrics](https://source.dot.net/#Microsoft.AspNetCore.Hosting/Internal/HostingMetrics.cs,0e3eae9d5e50ff0d) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the code under test does not produce the intended metric, is this just going to hang indefinitely until some higher level test timeout occurs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added WaitAsync - #30091