Export to Prometheus and Grafana
Você está visualizando a versão em versão em inglês desta página porque ela ainda não foi traduzida. Possui interesse em ajudar? Veja como contribuir.
This guide will show you how to export OpenTelemetry metrics to Prometheus and visualize them in Grafana.
Prerequisites
- .NET SDK installed on your computer
- Prometheus downloaded (we’ll cover installation)
- Grafana downloaded (we’ll cover installation)
Creating a .NET application with OTLP export
First, follow the Getting Started with Console guide to understand the basics of metrics collection.
Create a new console application:
dotnet new console --output getting-started-prometheus-grafana
cd getting-started-prometheus-grafana
Install the OpenTelemetry OTLP Exporter package:
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
Update the Program.cs
file with the following code:
using System;
using System.Diagnostics.Metrics;
using System.Threading;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
// Define a meter
var myMeter = new Meter("MyCompany.MyProduct.MyLibrary", "1.0");
// Create a counter instrument
var myFruitCounter = myMeter.CreateCounter<long>("MyFruitCounter");
// Configure the OpenTelemetry MeterProvider with OTLP export
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("MyCompany.MyProduct.MyLibrary")
.AddOtlpExporter((exporterOptions, metricReaderOptions) =>
{
exporterOptions.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
exporterOptions.Protocol = OtlpExportProtocol.HttpProtobuf;
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
.Build();
Console.WriteLine("Press any key to exit");
// Keep generating metrics until user presses a key
while (!Console.KeyAvailable)
{
myFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
myFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
myFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));
myFruitCounter.Add(2, new("name", "apple"), new("color", "green"));
myFruitCounter.Add(5, new("name", "apple"), new("color", "red"));
myFruitCounter.Add(4, new("name", "lemon"), new("color", "yellow"));
Thread.Sleep(300);
}
// Dispose meter provider before the application ends.
// This will flush the remaining metrics and shutdown the metrics pipeline.
meterProvider.Dispose();
When you run this application, it will attempt to export metrics to Prometheus
at http://localhost:9090/api/v1/otlp/v1/metrics
. Initially, this will fail
because we haven’t set up Prometheus yet, which we’ll do next.
Setting up Prometheus
Prometheus is an open source monitoring and alerting system that can scrape and store metrics.
Installing and running Prometheus
- Download Prometheus from the official site
- Extract it to a location on your machine
- Run Prometheus with the OTLP receiver enabled:
./prometheus --web.enable-otlp-receiver
The --web.enable-otlp-receiver
flag enables
Prometheus to receive metrics through the OpenTelemetry Protocol (OTLP).
Viewing metrics in Prometheus
- Run your .NET application (it should now successfully export metrics to Prometheus)
- Open a web browser and navigate to http://localhost:9090/graph
- In the expression bar, type
MyFruitCounter_total
and click Execute
You should see a graph showing the increasing counter values for each combination of fruit name and color.
Setting up Grafana
Grafana provides more powerful visualization capabilities than the basic Prometheus UI.
Installing and running Grafana
- Install Grafana following the official instructions
- Start the Grafana server (command varies by OS)
- Navigate to http://localhost:3000 in your browser
- Log in with the default credentials (username:
admin
, password:admin
) and set a new password when prompted
Configuring Prometheus as a data source
- In Grafana, hover over the Configuration (gear) icon in the left sidebar and click “Data sources”
- Click “Add data source”
- Select “Prometheus”
- Set the URL to
http://localhost:9090
- Click “Save & Test” at the bottom
Creating a dashboard
- Click the “+” icon in the left sidebar and select “Dashboard”
- Click “Add new panel”
- In the query editor, enter a PromQL query such as
rate(MyFruitCounter_total[5m])
to see the per-second rate of increase over the past 5 minutes - Click “Apply” to add the panel to your dashboard
- Save your dashboard by clicking the save icon in the top right
Understanding the metrics flow
flowchart LR App[.NET App] --> Exporter[OTLP Exporter] Exporter --> Prometheus[Prometheus Server] Prometheus --> Grafana[Grafana Dashboard]
- Your .NET application collects metrics using OpenTelemetry instruments
- The OTLP Exporter sends these metrics to Prometheus using the OTLP protocol
- Prometheus stores the metrics in its time-series database
- Grafana queries Prometheus and visualizes the metrics in dashboards
Learn more
- Prometheus documentation
- Grafana documentation
- PromQL cheat sheet
- OpenTelemetry Protocol (OTLP) specification
Feedback
Was this page helpful?
Thank you. Your feedback is appreciated!
Please let us know how we can improve this page. Your feedback is appreciated!