Skip to content

Provides an interceptor that can be used to track [protobuf-net.Grpc.AspNetCore] endpoint calls using [App.Metrics.AspNetCore.Tracking] middleware components

License

Notifications You must be signed in to change notification settings

Kurounin/AppMetrics.Grpc.AspNetCore

Repository files navigation

AppMetrics.Grpc.AspNetCore

Build status NuGet License

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.

Installation

Add the package to your application using

dotnet add package AppMetrics.Grpc.AspNetCore

Usage with Minimal APIs

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();

Usage without Minimal APIs

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>();
            });
    }
}

Usage of standalone MetricsServer

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;
    });

Configuration

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.

Grafana

The following dashboard can be used to view the exported metrics in Grafana: https://grafana.com/grafana/dashboards/15840

License

The source code and documentation in this project are released under the MIT License.

About

Provides an interceptor that can be used to track [protobuf-net.Grpc.AspNetCore] endpoint calls using [App.Metrics.AspNetCore.Tracking] middleware components

Resources

License

Stars

Watchers

Forks

Languages