Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Grafana and Prometheus Integration in ASP.NET Core: Metrics and Dashboard

Grafana and Prometheus Integration in ASP.NET Core: Metrics and Dashboard

Date- May 14,2026 310
grafana prometheus

Overview

Grafana and Prometheus are powerful tools in the realm of monitoring and observability. Prometheus is an open-source systems monitoring and alerting toolkit that collects metrics from configured targets at specified intervals. It stores these metrics in a time-series database, allowing for efficient querying and analysis. Grafana, on the other hand, is a multi-platform open-source analytics and monitoring solution that integrates seamlessly with Prometheus to visualize metrics in a user-friendly dashboard format. This integration addresses the challenge of gaining insights from raw data, transforming it into actionable information.

In real-world scenarios, this integration is invaluable. For instance, a microservices architecture can generate a vast amount of metrics, making it difficult to monitor performance and health effectively. By utilizing Prometheus to collect these metrics and Grafana to visualize them, developers and operations teams can quickly identify bottlenecks, track performance trends, and maintain system reliability. Use cases include monitoring web application performance, tracking resource utilization, and analyzing user behavior.

Prerequisites

  • ASP.NET Core knowledge: Familiarity with building web applications using ASP.NET Core is essential.
  • Docker: Basic understanding of Docker containers for running Prometheus and Grafana.
  • Prometheus: Awareness of how Prometheus works and its data model.
  • Grafana: Understanding of Grafana’s dashboard capabilities and data source configuration.
  • NuGet Packages: Familiarity with adding and managing NuGet packages in ASP.NET Core.

Setting Up Prometheus

To begin with, we need to set up Prometheus, which will scrape metrics from our ASP.NET Core application. Prometheus operates on a pull model, meaning it periodically requests metrics from configured endpoints. This requires configuring a Prometheus.yml file, specifying the target ASP.NET Core application and the metrics endpoint.

# prometheus.yml

global:
  scrape_interval: 15s  # Default scrape interval

scrape_configs:
  - job_name: 'aspnetcore'
    static_configs:
      - targets: ['host.docker.internal:5000']  # Adjust the host and port

This configuration sets a global scrape interval of 15 seconds and defines a job named 'aspnetcore' that targets our ASP.NET Core application running on port 5000. The use of host.docker.internal allows Docker containers to access the host machine.

Running Prometheus in Docker

We can easily run Prometheus using Docker by pulling the official Prometheus image and running it with our configuration file:

docker run -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml --name prometheus prom/prometheus

This command does the following:

  • -d: Runs the container in detached mode.
  • -p 9090:9090: Maps port 9090 of the container to port 9090 of the host.
  • -v: Mounts the local prometheus.yml file into the container.
  • --name: Names the container 'prometheus'.
  • prom/prometheus: Specifies the Prometheus image to use.

After executing this command, you can access the Prometheus UI by navigating to http://localhost:9090.

Setting Up ASP.NET Core Application for Metrics

Next, we will configure our ASP.NET Core application to expose metrics for Prometheus. We achieve this by using the prometheus-net.AspNetCore NuGet package, which simplifies the integration.

// Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddPrometheus();  // Add this line to configure Prometheus metrics
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapMetrics();  // Expose metrics endpoint
    });
}

In this code snippet:

  • services.AddPrometheus(); Adds the necessary services for Prometheus metrics.
  • endpoints.MapMetrics(); Exposes the metrics endpoint at /metrics.

With this setup, Prometheus will scrape metrics from http://localhost:5000/metrics.

Custom Metrics in ASP.NET Core

In addition to the default metrics provided by the framework, you can create custom metrics tailored to your application. For example, you might want to track the number of requests processed:

public class MyController : ControllerBase
{
    private static readonly Counter RequestCounter = Metrics.CreateCounter("http_requests_total", "Total number of HTTP requests.");

    [HttpGet]
    public IActionResult Get()
    {
        RequestCounter.Inc();  // Increment the counter for each request
        return Ok("Hello, World!");
    }
}

In this controller:

  • Metrics.CreateCounter: Creates a counter metric named http_requests_total.
  • RequestCounter.Inc(); Increments the counter each time the Get action is called.

Prometheus will now scrape this custom metric alongside the default metrics.

Setting Up Grafana

With Prometheus collecting metrics, the next step is to visualize these metrics using Grafana. First, we need to run Grafana using Docker:

docker run -d -p 3000:3000 --name=grafana grafana/grafana

This command runs Grafana in a container accessible at http://localhost:3000. The default login is admin/admin. After logging in, we need to add Prometheus as a data source.

