diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index bbf749d5414..01ae418524d 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -63,6 +63,8 @@ internal class HttpInListener : ListenerHandler #endif private readonly PropertyFetcher stopExceptionFetcher = new("Exception"); private readonly AspNetCoreInstrumentationOptions options; + private readonly bool emitOldAttributes; + private readonly bool emitNewAttributes; public HttpInListener(AspNetCoreInstrumentationOptions options) : base(DiagnosticSourceName) @@ -70,6 +72,10 @@ public HttpInListener(AspNetCoreInstrumentationOptions options) Guard.ThrowIfNull(options); this.options = options; + + this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old); + + this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New); } public override void OnEventWritten(string name, object payload) @@ -195,7 +201,7 @@ public void OnStartActivity(Activity activity, object payload) activity.DisplayName = path; // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old)) + if (this.emitOldAttributes) { if (request.Host.HasValue) { @@ -224,7 +230,7 @@ public void OnStartActivity(Activity activity, object payload) } // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New)) + if (this.emitNewAttributes) { if (request.Host.HasValue) { @@ -281,12 +287,12 @@ public void OnStopActivity(Activity activity, object payload) var response = context.Response; - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old)) + if (this.emitOldAttributes) { activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)); } - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New)) + if (this.emitNewAttributes) { activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)); } @@ -485,12 +491,12 @@ private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext activity.SetTag(SemanticConventions.AttributeNetPeerIp, context.Connection.RemoteIpAddress.ToString()); } - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old)) + if (this.emitOldAttributes) { activity.SetTag(SemanticConventions.AttributeNetPeerPort, context.Connection.RemotePort); } - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New)) + if (this.emitNewAttributes) { activity.SetTag(SemanticConventions.AttributeServerPort, context.Connection.RemotePort); } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs index 981d4bc26b0..27d7ef6bb67 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs @@ -34,6 +34,8 @@ internal sealed class HttpInMetricsListener : ListenerHandler private readonly Meter meter; private readonly AspNetCoreMetricsInstrumentationOptions options; private readonly Histogram httpServerDuration; + private readonly bool emitOldAttributes; + private readonly bool emitNewAttributes; internal HttpInMetricsListener(string name, Meter meter, AspNetCoreMetricsInstrumentationOptions options) : base(name) @@ -41,6 +43,10 @@ internal HttpInMetricsListener(string name, Meter meter, AspNetCoreMetricsInstru this.meter = meter; this.options = options; this.httpServerDuration = meter.CreateHistogram(HttpServerDurationMetricName, "ms", "Measures the duration of inbound HTTP requests."); + + this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old); + + this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New); } public override void OnEventWritten(string name, object payload) @@ -79,7 +85,7 @@ public override void OnEventWritten(string name, object payload) TagList tags = default; // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old)) + if (this.emitOldAttributes) { tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol))); tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpScheme, context.Request.Scheme)); @@ -98,7 +104,7 @@ public override void OnEventWritten(string name, object payload) } // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New)) + if (this.emitNewAttributes) { tags.Add(new KeyValuePair(SemanticConventions.AttributeNetworkProtocolVersion, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol))); tags.Add(new KeyValuePair(SemanticConventions.AttributeUrlScheme, context.Request.Scheme)); diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs index 6248407b7a5..b7d2458d23b 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs @@ -45,6 +45,8 @@ internal sealed class HttpHandlerDiagnosticListener : ListenerHandler private readonly PropertyFetcher stopExceptionFetcher = new("Exception"); private readonly PropertyFetcher stopRequestStatusFetcher = new("RequestTaskStatus"); private readonly HttpClientInstrumentationOptions options; + private readonly bool emitOldAttributes; + private readonly bool emitNewAttributes; static HttpHandlerDiagnosticListener() { @@ -62,6 +64,10 @@ public HttpHandlerDiagnosticListener(HttpClientInstrumentationOptions options) : base("HttpHandlerDiagnosticListener") { this.options = options; + + this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old); + + this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New); } public override void OnEventWritten(string name, object payload) @@ -159,7 +165,7 @@ public void OnStartActivity(Activity activity, object payload) } // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old)) + if (this.emitOldAttributes) { activity.SetTag(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme); activity.SetTag(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)); @@ -174,7 +180,7 @@ public void OnStartActivity(Activity activity, object payload) } // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New)) + if (this.emitNewAttributes) { activity.SetTag(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme); activity.SetTag(SemanticConventions.AttributeHttpRequestMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)); @@ -238,12 +244,12 @@ public void OnStopActivity(Activity activity, object payload) if (this.stopResponseFetcher.TryFetch(payload, out HttpResponseMessage response) && response != null) { - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old)) + if (this.emitOldAttributes) { activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)); } - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New)) + if (this.emitNewAttributes) { activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)); } diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs index 55d2e1edf74..6fe05338b47 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs @@ -32,12 +32,18 @@ internal sealed class HttpHandlerMetricsDiagnosticListener : ListenerHandler private readonly PropertyFetcher stopRequestFetcher = new("Request"); private readonly Histogram httpClientDuration; private readonly HttpClientMetricInstrumentationOptions options; + private readonly bool emitOldAttributes; + private readonly bool emitNewAttributes; public HttpHandlerMetricsDiagnosticListener(string name, Meter meter, HttpClientMetricInstrumentationOptions options) : base(name) { this.httpClientDuration = meter.CreateHistogram("http.client.duration", "ms", "Measures the duration of outbound HTTP requests."); this.options = options; + + this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old); + + this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New); } public override void OnEventWritten(string name, object payload) @@ -55,7 +61,7 @@ public override void OnEventWritten(string name, object payload) TagList tags = default; // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old)) + if (this.emitOldAttributes) { tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method))); tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme)); @@ -74,7 +80,7 @@ public override void OnEventWritten(string name, object payload) } // see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md - if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New)) + if (this.emitNewAttributes) { tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpRequestMethod, HttpTagHelper.GetNameForHttpMethod(request.Method))); tags.Add(new KeyValuePair(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme));