Introduction
In my previous article, I talked about telemetry and its benefits, and I implemented a telemetry sample using Jaeger.
In this article, I will continue on the same subject, but I will show other tools that can help to enhance the metrics visibility of your Web API. I will start by creating a Web API project, then I'll use Prometheus to expose the metrics and then I'll connect Prometheus with Grafana to have a clear dashboard about the metrics.
Prerequisite
- If you want to have an idea about the definition of telemetry, please have a look at the introduction of my previous article.
- Visual Studio 2022
- Prometheus Nuget packages
- Prometheus, you can download it here Download | Prometheus (Version : 2.51.0 / 2024-03-18 for this aricle)
- Grafana, you can download it here: Download Grafana | Grafana Labs (Version: Grafana-enterprise-10.4.1 for this article)
Prometheus
Prometheus is an open-source systems monitoring and alerting toolkit [...] it collects and stores its metrics as time series data, i.e. metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels. source: Prometheus website
Any application that wants to raise monitoring data in a way that Prometheus understands it, should integrate the Prometheus exporter and expose its metrics through it.
In our case, we will deploy the Prometheus server locally and integrate the nugget packages in the WebApi.
Grafana
Grafana open-source software enables you to query, visualize, alert on, and explore your metrics, logs, and traces wherever they are stored. Grafana OSS provides you with tools to turn your time-series database (TSDB) data into insightful graphs and visualizations. Source : grafana website
In our case, we will use it to visualize the metrics exported by the Web API into Prometheus.
Remarks
- Since we are focusing on the metrics management in this article, we will proceed in HTTP mode to avoid having certificate issues during the Prometheus connection So create your web API in HTTP mode
- The interval time of Prometheus scrape is configured in the YAML configuration file which we will see further in this article. So if you perform a call in the Api, wait a while before checking the Prometheus side)
Step 1. Create a webApi netcore project
Create a new Web API net core project and follow the instructions. You can call it TelemetryUsingPrometheus.

Step 2. Add NuGet Packages
Double-click on your TelemetryUsingPrometheus.csproj then add the following packages under the section ItemGroup and they will be installed automatically from Nuget when you build the solution.
<PackageReference Include="prometheus-net" Version="8.2.1" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
Step 3. Initialize the Prometheus exporter
Once you added the needed packages, you are able to use them in your code.
In your program.cs, you need to indicate that the solution will use the metrics.
//These metrics are called to use prometheus
app.UseMetricServer();
app.UseHttpMetrics();
Step 4. Set the metrics in your code
Prometheus offers several objects of metrics. You can view your metrics in the form of a histogram, gauge, counter, or summary.
So, you need to declare them and then perform the needed updates related to your performance data on every object.
In our case, we will set them from the API call just to have more dynamic interaction. But in real life, you should update the metrics objects from data related to the execution of your code.
using Microsoft.AspNetCore.Mvc;
using Prometheus;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace TelemetryUsingPrometheus.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TestTelemetryController : ControllerBase
{
Counter _counter;
Histogram _histogram;
Gauge _gauge;
Summary _summary;
public TestTelemetryController()
{
_counter = Metrics.CreateCounter("TestMetricCounter", "will be incremented and published as metrics");
_histogram = Metrics.CreateHistogram("TestMetricHistogram", "Will observe a value and publish it as Histogram");
_gauge = Metrics.CreateGauge("TestMetricGauge", "Will observe a value and publish it as Gauge");
_summary = Metrics.CreateSummary("TestMetricSummary", "Will observe a value and publish it as Summary");
}
[HttpPost("IncrementCounter")]
public void PostCounter(int inc)
{
_counter.Inc(inc);
_counter.Publish();
}
[HttpPost("IncrementHistogram")]
public void PostHistogram(int inc)
{
_histogram.Observe(inc);
_histogram.Publish();
}
[HttpPost("IncrementGauge")]
public void IncrementGauge(int inc)
{
_gauge.Inc(inc);
_gauge.Publish();
}
[HttpPost("DecrementGauge")]
public void DecrementGauge(int inc)
{
_gauge.Dec(inc);
_gauge.Publish();
}
[HttpPost("IncrementSummary")]
public void PostSummary(int inc)
{
_summary.Observe(inc);
_summary.Publish();
}
}
}
Now you can run your API and then type after your port number /metrics. You can see some generated logs by the Prometheus exporter that you have in your solution.

Step 5. Download and configure Prometheus
Once you download Prometheus, you'll have to the following folder.

Open the file prometheus .yml then add the section related to your service. By default, you'll find the Prometheus job.

















Join the conversation! Your thoughts help the community grow.