opentelemetry-dotnet
opentelemetry-dotnet copied to clipboard
Automatically counters and histograms for Activities
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 Activity
s.
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";
});
});
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.)
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
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 Activity
s 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 Activity
s 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.
@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?