.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 quick look into Elsa Workflows

3 APIs To Get Flight Data Of Air Busan Airlines

How to run a Flutter Project from Github to your system in VsCode and install the Flutter App in…

When do you use an automation framework and when do you use a testing tool?

Get Youtube Channel Link From A Domain Using An API

Four Questions For Engineers

A primer on *args, **kwargs, decorators for Data Scientists

Core Web Vitals FID Case Study

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

CI/CD with Gitlab-CI

Azure DevOps Server Architectures

Using GitLab CI/CD pipeline to build WebAPI and deploy to Azure Appservice-Part1

Gitlab SSH Config Host File, Allowing Multiple Account SSH Access