TRACING AND MONITORING

Track API usage with Prometheus and Grafana in ASP.Net core

A brief guide on how Prometheus and Grafana can be used for monitoring the API usage in ASP.NET core web application

Nitesh Singhal
4 min readDec 20, 2021

--

Image by Nitesh Singhal

There can be many scenario where you want to trace API usage for different purposes.

  • you want to charge your customer based on no of API calls.
  • you want to know popularity of certain APIs.
  • and many others.

In this tutorial, we are going to look at how we can use Prometheus to track the API usage.

So let’s get started.

Setting up Prometheus and Grafana

I will be making use of Prometheus and Grafana docker version. Below you can find docker-compose file to setup Prometheus and Grafana.

Promethues & Grafana docker-compose by Nitesh Singhal

Note: Be sure to create grafana-data and prom directory in current directory

create a file inside ‘prom’ directory “prometheus.yml” and add the configuration.

Open cmd and run the command to create the containers.

docker-compose up -d
Docker UI

So now both containers are running , we can verify by visiting the respective urls.

For Prometheus : http://localhost:30090

For Grafana : http://localhost:30091

Our basic setup for Prometheus and grafana is ready. we comeback to them once we write our API.

Sample API

Now we implement simple API of which we want to track the usage.

dotnet new webapi -o MyDemoAPI

open it visual studio or VS code.

change the applicationUrl in launchSettings.json

“applicationUrl”: “http://localhost:5000",

build the API and ran it once to confirm if it is running on port 5000.

Add the required nuget reference.

Install-Package prometheus-net.AspNetCore

and add the required code in Program.cs

Program.cs changes

we are ready to run.

let’s build the API and ran it.

MyDemoAPI

Let’s also verify if Prometheus is able to detect it.

go to http://localhost:30090/targets

Prometheus UI

as we can see it is up.

Now call the API from swagger or any other tool.

View Prometheus metrics

we can Prometheus metric at http://localhost:5000/metrics

http://localhost:5000/metrics

and in the marked area we can see the http_requests_received_total which indicates the no of request received and we can also see which API wise counter.

we can also verify same via Prometheus UI.

go to http://localhost:30090/graph and select graph tab.

Enter http_requests_received_total as query in search box and hit

Prometheus UI
Prometheus UI

we can see in the graph the no of API calls.

Till now we have seen this graphs in Prometheus.

Setup Grafana dashboard

Now move to Grafana http://localhost:30091

First add the data source as Prometheus with correct URL.

and then Add new dashboard and add new panel and add the query

Grafana UI

Save the dashboard, Give it name you want and save it.

Grafana UI

and finally you can visualize the API usage in the Grafana dashboard.

Source code available here

Summary

Tracking API Usage helps businesses track and reconcile product usage metrics to make it easier to measure product performance, integrate usage information in lead scoring, and perform billing against contracts.

In this tutorial, we have looked at how we can use Prometheus to collect the API metrics and use Grafana for nice visualization.

Hope you enjoy reading..!

Additional resources.

--

--

Nitesh Singhal

Software architect, Exploring ASP.Net core and containerization technologies