Spoiler: In this article, we’ll walk through how to collect and visualize metrics from an ASP.NET Core application using OpenTelemetry, Prometheus, and Grafana. We’ll configure the necessary services and see how to monitor performance metrics in a real-time dashboard.
Collect metrics with OTLP
A metric is a measurement of your application’s behavior that we aggregate over a given time period and capture at runtime. OpenTelemetry (OTLP) is an industry standard for collecting and exporting metrics. Here’s an example of how to set it up in your ASP.NET Core application metrics:
appBuilder.Services.AddOpenTelemetry()
.ConfigureResource(builder => builder
.AddService(serviceName: "my_service_name"))
.WithMetrics(builder => builder
.AddAspNetCoreInstrumentation() // metric collector
);
Produce metrics with Prometheus
Once you've collected metrics with OpenTelemetry, you can export them using any supported OpenTelemetry exporter. To export them to Prometheus, we’ll add the .AddPrometheusExporter()
method to the metric configuration. It works as a pull model, with a scrap date from the application. That means, an application should provide an endpoint with metrics for scrapping. To do that we need to add middleware app.MapPrometheusScrapingEndpoint()
. Prometheus scrapes this /metrics
endpoint at regular intervals to collect the application’s time-series data metrics:
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027 1395066363000
Define Grafana charts
The dotnet team created a Grafana charts for the built-in Asp Net Core metrics. We can also search the community charts published in Grafana. We just need to import it.
Run locally
We will use docker-compose with AspNet Core, Prometheus, and Grafana containers. We download Grafana charts and add them to the container (grafana folder). We also configured the Prometheus scrab job, prometheus.yml
.
services:
httpprometheus:
image: ${DOCKER_REGISTRY-}httpprometheus
build:
context: HttpPrometheus
dockerfile: Dockerfile
environment:
- OTEL_RESOURCE_ATTRIBUTES=service.namespace=demo,deployment.environment=dev
prometheus:
image: prom/prometheus
restart: always
ports:
- 9090:9090
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
grafana:
image: grafana/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=P@ssw0rd
restart: always
ports:
- 3000:3000
volumes:
- ./grafana/datasource.yml:/etc/grafana/provisioning/datasources/datasource.yml
- ./grafana/dashboards:/etc/grafana/provisioning/dashboards
Source code
Vsualize Asp Net Core metrics with Promethous and Grafana
An example of Asp Net Core metric configuration using Promethous and Grafana. Also docker-compose was provided to run locally.
Requirements
- .NET8
- Docker
Help Links
- https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/metrics/getting-started-aspnetcore/README.md
- https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md
- https://grafana.com/orgs/dotnetteam/dashboards
- https://grafana.com/grafana/dashboards/17706-asp-net-otel-metrics/
Top comments (0)