Skip to content

Latest commit

 

History

History
152 lines (109 loc) · 5.11 KB

File metadata and controls

152 lines (109 loc) · 5.11 KB

Getting Started with Prometheus and Grafana

Table of Contents

Export metrics from the application

It is highly recommended to go over the getting started in 5 minutes - Console Application doc before following along this document.

Create a new console application and run it:

dotnet new console --output getting-started-prometheus-grafana
cd getting-started-prometheus-grafana
dotnet run

Add reference to OTLP Exporter:

dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol

Now copy the code from Program.cs and run the application again.

When we ran the application, the OTLP Exporter was attempting to export the metrics to http://localhost:9090/api/v1/otlp/v1/metrics. Since Prometheus server was not running, the metrics received by OtlpExporter were simply dropped on the floor. In the next step, we are going to learn about how to use Prometheus to collect and visualize the metrics.

graph LR

subgraph SDK
  MeterProvider
  MetricReader[BaseExportingMetricReader]
  OtlpExporter
end

subgraph API
  Instrument["Meter(#quot;MyCompany.MyProduct.MyLibrary#quot;, #quot;1.0#quot;)<br/>Counter(#quot;MyFruitCounter#quot;)"]
end

Instrument --> | Measurements | MeterProvider

MeterProvider --> | Metrics | MetricReader --> | Push | OtlpExporter --> | HTTP/protobuf | PrometheusServer[Prometheus server]
Loading

Also, for our learning purpose, use a while-loop to keep increasing the counter value until any key is pressed.

Console.WriteLine("Press any key to exit");

while (!Console.KeyAvailable)
{
    MyFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
    MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
    ...

    Thread.Sleep(300);
}

Collect metrics using Prometheus

Install and run Prometheus

Follow the first steps to download the latest release of Prometheus.

After finished downloading, extract it to a local location that's easy to access. Run the prometheus(.exe) server executable with feature flag otlp-receiver enabled:

./prometheus --enable-feature=otlp-write-receiver

View results in Prometheus

To use the graphical interface for viewing our metrics with Prometheus, navigate to http://localhost:9090/graph, and type MyFruitCounter_total in the expression bar of the UI; finally, click the execute button.

We should be able to see the following chart from the browser:

Prometheus UI

Congratulations!

Now we know how to configure Prometheus server and deploy OpenTelemetry OtlpExporter to export our metrics. Next, we are going to explore a tool called Grafana, which has powerful visualizations for the metrics.

Explore metrics using Grafana

Install Grafana.

Start the standalone Grafana server (grafana-server.exe or ./bin/grafana-server, depending on the operating system). Then, use the browser to navigate to http://localhost:3000/.

Follow the instructions in the Grafana getting started doc to log in.

After successfully logging in, hover on the Configuration icon on the panel at the left hand side, and click on Plugins. Find and click on the Prometheus plugin. Next click on Create a Prometheus data source button. Type in the default endpoint of Prometheus as suggested by the UI as the value for the URI.

http://localhost:9090

At the bottom of the page click Save & test to ensure the data source is working. Then, click on the Explore button - we should be able to write some queries to explore our metrics now!

Feel free to find some handy PromQL here.

In the below example, the query targets to find out what is the per-second rate of increase of MyFruitCounter_total over the past 5 minutes:

Grafana UI

Learn more