Skip to content

Commit 2fce583

Browse files
[HttpClient] Remove http.host and add net.peer.name & net.peer.port to match spec (#3832)
1 parent a892796 commit 2fce583

File tree

8 files changed

+77
-68
lines changed

8 files changed

+77
-68
lines changed

src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
* **Breaking change** `http.host` will no longer be populated. `net.peer.name`
6+
and `net.peer.port` attributes will be populated instead.
7+
([#3832](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3832))
8+
59
## 1.0.0-rc9.9
610

711
Released 2022-Nov-07

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ public void OnStartActivity(Activity activity, object payload)
159159

160160
activity.SetTag(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme);
161161
activity.SetTag(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
162-
activity.SetTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
162+
activity.SetTag(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host);
163+
if (!request.RequestUri.IsDefaultPort)
164+
{
165+
activity.SetTag(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port);
166+
}
167+
163168
activity.SetTag(SemanticConventions.AttributeHttpUrl, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri));
164169
activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version));
165170

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ internal static class HttpTagHelper
2727
private static readonly ConcurrentDictionary<string, string> MethodOperationNameCache = new();
2828
private static readonly ConcurrentDictionary<HttpMethod, string> HttpMethodOperationNameCache = new();
2929
private static readonly ConcurrentDictionary<HttpMethod, string> HttpMethodNameCache = new();
30-
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<int, string>> HostAndPortToStringCache = new();
3130
private static readonly ConcurrentDictionary<Version, string> ProtocolVersionToStringCache = new();
3231

3332
private static readonly Func<string, string> ConvertMethodToOperationNameRef = ConvertMethodToOperationName;
@@ -63,37 +62,6 @@ internal static class HttpTagHelper
6362
/// <returns>Span flavor value.</returns>
6463
public static string GetFlavorTagValueFromProtocolVersion(Version protocolVersion) => ProtocolVersionToStringCache.GetOrAdd(protocolVersion, ConvertProtocolVersionToStringRef);
6564

66-
/// <summary>
67-
/// Gets the OpenTelemetry standard host tag value for a span based on its request <see cref="Uri"/>.
68-
/// </summary>
69-
/// <param name="requestUri"><see cref="Uri"/>.</param>
70-
/// <returns>Span host value.</returns>
71-
public static string GetHostTagValueFromRequestUri(Uri requestUri)
72-
{
73-
string host = requestUri.Host;
74-
75-
if (requestUri.IsDefaultPort)
76-
{
77-
return host;
78-
}
79-
80-
int port = requestUri.Port;
81-
82-
if (!HostAndPortToStringCache.TryGetValue(host, out ConcurrentDictionary<int, string> portCache))
83-
{
84-
portCache = new ConcurrentDictionary<int, string>();
85-
HostAndPortToStringCache.TryAdd(host, portCache);
86-
}
87-
88-
if (!portCache.TryGetValue(port, out string hostTagValue))
89-
{
90-
hostTagValue = $"{requestUri.Host}:{requestUri.Port}";
91-
portCache.TryAdd(port, hostTagValue);
92-
}
93-
94-
return hostTagValue;
95-
}
96-
9765
/// <summary>
9866
/// Gets the OpenTelemetry standard uri tag value for a span based on its request <see cref="Uri"/>.
9967
/// </summary>

src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, A
9999
if (activity.IsAllDataRequested)
100100
{
101101
activity.SetTag(SemanticConventions.AttributeHttpMethod, request.Method);
102+
activity.SetTag(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host);
103+
if (!request.RequestUri.IsDefaultPort)
104+
{
105+
activity.SetTag(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port);
106+
}
107+
102108
activity.SetTag(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme);
103-
activity.SetTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
104109
activity.SetTag(SemanticConventions.AttributeHttpUrl, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri));
105110
activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.ProtocolVersion));
106111