Configuring Grafana Data Source

To add Prometheus as a data source in Grafana:

  1. Navigate to Configuration > Data Sources.
  2. Click on Add data source.
  3. Select Prometheus from the list.
  4. Set the URL to http://host.docker.internal:9090 (or the appropriate address if using a different setup).
  5. Click Save & Test to validate the connection.

This configuration allows Grafana to query metrics collected by Prometheus.

Creating Grafana Dashboards

Once the data source is configured, you can create dashboards to visualize your application metrics. Create a new dashboard by selecting Create > Dashboard in Grafana.

Add a panel and select the Prometheus data source. You can then use PromQL (Prometheus Query Language) to query your metrics. For instance, to visualize the total number of HTTP requests, use the query:

http_requests_total

This query will display the total count of requests received by your ASP.NET Core application over time.

Edge Cases & Gotchas

While integrating Grafana and Prometheus with ASP.NET Core, several common pitfalls can arise:

Incorrect Metrics Endpoint Configuration

Ensure that the metrics endpoint is correctly exposed. If Prometheus cannot access /metrics, no metrics will be collected. Verify that your ASP.NET Core application is running and reachable.

// Incorrect example: missing endpoint mapping
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();  // Missing MapMetrics()
    });
}

In this incorrect example, the metrics endpoint is not exposed, leading to an empty metrics collection.

Prometheus Scraping Issues

Check the Prometheus logs if you encounter scraping errors. Common issues include incorrect target configuration or network connectivity problems. Ensure the target is reachable and that firewalls are not blocking access.

Performance & Best Practices

To optimize the performance of your Grafana and Prometheus setup:

Sampling Frequency

Adjust the scrape_interval in the Prometheus configuration according to your application needs. Frequent scraping can lead to increased load on your application and Prometheus server. A common best practice is to set it to 15-30 seconds for production environments.

Reduce Metric Cardinality

High cardinality metrics (metrics with many unique label values) can lead to performance degradation in Prometheus. Avoid using labels with high variability, such as user IDs or session IDs, and instead use more stable identifiers.

Use Aggregation

Utilize Prometheus’s aggregation functions in your queries to reduce the amount of data sent to Grafana. For example, use sum and avg to consolidate metrics over time.

Real-World Scenario: Monitoring an ASP.NET Core API

Let’s tie all these concepts together in a mini-project where we create a simple ASP.NET Core API that exposes metrics, which are then visualized in Grafana.

// Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup();
        });
}

// Startup.cs
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddPrometheus();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapMetrics();
        });
    }
}

// MyController.cs
[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
    private static readonly Counter RequestCounter = Metrics.CreateCounter("http_requests_total", "Total number of HTTP requests.");

    [HttpGet]
    public IActionResult Get()
    {
        RequestCounter.Inc();
        return Ok("Hello, World!");
    }
}

This minimal example demonstrates an ASP.NET Core API with a metrics endpoint. Running this application alongside Prometheus and Grafana allows you to visualize the total HTTP requests received.

Conclusion

  • Grafana and Prometheus are essential tools for monitoring and visualizing application metrics.
  • ASP.NET Core integration allows you to expose metrics for collection and analysis.
  • Custom metrics can provide insights specific to your application’s performance.
  • Best practices include managing scrape frequency, reducing metric cardinality, and using aggregation in PromQL.
  • Real-world scenarios demonstrate practical applications of these integrations in production environments.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Integrating Datadog APM for Distributed Tracing in ASP.NET Core Applications
May 14, 2026
Implementing IP Whitelisting and Blacklisting Middleware in ASP.NET Core
Jun 10, 2026
Implementing API Key Authentication Middleware in ASP.NET Core Web API
Jun 10, 2026
CWE-1021: Preventing Clickjacking in ASP.NET Core with X-Frame-Options and CSP Headers
Jun 06, 2026
Previous in ASP.NET Core
Integrating New Relic for Comprehensive Performance Monitoring in…
Next in ASP.NET Core
Integrating Seq Log Server with ASP.NET Core for Centralized Stru…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 328 views
  • 2
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,928 views
  • 3
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 240 views
  • 4
    Error-An error occurred while processing your request in .… 11,953 views
  • 5
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 817 views
  • 6
    Send Email With HTML Template And PDF Using ASP.Net C# 17,171 views
  • 7
    Integrating Google reCAPTCHA Validation in ASP.NET MVC 6,457 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26677 views
  • Exception Handling Asp.Net Core 21714 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21168 views
  • How to implement Paypal in Asp.Net Core 20126 views
  • Task Scheduler in Asp.Net core 18195 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor