Skip to content

Commit ad08228

Browse files
Follow up from #4627 (#4645)
1 parent b6b87d6 commit ad08228

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,19 @@ internal class HttpInListener : ListenerHandler
6363
#endif
6464
private readonly PropertyFetcher<Exception> stopExceptionFetcher = new("Exception");
6565
private readonly AspNetCoreInstrumentationOptions options;
66+
private readonly bool emitOldAttributes;
67+
private readonly bool emitNewAttributes;
6668

6769
public HttpInListener(AspNetCoreInstrumentationOptions options)
6870
: base(DiagnosticSourceName)
6971
{
7072
Guard.ThrowIfNull(options);
7173

7274
this.options = options;
75+
76+
this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old);
77+
78+
this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New);
7379
}
7480

7581
public override void OnEventWritten(string name, object payload)
@@ -195,7 +201,7 @@ public void OnStartActivity(Activity activity, object payload)
195201
activity.DisplayName = path;
196202

197203
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md
198-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
204+
if (this.emitOldAttributes)
199205
{
200206
if (request.Host.HasValue)
201207
{
@@ -224,7 +230,7 @@ public void OnStartActivity(Activity activity, object payload)
224230
}
225231

226232
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md
227-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
233+
if (this.emitNewAttributes)
228234
{
229235
if (request.Host.HasValue)
230236
{
@@ -281,12 +287,12 @@ public void OnStopActivity(Activity activity, object payload)
281287

282288
var response = context.Response;
283289

284-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
290+
if (this.emitOldAttributes)
285291
{
286292
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
287293
}
288294

289-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
295+
if (this.emitNewAttributes)
290296
{
291297
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
292298
}
@@ -485,12 +491,12 @@ private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext
485491
activity.SetTag(SemanticConventions.AttributeNetPeerIp, context.Connection.RemoteIpAddress.ToString());
486492
}
487493

488-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
494+
if (this.emitOldAttributes)
489495
{
490496
activity.SetTag(SemanticConventions.AttributeNetPeerPort, context.Connection.RemotePort);
491497
}
492498

493-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
499+
if (this.emitNewAttributes)
494500
{
495501
activity.SetTag(SemanticConventions.AttributeServerPort, context.Connection.RemotePort);
496502
}

src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInMetricsListener.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ internal sealed class HttpInMetricsListener : ListenerHandler
3434
private readonly Meter meter;
3535
private readonly AspNetCoreMetricsInstrumentationOptions options;
3636
private readonly Histogram<double> httpServerDuration;
37+
private readonly bool emitOldAttributes;
38+
private readonly bool emitNewAttributes;
3739

3840
internal HttpInMetricsListener(string name, Meter meter, AspNetCoreMetricsInstrumentationOptions options)
3941
: base(name)
4042
{
4143
this.meter = meter;
4244
this.options = options;
4345
this.httpServerDuration = meter.CreateHistogram<double>(HttpServerDurationMetricName, "ms", "Measures the duration of inbound HTTP requests.");
46+
47+
this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old);
48+
49+
this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New);
4450
}
4551

4652
public override void OnEventWritten(string name, object payload)
@@ -79,7 +85,7 @@ public override void OnEventWritten(string name, object payload)
7985
TagList tags = default;
8086

8187
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md
82-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
88+
if (this.emitOldAttributes)
8389
{
8490
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol)));
8591
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, context.Request.Scheme));
@@ -98,7 +104,7 @@ public override void OnEventWritten(string name, object payload)
98104
}
99105

100106
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md
101-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
107+
if (this.emitNewAttributes)
102108
{
103109
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeNetworkProtocolVersion, HttpTagHelper.GetFlavorTagValueFromProtocol(context.Request.Protocol)));
104110
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeUrlScheme, context.Request.Scheme));

src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ internal sealed class HttpHandlerDiagnosticListener : ListenerHandler
4545
private readonly PropertyFetcher<Exception> stopExceptionFetcher = new("Exception");
4646
private readonly PropertyFetcher<TaskStatus> stopRequestStatusFetcher = new("RequestTaskStatus");
4747
private readonly HttpClientInstrumentationOptions options;
48+
private readonly bool emitOldAttributes;
49+
private readonly bool emitNewAttributes;
4850

4951
static HttpHandlerDiagnosticListener()
5052
{
@@ -62,6 +64,10 @@ public HttpHandlerDiagnosticListener(HttpClientInstrumentationOptions options)
6264
: base("HttpHandlerDiagnosticListener")
6365
{
6466
this.options = options;
67+
68+
this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old);
69+
70+
this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New);
6571
}
6672

6773
public override void OnEventWritten(string name, object payload)
@@ -159,7 +165,7 @@ public void OnStartActivity(Activity activity, object payload)
159165
}
160166

161167
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md
162-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
168+
if (this.emitOldAttributes)
163169
{
164170
activity.SetTag(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme);
165171
activity.SetTag(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
@@ -174,7 +180,7 @@ public void OnStartActivity(Activity activity, object payload)
174180
}
175181

176182
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md
177-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
183+
if (this.emitNewAttributes)
178184
{
179185
activity.SetTag(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme);
180186
activity.SetTag(SemanticConventions.AttributeHttpRequestMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
@@ -238,12 +244,12 @@ public void OnStopActivity(Activity activity, object payload)
238244

239245
if (this.stopResponseFetcher.TryFetch(payload, out HttpResponseMessage response) && response != null)
240246
{
241-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
247+
if (this.emitOldAttributes)
242248
{
243249
activity.SetTag(SemanticConventions.AttributeHttpStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
244250
}
245251

246-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
252+
if (this.emitNewAttributes)
247253
{
248254
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode));
249255
}

src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ internal sealed class HttpHandlerMetricsDiagnosticListener : ListenerHandler
3232
private readonly PropertyFetcher<HttpRequestMessage> stopRequestFetcher = new("Request");
3333
private readonly Histogram<double> httpClientDuration;
3434
private readonly HttpClientMetricInstrumentationOptions options;
35+
private readonly bool emitOldAttributes;
36+
private readonly bool emitNewAttributes;
3537

3638
public HttpHandlerMetricsDiagnosticListener(string name, Meter meter, HttpClientMetricInstrumentationOptions options)
3739
: base(name)
3840
{
3941
this.httpClientDuration = meter.CreateHistogram<double>("http.client.duration", "ms", "Measures the duration of outbound HTTP requests.");
4042
this.options = options;
43+
44+
this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old);
45+
46+
this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New);
4147
}
4248

4349
public override void OnEventWritten(string name, object payload)
@@ -55,7 +61,7 @@ public override void OnEventWritten(string name, object payload)
5561
TagList tags = default;
5662

5763
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/trace/semantic_conventions/http.md
58-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old))
64+
if (this.emitOldAttributes)
5965
{
6066
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)));
6167
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme));
@@ -74,7 +80,7 @@ public override void OnEventWritten(string name, object payload)
7480
}
7581

7682
// see the spec https://github.com/open-telemetry/opentelemetry-specification/blob/v1.21.0/specification/trace/semantic_conventions/http.md
77-
if (this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New))
83+
if (this.emitNewAttributes)
7884
{
7985
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeHttpRequestMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)));
8086
tags.Add(new KeyValuePair<string, object>(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme));

0 commit comments

Comments
 (0)