.NET Core Web API Metrics with Prometheus and Grafana

Example API Controller Grafana/Prometheus dashboard with prometheus-net.

Introduction

Prerequisites for this Project

Setting Up Your Project

dotnet new webapi -n peopleapi
dotnet new webapi -n locationsapi
dotnet add package prometheus-net.AspNetCore
dotnet restore
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Custom Metrics to count requests for each endpoint and the method
var counter = Metrics.CreateCounter("peopleapi_path_counter", "Counts requests to the People API endpoints", new CounterConfiguration
{
LabelNames = new[] { "method", "endpoint" }
});
app.Use((context, next) =>
{
counter.WithLabels(context.Request.Method, context.Request.Path).Inc();
return next();
});
// Use the Prometheus middleware
app.UseMetricServer();
app.UseHttpMetrics();
...
}

Containerize It All

cd peopleapi
docker build -f Dockerfile -t peopleapi .
cd ../locationsapi
docker build -f Dockerfile -t locationsapi .
cd ..
docker images | more
docker-compose up

Using Straight Prometheus

Example Prometheus screen for .NET Core metrics

Setting Up Grafana Dashboards

Prometheus data source for Grafana
Your new Grafana dashboard showing .NET Core default metrics
Your new Grafana dashboard showing .NET Web API Controller metrics

Conclusion

docker-compose down

Useful Links

--

--

--

CEO of Soteria Software. Developer on OpenRMF. Software Geek by trade. Father of three daughters. Husband. Love new tech where it fits. Follow at @soteriasoft

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A webserver running WordPress and database server, running on AWS’ VPC’s public and private subnets.

Bridge the gap between the remote and local system

ASP.NET Core Project Admin (Crud)

Upload base64 Image using Amazon S3 without storing the image on the local directory with Laravel

The MIT License: in Plain English

Don’t have a Constants file.

Creating Web Integration Tests for Spring Boot REST Controller

How to Create a Virtual Network in Azure

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dale Bingham

Dale Bingham

CEO of Soteria Software. Developer on OpenRMF. Software Geek by trade. Father of three daughters. Husband. Love new tech where it fits. Follow at @soteriasoft

More from Medium

Configure ELK stack in .NET Core 6

Azure DevOps with Dockerized Jenkins, NGINX Proxy Manager and Cloudflare Part #1…

1:1 with docker

How to install Chocolatey in windows- DevOps Champ