Skip to content

Conversation

@JamesNK
Copy link
Member

@JamesNK JamesNK commented Aug 16, 2023

Addresses dotnet/aspnetcore#33387


Internal previews

📄 File 🔗 Preview link
aspnetcore/log-mon/metrics/metrics.md ASP.NET Core metrics


## Test metrics in ASP.NET Core apps

It's possible to test metrics in ASP.NET Core apps.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It's possible to test metrics in ASP.NET Core apps.
It's possible to test metrics in ASP.NET Core apps with the `MetricCollector<T>`.

?

The MetricCollector is the heart here for testing, so I'd like to see that type given in a prominent place.

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<T>>.
Copy link
Contributor

@Rick-Anderson Rick-Anderson Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a valid link but is breaking the build for some reason. @tdykstra ?
you could use

[`MetricCollector<T>`](/dotnet/api/microsoft.extensions.telemetry.testing.metering.metriccollector-1.-ctor?view=dotnet-plat-ext-8.0#microsoft-extensions-telemetry-testing-metering-metriccollector-1-ctor(system-diagnostics-metrics-instrument((-0))-system-timeprovider)) 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't a valid link -- it needed %601 instead of <T>

@JamesNK JamesNK merged commit bfafbd2 into main Aug 17, 2023
@JamesNK JamesNK deleted the jamesnk/metrics-testing branch August 17, 2023 02:13
// Assert
Assert.Equal("Hello World!", await response.Content.ReadAsStringAsync());

await collector.WaitForMeasurementsAsync(minCount: 1);
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added WaitAsync - #30091

* 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app web -> web app?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants