Prometheus

Basics

Prometheus promotes a Pull based approach rather than Push, therefore App Metrics does not include a reporter to push metrics, but rather supports formatting metric data in Prometheus formats using the App.Metrics.Formatters.Prometheus nuget package. App.Metrics.Formatters.Prometheus supports both Prometheus’s plain text and protobuf formats.

To use the Prometheus formatter, first install the nuget package:

nuget install App.Metrics.Formatters.Prometheus

Then enable Prometheus formatting using the MetricsBuilder:

Plain text

To configure Prometheus plain-text formatting:

var metrics = new MetricsBuilder()
    .OutputMetrics.AsPrometheusPlainText()
    .Build();

Protobuf

To configure Prometheus protobuf formatting:

var metrics = new MetricsBuilder()
    .OutputMetrics.AsPrometheusProtobuf()
    .Build();

See the metrics formatting documentation on more details around outputting metrics using a specific formatter.

ASP.NET Core Configuration

To expose metrics for Prometheus to scrape in an ASP.NET Core application:

First install the App.Metrics.AspNetCore nuget package:

nuget install App.Metrics.AspNetCore

Then configure the WebHost:

public static class Program
{
    public static IWebHost BuildWebHost(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
                        .ConfigureMetricsWithDefaults(
                            builder =>
                            {
                                builder.OutputMetrics.AsPrometheusPlainText();
                                builder.OutputMetrics.AsPrometheusProtobuf();
                            })
                        .UseMetrics(
                            options =>
                            {
                                options.EndpointOptions = endpointsOptions =>
                                {
                                    endpointsOptions.MetricsTextEndpointOutputFormatter = new MetricsPrometheusTextOutputFormatter();
                                    endpointsOptions.MetricsEndpointOutputFormatter = new MetricsPrometheusProtobufOutputFormatter();
                                };
                            })
                        .UseStartup<Startup>()
                        .Build();
    }

    public static void Main(string[] args) { BuildWebHost(args).Run(); }
}

With the above configuration, /metrics-text will return metrics in Prometheus plain text format and /metrics in Prometheus protobuf format.

See the ASP.NET Core documentation for more details on integrating App Metrics in an ASP.NET Core application.

Web Monitoring

Grafana

The App.Metrics.AspNetCore.Tracking nuget package automatically records typical web metrics when added to an ASP.NET Core application. App Metrics includes pre-built Grafana dashboards to visualize these metrics:

  1. Configure the Prometheus scrape config
  2. Install the App Metrics Grafana dashboard.