Skip to content

Commit 01d168b

Browse files
committed
[Instrumentation.GrpcNetClient] Support only stable HTTP semantic convention
1 parent 914ebf9 commit 01d168b

File tree

5 files changed

+15
-254
lines changed

5 files changed

+15
-254
lines changed

src/OpenTelemetry.Instrumentation.GrpcNetClient/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
[issue](https://github.com/open-telemetry/opentelemetry-dotnet/issues/5092)
1010
for details and workaround.
1111
([#5077](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5077))
12+
* Removed support for `http` and `http/dup` values for`OTEL_SEMCONV_STABILITY_OPT_IN`
13+
environment variable. The library will now emit only the
14+
[stable](https://github.com/open-telemetry/semantic-conventions/tree/v1.23.0/docs/http)
15+
HTTP semantic conventions.
16+
([#5259](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5259))
1217

1318
## 1.6.0-beta.3
1419

src/OpenTelemetry.Instrumentation.GrpcNetClient/GrpcClientInstrumentationOptions.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,6 @@ namespace OpenTelemetry.Instrumentation.GrpcNetClient;
1212
/// </summary>
1313
public class GrpcClientInstrumentationOptions
1414
{
15-
internal readonly HttpSemanticConvention HttpSemanticConvention;
16-
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="GrpcClientInstrumentationOptions"/> class.
19-
/// </summary>
20-
public GrpcClientInstrumentationOptions()
21-
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
22-
{
23-
}
24-
25-
internal GrpcClientInstrumentationOptions(IConfiguration configuration)
26-
{
27-
Debug.Assert(configuration != null, "configuration was null");
28-
29-
this.HttpSemanticConvention = GetSemanticConventionOptIn(configuration);
30-
}
31-
3215
/// <summary>
3316
/// Gets or sets a value indicating whether down stream instrumentation is suppressed (disabled).
3417
/// </summary>

src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/GrpcClientDiagnosticListener.cs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using OpenTelemetry.Context.Propagation;
1010
using OpenTelemetry.Instrumentation.Http;
1111
using OpenTelemetry.Trace;
12-
using static OpenTelemetry.Internal.HttpSemanticConventionHelper;
1312

1413
namespace OpenTelemetry.Instrumentation.GrpcNetClient.Implementation;
1514

@@ -27,17 +26,11 @@ internal sealed class GrpcClientDiagnosticListener : ListenerHandler
2726
private static readonly PropertyFetcher<HttpResponseMessage> StopResponseFetcher = new("Response");
2827

2928
private readonly GrpcClientInstrumentationOptions options;
30-
private readonly bool emitOldAttributes;
31-
private readonly bool emitNewAttributes;
3229

3330
public GrpcClientDiagnosticListener(GrpcClientInstrumentationOptions options)
3431
: base("Grpc.Net.Client")
3532
{
3633
this.options = options;
37-
38-
this.emitOldAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.Old);
39-
40-
this.emitNewAttributes = this.options.HttpSemanticConvention.HasFlag(HttpSemanticConvention.New);
4134
}
4235

4336
public override void OnEventWritten(string name, object payload)
@@ -133,35 +126,18 @@ public void OnStartActivity(Activity activity, object payload)
133126
}
134127

135128
var uriHostNameType = Uri.CheckHostName(request.RequestUri.Host);
136-
if (this.emitOldAttributes)
137-
{
138-
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
139-
{
140-
activity.SetTag(SemanticConventions.AttributeNetPeerIp, request.RequestUri.Host);
141-
}
142-
else
143-
{
144-
activity.SetTag(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host);
145-
}
146129

147-
activity.SetTag(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port);
130+
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
131+
{
132+
activity.SetTag(SemanticConventions.AttributeServerSocketAddress, request.RequestUri.Host);
148133
}
149-
150-
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md
151-
if (this.emitNewAttributes)
134+
else
152135
{
153-
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
154-
{
155-
activity.SetTag(SemanticConventions.AttributeServerSocketAddress, request.RequestUri.Host);
156-
}
157-
else
158-
{
159-
activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host);
160-
}
161-
162-
activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port);
136+
activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host);
163137
}
164138

139+
activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port);
140+
165141
try
166142
{
167143
this.options.EnrichWithHttpRequestMessage?.Invoke(activity, request);

src/OpenTelemetry.Instrumentation.GrpcNetClient/TracerProviderBuilderExtensions.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,10 @@ public static TracerProviderBuilder AddGrpcClientInstrumentation(
5050

5151
name ??= Options.DefaultName;
5252

53-
builder.ConfigureServices(services =>
53+
if (configure != null)
5454
{
55-
if (configure != null)
56-
{
57-
services.Configure(name, configure);
58-
}
59-
60-
services.RegisterOptionsFactory(configuration => new GrpcClientInstrumentationOptions(configuration));
61-
});
55+
builder.ConfigureServices(services => services.Configure(name, configure));
56+
}
6257

6358
builder.AddSource(GrpcClientDiagnosticListener.ActivitySourceName);
6459
builder.AddLegacySource("Grpc.Net.Client.GrpcOut");

test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.client.cs

Lines changed: 0 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Grpc.Core;
99
#endif
1010
using Grpc.Net.Client;
11-
using Microsoft.Extensions.Configuration;
1211
using Microsoft.Extensions.DependencyInjection;
1312
#if !NETFRAMEWORK
1413
using OpenTelemetry.Context.Propagation;
@@ -91,213 +90,16 @@ public void GrpcClientCallsAreCollectedSuccessfully(string baseAddress, bool sho
9190

9291
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
9392
{
94-
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeNetPeerIp));
95-
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeNetPeerName));
96-
}
97-
else
98-
{
99-
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeNetPeerIp));
100-
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeNetPeerName));
101-
}
102-
103-
Assert.Equal(uri.Port, activity.GetTagValue(SemanticConventions.AttributeNetPeerPort));
104-
Assert.Equal(Status.Unset, activity.GetStatus());
105-
106-
// Tags added by the library then removed from the instrumentation
107-
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName));
108-
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName));
109-
Assert.Equal(0, activity.GetTagValue(SemanticConventions.AttributeRpcGrpcStatusCode));
110-
111-
if (shouldEnrich)
112-
{
113-
Assert.True(enrichWithHttpRequestMessageCalled);
114-
Assert.True(enrichWithHttpResponseMessageCalled);
115-
}
116-
}
117-
118-
[Theory]
119-
[InlineData("http://localhost")]
120-
[InlineData("http://localhost", false)]
121-
[InlineData("http://127.0.0.1")]
122-
[InlineData("http://127.0.0.1", false)]
123-
[InlineData("http://[::1]")]
124-
[InlineData("http://[::1]", false)]
125-
public void GrpcClientCallsAreCollectedSuccessfully_New(string baseAddress, bool shouldEnrich = true)
126-
{
127-
var config = new KeyValuePair<string, string>[] { new("OTEL_SEMCONV_STABILITY_OPT_IN", "http") };
128-
var configuration = new ConfigurationBuilder()
129-
.AddInMemoryCollection(config)
130-
.Build();
131-
132-
bool enrichWithHttpRequestMessageCalled = false;
133-
bool enrichWithHttpResponseMessageCalled = false;
134-
135-
var uri = new Uri($"{baseAddress}:1234");
136-
var uriHostNameType = Uri.CheckHostName(uri.Host);
137-
138-
using var httpClient = ClientTestHelpers.CreateTestClient(async request =>
139-
{
140-
var streamContent = await ClientTestHelpers.CreateResponseContent(new HelloReply());
141-
var response = ResponseUtils.CreateResponse(HttpStatusCode.OK, streamContent, grpcStatusCode: global::Grpc.Core.StatusCode.OK);
142-
response.TrailingHeaders().Add("grpc-message", "value");
143-
return response;
144-
});
145-
146-
var exportedItems = new List<Activity>();
147-
148-
using var parent = new Activity("parent")
149-
.SetIdFormat(ActivityIdFormat.W3C)
150-
.Start();
151-
152-
using (Sdk.CreateTracerProviderBuilder()
153-
.SetSampler(new AlwaysOnSampler())
154-
.ConfigureServices(services => services.AddSingleton<IConfiguration>(configuration))
155-
.AddGrpcClientInstrumentation(options =>
156-
{
157-
if (shouldEnrich)
158-
{
159-
options.EnrichWithHttpRequestMessage = (activity, httpRequestMessage) => { enrichWithHttpRequestMessageCalled = true; };
160-
options.EnrichWithHttpResponseMessage = (activity, httpResponseMessage) => { enrichWithHttpResponseMessageCalled = true; };
161-
}
162-
})
163-
.AddInMemoryExporter(exportedItems)
164-
.Build())
165-
{
166-
var channel = GrpcChannel.ForAddress(uri, new GrpcChannelOptions
167-
{
168-
HttpClient = httpClient,
169-
});
170-
var client = new Greeter.GreeterClient(channel);
171-
var rs = client.SayHello(new HelloRequest());
172-
}
173-
174-
Assert.Single(exportedItems);
175-
var activity = exportedItems[0];
176-
177-
ValidateGrpcActivity(activity);
178-
Assert.Equal(parent.TraceId, activity.Context.TraceId);
179-
Assert.Equal(parent.SpanId, activity.ParentSpanId);
180-
Assert.NotEqual(parent.SpanId, activity.Context.SpanId);
181-
Assert.NotEqual(default, activity.Context.SpanId);
182-
183-
Assert.Equal($"greet.Greeter/SayHello", activity.DisplayName);
184-
Assert.Equal("grpc", activity.GetTagValue(SemanticConventions.AttributeRpcSystem));
185-
Assert.Equal("greet.Greeter", activity.GetTagValue(SemanticConventions.AttributeRpcService));
186-
Assert.Equal("SayHello", activity.GetTagValue(SemanticConventions.AttributeRpcMethod));
187-
188-
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
189-
{
190-
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeServerSocketAddress));
191-
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeServerAddress));
192-
}
193-
else
194-
{
195-
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeServerSocketAddress));
196-
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeServerAddress));
197-
}
198-
199-
Assert.Equal(uri.Port, activity.GetTagValue(SemanticConventions.AttributeServerPort));
200-
Assert.Equal(Status.Unset, activity.GetStatus());
201-
202-
// Tags added by the library then removed from the instrumentation
203-
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName));
204-
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName));
205-
Assert.Equal(0, activity.GetTagValue(SemanticConventions.AttributeRpcGrpcStatusCode));
206-
207-
if (shouldEnrich)
208-
{
209-
Assert.True(enrichWithHttpRequestMessageCalled);
210-
Assert.True(enrichWithHttpResponseMessageCalled);
211-
}
212-
}
213-
214-
[Theory]
215-
[InlineData("http://localhost")]
216-
[InlineData("http://localhost", false)]
217-
[InlineData("http://127.0.0.1")]
218-
[InlineData("http://127.0.0.1", false)]
219-
[InlineData("http://[::1]")]
220-
[InlineData("http://[::1]", false)]
221-
public void GrpcClientCallsAreCollectedSuccessfully_Dupe(string baseAddress, bool shouldEnrich = true)
222-
{
223-
var config = new KeyValuePair<string, string>[] { new("OTEL_SEMCONV_STABILITY_OPT_IN", "http/dup") };
224-
var configuration = new ConfigurationBuilder()
225-
.AddInMemoryCollection(config)
226-
.Build();
227-
228-
bool enrichWithHttpRequestMessageCalled = false;
229-
bool enrichWithHttpResponseMessageCalled = false;
230-
231-
var uri = new Uri($"{baseAddress}:1234");
232-
var uriHostNameType = Uri.CheckHostName(uri.Host);
233-
234-
using var httpClient = ClientTestHelpers.CreateTestClient(async request =>
235-
{
236-
var streamContent = await ClientTestHelpers.CreateResponseContent(new HelloReply());
237-
var response = ResponseUtils.CreateResponse(HttpStatusCode.OK, streamContent, grpcStatusCode: global::Grpc.Core.StatusCode.OK);
238-
response.TrailingHeaders().Add("grpc-message", "value");
239-
return response;
240-
});
241-
242-
var exportedItems = new List<Activity>();
243-
244-
using var parent = new Activity("parent")
245-
.SetIdFormat(ActivityIdFormat.W3C)
246-
.Start();
247-
248-
using (Sdk.CreateTracerProviderBuilder()
249-
.SetSampler(new AlwaysOnSampler())
250-
.ConfigureServices(services => services.AddSingleton<IConfiguration>(configuration))
251-
.AddGrpcClientInstrumentation(options =>
252-
{
253-
if (shouldEnrich)
254-
{
255-
options.EnrichWithHttpRequestMessage = (activity, httpRequestMessage) => { enrichWithHttpRequestMessageCalled = true; };
256-
options.EnrichWithHttpResponseMessage = (activity, httpResponseMessage) => { enrichWithHttpResponseMessageCalled = true; };
257-
}
258-
})
259-
.AddInMemoryExporter(exportedItems)
260-
.Build())
261-
{
262-
var channel = GrpcChannel.ForAddress(uri, new GrpcChannelOptions
263-
{
264-
HttpClient = httpClient,
265-
});
266-
var client = new Greeter.GreeterClient(channel);
267-
var rs = client.SayHello(new HelloRequest());
268-
}
269-
270-
Assert.Single(exportedItems);
271-
var activity = exportedItems[0];
272-
273-
ValidateGrpcActivity(activity);
274-
Assert.Equal(parent.TraceId, activity.Context.TraceId);
275-
Assert.Equal(parent.SpanId, activity.ParentSpanId);
276-
Assert.NotEqual(parent.SpanId, activity.Context.SpanId);
277-
Assert.NotEqual(default, activity.Context.SpanId);
278-
279-
Assert.Equal($"greet.Greeter/SayHello", activity.DisplayName);
280-
Assert.Equal("grpc", activity.GetTagValue(SemanticConventions.AttributeRpcSystem));
281-
Assert.Equal("greet.Greeter", activity.GetTagValue(SemanticConventions.AttributeRpcService));
282-
Assert.Equal("SayHello", activity.GetTagValue(SemanticConventions.AttributeRpcMethod));
283-
284-
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
285-
{
286-
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeNetPeerIp));
287-
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeNetPeerName));
28893
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeServerSocketAddress));
28994
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeServerAddress));
29095
}
29196
else
29297
{
293-
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeNetPeerIp));
294-
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeNetPeerName));
29598
Assert.Null(activity.GetTagValue(SemanticConventions.AttributeServerSocketAddress));
29699
Assert.Equal(uri.Host, activity.GetTagValue(SemanticConventions.AttributeServerAddress));
297100
}
298101

299102
Assert.Equal(uri.Port, activity.GetTagValue(SemanticConventions.AttributeServerPort));
300-
Assert.Equal(uri.Port, activity.GetTagValue(SemanticConventions.AttributeNetPeerPort));
301103
Assert.Equal(Status.Unset, activity.GetStatus());
302104

303105
// Tags added by the library then removed from the instrumentation

0 commit comments

Comments
 (0)