Home > Software engineering >  How to access dotnet core event-counters on code - publish as metrics
How to access dotnet core event-counters on code - publish as metrics

Time:12-21

I am going to publish some of the statistics provided by event-counter as metrics. There are many pre-defined ones in the framework as we can see here.

How I can access the information (that is available through dotnet-counters)?

CodePudding user response:

This is available through the OpenTelemetry packages. In my own applications I use the following to add OpenTelemetry metrics for the runtime and process metrics and publish them to Prometheus :

public static IServiceCollection AddOtelMetrics(
    this IServiceCollection services,
    string[]? names=null)
{
 services.AddOpenTelemetryMetrics(meters =>
 {
     meters.AddEventCountersInstrumentation(c =>
     {
         c.AddEventSources("rabbitmq-client");
     });

     meters.AddAspNetCoreInstrumentation();
     meters.AddHttpClientInstrumentation();
     meters.AddRuntimeInstrumentation();
     meters.AddProcessInstrumentation();

     if (names != null)
     {
         meters.AddMeter(names);
     }

     meters.AddPrometheusExporter();
 });

The AddAspNetCoreInstrumentation, AddRuntimeInstrumentation and AddProcessInstrumentation add the well known meters. Other meters can be added by passing their names to AddMeter. EventSources can also be added, with AddEventCountersInstrumentation and passing the EventSource name.

 meters.AddEventCountersInstrumentation(c =>
 {
     c.AddEventSources("rabbitmq-client");
 });
  • Related