App Metrics 1.0 Fork me on GitHub
Show / Hide Table of Contents

ASP.NET Core Middleware Prometheus Formatting

If using Prometheus for persisting metrics, you'll find that Prometheus promotes a Pull rather than Push based model. We therefore need to have the /metrics endpoint return data formatted for Prometheus. Prometheus supports metric data as plain text or protobuf, App Metrics supports both of these formats whereby /metrics-text will return metrics in Prometheus plain text format and /metrics will return metrics in Prometheus protobuf format.

To add enable metric serialization supporting Prometheus:

  1. Add the App.Metrics.Formatters.Prometheus nuget package to your ASP.NET Core web application.
  2. Configure Protobuf & Plain Text Prometheus serialization in your Startup.cs
public class Startup
{
	public void ConfigureServices(IServiceCollection services)
	{
		services.AddMetrics()
			// .AddPrometheusProtobufSerialization() - Enables both plain text and protobof formats on the /metrics-text and /metrics endpoints respectively.
			.AddPrometheusPlainTextSerialization() // Enables plain text format on the /metrics-text endpoint.
			.AddPrometheusProtobufSerialization() // Enables protobuf format on the /metrics endpoint.
			.AddJsonHealthSerialization()
			.AddHealthChecks()
			.AddMetricsMiddleware();
	}

	public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
	{            
		app.UseMetrics();
	}
}
Note

Notice the above is still using the App.Metrics.Formatters.Json package for health check serialization through AddJsonHealthSerialization.

  • Edit this Doc
Back to top Copyright © 2017 Allan Hardy
Generated by DocFX