open-telemetry / opentelemetry-dotnet

The OpenTelemetry .NET Client
https://opentelemetry.io
Apache License 2.0
3.18k stars 753 forks source link

Automatically counters and histograms for Activities #3450

Open bastianeicher opened 2 years ago

bastianeicher commented 2 years ago

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";
            });
});
cijothomas commented 2 years ago

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.)

alanwest commented 2 years ago

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

bastianeicher commented 2 years ago

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:

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 commented 2 years ago

@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?

github-actions[bot] commented 4 days ago

This issue was marked stale due to lack of activity and will be closed in 7 days. Commenting will instruct the bot to automatically remove the label. This bot runs once per day.