DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Monitoring Apache Ignite Cluster With Grafana (Part 1)
  • Unified Observability Exporters: Metrics, Logs, and Tracing
  • Unified Observability: Metrics, Logs, and Tracing of App and Database Tiers in a Single Grafana Console
  • Docker + .NET APIs: Simplifying Deployment and Scaling

Trending

  • Implementing Explainable AI in CRM Using Stream Processing
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • AI Agents: A New Era for Integration Professionals
  • Securing the Future: Best Practices for Privacy and Data Governance in LLMOps
  1. DZone
  2. Data Engineering
  3. Databases
  4. Real-Time Performance Monitoring in .NET Core With Grafana, InfluxDB, and Docker

Real-Time Performance Monitoring in .NET Core With Grafana, InfluxDB, and Docker

Learn how to monitor the performance of your APIs in real time with the combination of Grafana and InfluxDB in Docker containers.

By 
Thiago Loureiro user avatar
Thiago Loureiro
·
Jun. 05, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
39.6K Views

Join the DZone community and get the full member experience.

Join For Free

APIs are everywhere, and at some point, we need to start monitoring our APIs in real time, and it is through this tutorial that I will demonstrate how to do this with .NET Core, InfluxDB, and Grafana (InfluxDB and Grafana will be used through Docker).

First of all, let's understand what App Metrics is (from the app-metrics.io website):

What is App Metrics? App Metrics is an open-source and cross-platform .NET library used to record metrics within an application. App Metrics can run on .NET Core or on the full .NET framework also supporting .NET 4.5.2.App Metrics abstracts away the underlying repository of your Metrics for example InfluxDB, Prometheus, Graphite, Elasticsearch etc, by sampling and aggregating in memory and providing extensibility points to flush metrics to a repository at a specified interval.App Metrics provides various metric types to measure things such as the rate of requests, counting the number of user logins over time, measure the time taken to execute a database query, measure the amount of free memory and so on. Metrics types supported are Apdex, Gauges, Counters, Meters, Histograms and Timers.App Metrics also provides a health checking system allowing you to monitor the health of your application through user defined checks.

So, let's create what we need to make this work!

Creating the API

Creating the API is very simple. Just create a new project in VisualStudio and select the .NET Core/ASP.NET Core Web Application option.

Installing Grafana

To do the Grafana setup through Docker is very simple — just run the following command line:

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

More information can be checked here on the Grafana website.

Installing InfluxDB

The procedure is the same as Grafana, just execute the following command line:

docker run -p 8086: 8086 -d -v influxdb: /var/lib/influxdb influxdb

As you can see, Grafana was on port 3000 and Influx on port 8086.

Configuring the API

Now we need to go to the API and configure it to write the metrics in InfluxDB, so we will modify some files and install Nuget packages.

Install-Package App.Metrics.Extensions.Mvc
Install-Package App.Metrics.Formatters.Json
Install-Package App.Metrics.Extensions.Reporting.InfluxDB

Modify your Startup.cs file as shown below:

public  void  ConfigureServices ( IServiceCollection  services )
{
    var  database  =  " appmetricsdemo " ;
    var  uri  =  new  Uri ( " http://127.0.0.1:8086 " );

    services . AddMetrics ( options  =>
        {
            options . WithGlobalTags (( globalTags , info ) =>
            {
                globalTags . Add ( " app " , info . EntryAssemblyName );
                globalTags . Add ( " env " , " stage " );
            });
        })
        . AddHealthChecks ()
        . AddReporting (
            factory  =>
            {
                factory . AddInfluxDb (
                    new  InfluxDBReporterSettings
                    {
                        InfluxDbSettings  =  new  InfluxDBSettings ( database , uri ),
                        ReportInterval  =  TimeSpan . FromSeconds ( 5 )
                    });
            })
        . AddMetricsMiddleware ( options  =>  options . IgnoredHttpStatusCodes  =  new [] { 404 });

    services . AddMvc ( options  =>  options . AddMetricsResourceFilter ());
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public  void  Configure ( IApplicationBuilder  app , IHostingEnvironment  env , ILoggerFactory  loggerFactory , IApplicationLifetime  lifetime )
{
    loggerFactory . AddConsole ( Configuration . GetSection ( " Logging " ));
    app . UseMetrics ();
    app . UseMetricsReporting ( lifetime );
    app . UseMvc ();
}

Note that we added lines relevant to the InfluxDB configuration such as:

var database = "appmetricsdemo";
var uri = new Uri ("http://127.0.0.1:8086");

So use the way you have set it up.

Configuring Grafana

Let's now access Grafana, http: // localhost: 3000, user and default password is admin/admin.

Now you need to configure a DataSource. In our case, it will be Influx; here is a sample configuration:

Note that the URL I entered is the IP of the Docker container where it is running InfluxDB, since Grafana is also running in Docker and both are running on the same network.

Click Save & Test and you should see this result:

Now let's import the dashboard for Grafana:

Here on this site, you can find the JSON file to import into your dashboard, but I will also leave the copy of what I used in the demo here.

Let's now run our application — feel free to add new Controllers.

Results

Endpoints:

Detail of Endpoint requests:

Grafana filter settings:

The idea of this article is to show how easy and powerful App Metrics, Grafana, InfluxDB and Docker are, and the important pieces to make the whole environment works.

The source code of this article can be found here.

Grafana InfluxDB Docker (software) .NET Metric (unit) Web application app Database Open source API

Opinions expressed by DZone contributors are their own.

Related

  • Monitoring Apache Ignite Cluster With Grafana (Part 1)
  • Unified Observability Exporters: Metrics, Logs, and Tracing
  • Unified Observability: Metrics, Logs, and Tracing of App and Database Tiers in a Single Grafana Console
  • Docker + .NET APIs: Simplifying Deployment and Scaling

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!