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.Prometheus nuget package. App.Metrics.Prometheus
supports both Prometheus’s plain text and protobuf formats.
To use the Prometheus formatter, first install the nuget package:
nuget install App.Metrics.Prometheus
Then enable Prometheus formatting using the MetricsBuilder
:
To configure Prometheus plain-text formatting:
var metrics = new MetricsBuilder()
.OutputMetrics.AsPrometheusPlainText()
.Build();
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.
To expose metrics for Prometheus to scrape in an ASP.NET Core application:
First install the App.Metrics.AspNetCore.All nuget package:
nuget install App.Metrics.AspNetCore.All
Then configure the Host
:
public static class Program
{
public static IMetricsRoot Metrics { get; set; }
public static IHost BuildHost(string[] args)
{
Metrics = AppMetrics.CreateDefaultBuilder()
.OutputMetrics.AsPrometheusPlainText()
.OutputMetrics.AsPrometheusProtobuf()
.Build();
return Host.CreateDefaultBuilder(args)
.ConfigureMetrics(Metrics)
.UseMetrics(
options =>
{
options.EndpointOptions = endpointsOptions =>
{
endpointsOptions.MetricsTextEndpointOutputFormatter = Metrics.OutputMetricsFormatters.OfType<MetricsPrometheusTextOutputFormatter>().First();
endpointsOptions.MetricsEndpointOutputFormatter = Metrics.OutputMetricsFormatters.OfType<MetricsPrometheusProtobufOutputFormatter>().First();
};
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
.Build();
}
public static void Main(string[] args) { BuildHost(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.
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: