diff --git a/src/OpenTelemetry.Instrumentation.AspNet/AspNetMetricsInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNet/AspNetMetricsInstrumentationOptions.cs index cc9259f6a4..63730bff36 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/AspNetMetricsInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet/AspNetMetricsInstrumentationOptions.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Web; namespace OpenTelemetry.Instrumentation.AspNet; @@ -12,17 +8,30 @@ public class AspNetMetricsInstrumentationOptions /// /// Delegate for enrichment of recorded metric with additional tags. /// + /// The name of the metric being enriched. /// : the HttpContext object. Both Request and Response are available. /// : List of current tags. You can add additional tags to this list. - public delegate void AspNetMetricEnrichmentFunc(string name, HttpContext context, ref TagList tags); + public delegate void AspNetMetricEnrichmentFunc(string metricName, HttpContext context, ref TagList tags); /// - /// Gets or sets a Filter function that determines whether or not to collect telemetry about requests on a per request basis. - /// The Filter gets the HttpContext, and should return a boolean. - /// If Filter returns true, the request is collected. - /// If Filter returns false or throw exception, the request is filtered out. + /// Gets or sets a filter function that determines whether or not to + /// collect telemetry on a per request basis. /// - public Func Filter { get; set; } + /// + /// Notes: + /// + /// The first parameter is the name of the metric being + /// filtered. + /// The return value for the filter function is interpreted as: + /// + /// If filter returns , the request is + /// collected. + /// If filter returns or throws an + /// exception the request is NOT collected. + /// + /// + /// + public Func Filter { get; set; } /// /// Gets or sets an function to enrich a recorded metric with additional custom tags. diff --git a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInMetricsListener.cs b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInMetricsListener.cs index 20fc0433a2..1935120617 100644 --- a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInMetricsListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/HttpInMetricsListener.cs @@ -25,13 +25,14 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation; internal sealed class HttpInMetricsListener : IDisposable { private readonly Histogram httpServerDuration; - private readonly AspNetMetricsInstrumentationOptions options; + private readonly AspNetMetricsInstrumentationOptions options; + private readonly string httpServerDurationMetricName = "http.server.duration"; private readonly string onStopActivityEventName = "OnStopActivity"; public HttpInMetricsListener(Meter meter, AspNetMetricsInstrumentationOptions options) { this.options = options; - this.httpServerDuration = meter.CreateHistogram("http.server.duration", "ms", "Measures the duration of inbound HTTP requests."); + this.httpServerDuration = meter.CreateHistogram(this.httpServerDurationMetricName, "ms", "Measures the duration of inbound HTTP requests."); TelemetryHttpModule.Options.OnRequestStoppedCallback += this.OnStopActivity; } @@ -44,7 +45,7 @@ private void OnStopActivity(Activity activity, HttpContext context) { try { - if (this.options.Filter?.Invoke(context) == false && Activity.Current != null) + if (this.options.Filter?.Invoke(this.httpServerDurationMetricName, context) == false && Activity.Current != null) { AspNetInstrumentationEventSource.Log.RequestIsFilteredOut(Activity.Current.OperationName); return; @@ -69,7 +70,7 @@ private void OnStopActivity(Activity activity, HttpContext context) { try { - this.options.Enrich(this.onStopActivityEventName, context, ref tags); + this.options.Enrich(this.httpServerDurationMetricName, context, ref tags); } catch (Exception ex) {