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:
- Add the App.Metrics.Formatters.Prometheus nuget package to your ASP.NET Core web application.
- 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
.