opentelemetry-dotnet icon indicating copy to clipboard operation
opentelemetry-dotnet copied to clipboard

Automatically counters and histograms for Activities

Open bastianeicher opened this issue 2 years ago • 4 comments

Feature Request

Oftentimes the same things I would like trace spans for (e.g., database requests) are the things I would also like counters and/or duration histograms for.

To keep the amount of required instrumentation code small, I think it would be great to have metrics automatically generated for Activitys.

Perhaps configured something like this:

services.AddOpenTelemetryMetrics(builder =>
{
    builder
        .AddActivityCounters("MyActivitySourceName",
            opts =>
            {
                opts.Filter = activity => !activity.OperationName.StartsWith("abc");
                opts.Naming = activity => activity.OperationName.ToSnakeCase() + "_total";
            });
        .AddActivityDurationHistograms("MyActivitySourceName",
            opts =>
            {
                opts.Filter = activity => !activity.OperationName.StartsWith("abc");
                opts.Naming = activity => activity.OperationName.ToSnakeCase() + "_duration";
            });
});

bastianeicher avatar Jul 15 '22 12:07 bastianeicher

Metrics and Traces are independently enabled/disabled in OpenTelemetry.

One good option would be to derive metrics from traces in the collector. (Though this has accuracy/latency implications.)

cijothomas avatar Jul 15 '22 16:07 cijothomas

You could also consider writing your own activity processor for generating these metrics. Record the metric at Activity end

https://github.com/open-telemetry/opentelemetry-dotnet/blob/a19d106e0c40b60f5d0032999c315a8f0b8c5159/docs/trace/extending-the-sdk/MyProcessor.cs#L35-L38

alanwest avatar Jul 15 '22 21:07 alanwest

I don't really think that such a feature would conflict with the principle of enabling and disabling metrics and traces independently. Basically my suggestion would just be to consider Activitys as sources for both:

  • traces (already exists)
    services.AddOpenTelemetryTracing(builder => builder.AddSource("MyActivitySourceName"));
    
  • and metrics (new)
    services.AddOpenTelemetryMetrics(builder => builder.AddActivityDurationHistograms("MyActivitySourceName"));
    

AFAIK, the term "activity" doesn't have a specific meaning in the OpenTelemetry specification. So while Activitys can currently be mapped to tracing spans, I feel like it would be a nice addition to also be able to map them to metrics.

bastianeicher avatar Jul 16 '22 21:07 bastianeicher

@alanwest Thanks for the activity processor suggestion! Another option I wanted to look into is registering my own ActivityListener. Do you see some significant advantages to either one of these approaches?

bastianeicher avatar Jul 16 '22 21:07 bastianeicher