Provides an interceptor that can be used to track Grpc.AspNetCore and protobuf-net.Grpc.AspNetCore endpoint calls using App.Metrics.AspNetCore.Tracking middleware components.
A standalone MetricsServer
is provided to help expose the metrics on a separate port.
Add the package to your application using
dotnet add package AppMetrics.Grpc.AspNetCore
Add grpc metrics before http metrics:
using AppMetrics.Grpc.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
var metrics = App.Metrics.AppMetrics.CreateDefaultBuilder()
.OutputMetrics.AsPrometheusPlainText()
.Build();
builder.WebHost
.ConfigureMetrics(metrics)
.UseGrpcMetrics()
.UseMetrics();
Add grpc metrics before http metrics:
using AppMetrics.Grpc.AspNetCore;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) {
var metrics = App.Metrics.AppMetrics.CreateDefaultBuilder()
.OutputMetrics.AsPrometheusPlainText()
.Build();
return Host.CreateDefaultBuilder(args)
.ConfigureMetrics(metrics)
.UseGrpcMetrics()
.UseMetrics()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
To expose metrics on a different port using the MetricsServer
call AddMetricsServer
either on a IHostBuilder
or a IWebHostBuilder
and pass the same IMetricsRoot
instance used in the main host:
builder.WebHost
.ConfigureMetrics(metrics)
.UseGrpcMetrics()
.UseMetrics()
.AddMetricsServer(metrics, endpointsOptions =>
{
endpointsOptions.MetricsEndpointOutputFormatter = metrics.OutputMetricsFormatters.OfType<MetricsJsonOutputFormatter>().First();
endpointsOptions.MetricsTextEndpointOutputFormatter = metrics.OutputMetricsFormatters.OfType<MetricsPrometheusTextOutputFormatter>().First();
endpointsOptions.EnvironmentInfoEndpointEnabled = false;
});
Optionally add the following configuration to your appsettings.json
"MetricsServerOptions": {
"EnvironmentInfoEndpoint": "/env",
"MetricsEndpoint": "/metrics",
"MetricsTextEndpoint": "/metrics-text",
"Port": 5501
}
By default the MetricsServer
will listen on port 5501 on all interfaces.
The following dashboard can be used to view the exported metrics in Grafana: https://grafana.com/grafana/dashboards/15840
The source code and documentation in this project are released under the MIT License.