Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

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.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
grafana ,docker ,influxdb ,performance ,api ,.net ,tutorial ,monitoring ,web api

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}