test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public async Task DebugIndividualTestAsync()
231231
""spanAttributes"": {
232232
""http.scheme"": ""http"",
233233
""http.method"": ""GET"",
234-
""http.host"": ""{host}:{port}"",
234+
""net.peer.name"": ""{host}"",
235+
""net.peer.port"": ""{port}"",
235236
""http.status_code"": ""399"",
236237
""http.flavor"": ""{flavor}"",
237238
""http.url"": ""http://{host}:{port}/""

test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestActivitySourceTests.netfx.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class HttpWebRequestActivitySourceTests : IDisposable
3939
private readonly string testServerHost;
4040
private readonly int testServerPort;
4141
private readonly string hostNameAndPort;
42+
private readonly string netPeerName;
43+
private readonly int netPeerPort;
4244

4345
static HttpWebRequestActivitySourceTests()
4446
{
@@ -74,6 +76,8 @@ public HttpWebRequestActivitySourceTests()
7476
out this.testServerPort);
7577

7678
this.hostNameAndPort = $"{this.testServerHost}:{this.testServerPort}";
79+
this.netPeerName = this.testServerHost;
80+
this.netPeerPort = this.testServerPort;
7781

7882
void ProcessServerRequest(HttpListenerContext context)
7983
{
@@ -198,7 +202,7 @@ public async Task TestBasicReceiveAndResponseEvents(string method, string queryS
198202
// Check to make sure: The first record must be a request, the next record must be a response.
199203
Activity activity = AssertFirstEventWasStart(eventRecords);
200204

201-
VerifyActivityStartTags(this.hostNameAndPort, method, url, activity);
205+
VerifyActivityStartTags(this.netPeerName, this.netPeerPort, method, url, activity);
202206

203207
Assert.True(eventRecords.Records.TryDequeue(out var stopEvent));
204208
Assert.Equal("Stop", stopEvent.Key);
@@ -378,7 +382,7 @@ public async Task TestBasicReceiveAndResponseWebRequestEvents(string method, int
378382
// Check to make sure: The first record must be a request, the next record must be a response.
379383
Activity activity = AssertFirstEventWasStart(eventRecords);
380384

381-
VerifyActivityStartTags(this.hostNameAndPort, method, url, activity);
385+
VerifyActivityStartTags(this.netPeerName, this.netPeerPort, method, url, activity);
382386

383387
Assert.True(eventRecords.Records.TryDequeue(out var stopEvent));
384388
Assert.Equal("Stop", stopEvent.Key);
@@ -484,7 +488,7 @@ public async Task TestResponseWithoutContentEvents(string method)
484488
// Check to make sure: The first record must be a request, the next record must be a response.
485489
Activity activity = AssertFirstEventWasStart(eventRecords);
486490

487-
VerifyActivityStartTags(this.hostNameAndPort, method, url, activity);
491+
VerifyActivityStartTags(this.netPeerName, this.netPeerPort, method, url, activity);
488492

489493
Assert.True(eventRecords.Records.TryDequeue(out var stopEvent));
490494
Assert.Equal("Stop", stopEvent.Key);
@@ -523,9 +527,10 @@ public async Task TestRedirectedRequest(string method)
523527
[InlineData("POST")]
524528
public async Task TestRequestWithException(string method)
525529
{
530+
string host = Guid.NewGuid().ToString() + ".com";
526531
string url = method == "GET"
527-
? $"http://{Guid.NewGuid()}.com"
528-
: $"http://{Guid.NewGuid()}.com";
532+
? $"http://{host}"
533+
: $"http://{host}";
529534

530535
using var eventRecords = new ActivitySourceRecorder();
531536

@@ -548,7 +553,7 @@ public async Task TestRequestWithException(string method)
548553

549554
// Check to make sure: The first record must be a request, the next record must be an exception.
550555
Activity activity = AssertFirstEventWasStart(eventRecords);
551-
VerifyActivityStartTags(null, method, url, activity);
556+
VerifyActivityStartTags(host, null, method, url, activity);
552557

553558
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
554559
Assert.Equal("Stop", exceptionEvent.Key);
@@ -587,7 +592,7 @@ public async Task TestCanceledRequest(string method)
587592
Assert.Equal(1, eventRecords.Records.Count(rec => rec.Key == "Stop"));
588593

589594
Activity activity = AssertFirstEventWasStart(eventRecords);
590-
VerifyActivityStartTags(this.hostNameAndPort, method, url, activity);
595+
VerifyActivityStartTags(this.netPeerName, this.netPeerPort, method, url, activity);
591596

592597
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
593598
Assert.Equal("Stop", exceptionEvent.Key);
@@ -626,7 +631,7 @@ public async Task TestSecureTransportFailureRequest(string method)
626631
Assert.Equal(1, eventRecords.Records.Count(rec => rec.Key == "Stop"));
627632

628633
Activity activity = AssertFirstEventWasStart(eventRecords);
629-
VerifyActivityStartTags(null, method, url, activity);
634+
VerifyActivityStartTags("expired.badssl.com", null, method, url, activity);
630635

631636
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
632637
Assert.Equal("Stop", exceptionEvent.Key);
@@ -668,7 +673,7 @@ public async Task TestSecureTransportRetryFailureRequest(string method)
668673
Assert.Equal(1, eventRecords.Records.Count(rec => rec.Key == "Stop"));
669674

670675
Activity activity = AssertFirstEventWasStart(eventRecords);
671-
VerifyActivityStartTags(this.hostNameAndPort, method, url, activity);
676+
VerifyActivityStartTags(this.netPeerName, this.netPeerPort, method, url, activity);
672677

673678
Assert.True(eventRecords.Records.TryDequeue(out KeyValuePair<string, Activity> exceptionEvent));
674679
Assert.Equal("Stop", exceptionEvent.Key);
@@ -778,15 +783,17 @@ private static void VerifyHeaders(HttpWebRequest startRequest)
778783
Assert.Matches("^[0-9a-f]{2}-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$", traceparent);
779784
}
780785

781-
private static void VerifyActivityStartTags(string hostNameAndPort, string method, string url, Activity activity)
786+
private static void VerifyActivityStartTags(string netPeerName, int? netPeerPort, string method, string url, Activity activity)
782787
{
783788
Assert.NotNull(activity.TagObjects);
784789
Assert.Equal(method, activity.GetTagValue(SemanticConventions.AttributeHttpMethod));
785-
if (hostNameAndPort != null)
790+
if (netPeerPort != null)
786791
{
787-
Assert.Equal(hostNameAndPort, activity.GetTagValue(SemanticConventions.AttributeHttpHost));
792+
Assert.Equal(netPeerPort, activity.GetTagValue(SemanticConventions.AttributeNetPeerPort));
788793
}
789794

795+
Assert.Equal(netPeerName, activity.GetTagValue(SemanticConventions.AttributeNetPeerName));
796+
790797
Assert.Equal(url, activity.GetTagValue(SemanticConventions.AttributeHttpUrl));
791798
}
792799

test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ public void DebugIndividualTest()
182182
""spanAttributes"": {
183183
""http.scheme"": ""http"",
184184
""http.method"": ""GET"",
185-
""http.host"": ""{host}:{port}"",
185+
""net.peer.name"": ""{host}"",
186+
""net.peer.port"": ""{port}"",
186187
""http.flavor"": ""1.1"",
187188
""http.status_code"": ""200"",
188189
""http.url"": ""http://{host}:{port}/""

0 commit comments

Comments
 (0)