Introducing ASP.NET Core metrics and Grafana dashboards in .NET 8

James Newton-King

Metrics report diagnostics about your app. .NET 8 adds over a dozen useful metrics to ASP.NET Core:

  • HTTP request counts and duration
  • Number of active HTTP requests
  • Route matching results
  • Rate limiting lease and queue durations
  • SignalR transport usage
  • Low-level connection and TLS usage from Kestrel
  • Error handling diagnostics
  • And more

Metrics are numerical measurements reported over time. For example, each HTTP request handled by ASP.NET Core has its duration recorded to the http.server.request.duration metric. Tooling such as the .NET OpenTelemetry SDK can then be configured by an app to export metric data to a telemetry store such as Prometheus or Azure Monitor. Metrics are part of the OpenTelemetry standard and all modern tools support them.

Metrics data are useful when combined with tooling to monitor the health and activity of apps:

  • Display graphs on a dashboard to observe your app over time. For example, view activity from people using the app.
  • Trigger alerts in real-time if the app exceeds a threshold. For example, send an email if request duration or error count exceeds a limit.

Using metrics

ASP.NET Core’s built-in metrics are automatically recorded. How you use these metrics is up to you. Let’s explore some of the options available.

.NET Aspire dashboard

.NET Aspire is an opinionated stack for building observable, distributed applications. The Aspire dashboard includes a simple, user-friendly UI for viewing structured logs, traces and metrics. Aspire apps are automatically configured to send telemetry data to the dashboard during development.

A recording of the .NET Aspire dashboard metrics

Here you can see a collection of available metrics, along with name, description, and a graph of values. The Aspire UI includes metrics filters. Filtering is possible using a powerful feature of metrics: attributes.

Each time a value is recorded, it is tagged with metadata called attributes. For example, http.server.request.duration records a HTTP request duration along with attributes about that request: server address, HTTP request method, matched route, response status code and more. Attributes can then be queried to get the exact data you want:

  • HTTP request duration to a specific endpoint in your app, e.g. /product/{name}.
  • Count the number of requests with 4xx HTTP response codes.
  • View request count that threw a server exception over time.
  • HTTP vs HTTPS request duration.
  • Number of visitors using HTTP/1.1 vs HTTP/2.

ASP.NET Core Grafana dashboards

Grafana is powerful open-source tool for building advanced dashboards and alerts. It allows you to create interactive, customizable dashboards with a variety of panels, graphs, and charts. Once complete, a dashboard displays data from your telemetry store. Grafana is a good choice to monitor apps deployed to production and a dashboard gives a real-time view of an app health and usage.

Grafana gives you the power to build exactly what you want, but it takes time to build high-quality dashboards. As part of adding metrics in .NET 8, the .NET team created pre-built dashboards designed for ASP.NET Core’s built-in metrics.

A screenshot of the ASP.NET Core Grafana dashboard

The ASP.NET Core Grafana dashboards are open source on GitHub and available for download on grafana.com. You can use the dashboard as they are or customize them further to build a solution tailored to your needs.

Quickly try out Grafana + ASP.NET Core using the .NET Aspire metrics sample app.

And more

  • Metrics aren’t limited to what is built into .NET. You can create custom metrics for your apps.
  • dotnet-counters is a command-line tool that can view live metrics for .NET Core apps on demand. It doesn’t require setup, making it useful for ad-hoc investigations or verifying that metric instrumentation is working.
  • Use metrics in unit tests. ASP.NET Core integration testing, MetricCollector and IMeterFactory can be combined to assert values recorded by a test.

Try it now

.NET 8, ASP.NET Core Grafana dashboards and .NET Aspire (preview) are available now. Try using metrics today and let us know what you think:

Thanks for trying out .NET 8 and metrics!

12 comments

Leave a comment

  • Michael Teubner 0

    Why is the date on this blog post six days in the future?

  • Robert Gelb 0

    @James, do these libraries work with .NET 7? Or are them .NET 8 specific?

  • Archippe Araza 0

    Thanks @James for this insightful share.

    Can you tell us more about the benefits of using this instead of Azure Monitor in a cloud scenario ?

    • James Newton-KingMicrosoft employee 4

      I don’t have a huge amount of experience with Azure Monitor. I’m not qualified to give a good comparison between Azure Monitor vs Prometheus/Grafana.

      One notable difference is that while Grafana and Prometheus have managed offerings, e.g. Azure Managed Grafana or Grafana Cloud, they can also be run on your own machine. If you want a solution that is portable then Prometheus/Grafana are a good choice.

  • Portal User 2 0

    Thank you James. Also think about adding a “lite” version of these metrics dashboards directly in the Aspire dashboard app. In other words, this solution requires both Prometheus and Grafana containers. It would be nice to have a ‘lite’ dashboard built in blazor/html directly inside the Aspire Dashboard app (without needing those two extra containers) – just displaying the transient in-memory telemetry. That would be a fun job for an intern to spend a few weeks on.

    • James Newton-KingMicrosoft employee 2

      The Aspire dashboard has a capability to view metrics today. Because we have a simple solution, and it’s quite easy to setup Prometheus and Grafana in Aspire, we probably won’t add something like metrics dashboards to the Aspire dashboard app.

      But if you think it is important then create an issue at https://github.com/dotnet/aspire and we can see how much demand there is by Aspire users.

  • Alexey Sosnin 1

    Is there any recommended Grafana dashboard for http.client.* metrics?

    • James Newton-KingMicrosoft employee 2

      We haven’t made a dashboard that includes client metrics, but you could copy or extend the ASP.NET Core dashboards to include them.

      http.client.request.duration is very similar to http.server.request.duration so charts could be reused without significant changes.

  • iain 1

    So, if I want to migrate an existing cloud app to this and I’m not using Aspire (as it’s in Preview) I would have to run my own Prometheus server in Azure? There doesn’t seem to be an Azure managed option for Prometheus? (Outside the Kubernetes specific Azure Monitor option)

  • Artyom Petrosov 0

    Added grafana dashboards, but they dont show anything. We expose only OpenTelemetry metrics with otel exporter and otel collector. Do i need a prometheus scraping my app and collecting additional metrics for this dashboards to work?

Feedback usabilla icon