-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update Semantic Conventions: grpc attributes in aspnetcore #4660
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b75d922
update grpc attributes in aspnetcore
TimothyMothra 6dce271
changelog
TimothyMothra 1edd340
tests
TimothyMothra b4fd979
Merge branch 'main' into 4484_aspnetcore3
TimothyMothra a77b627
cleanup
TimothyMothra e2842fe
Merge branch '4484_aspnetcore3' of https://github.com/TimothyMothra/o…
TimothyMothra 9f40958
comment tests
TimothyMothra 904271b
const
TimothyMothra fc975e0
Merge branch 'main' into 4484_aspnetcore3
utpilla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,15 @@ | |
using Greet; | ||
using Grpc.Core; | ||
using Grpc.Net.Client; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using OpenTelemetry.Context.Propagation; | ||
using OpenTelemetry.Instrumentation.Grpc.Services.Tests; | ||
using OpenTelemetry.Instrumentation.GrpcNetClient; | ||
using OpenTelemetry.Trace; | ||
using Xunit; | ||
using static OpenTelemetry.Internal.HttpSemanticConventionHelper; | ||
using Status = OpenTelemetry.Trace.Status; | ||
|
||
namespace OpenTelemetry.Instrumentation.Grpc.Tests | ||
|
@@ -114,6 +117,189 @@ public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes(bool? enableGrpcA | |
Assert.StartsWith("grpc-dotnet", activity.GetTagValue(SemanticConventions.AttributeHttpUserAgent) as string); | ||
} | ||
|
||
// Tests for v1.21.0 Semantic Conventions for database client calls. | ||
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md | ||
// This test emits the new attributes. | ||
// This test method can replace the other (old) test method when this library is GA. | ||
[Theory] | ||
[InlineData(null)] | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes_New(bool? enableGrpcAspNetCoreSupport) | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new Dictionary<string, string> { [SemanticConventionOptInKeyName] = "http" }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need not be done in this PR but the tests shouldn't be using the |
||
.Build(); | ||
|
||
var exportedItems = new List<Activity>(); | ||
var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder() | ||
.ConfigureServices(services => services.AddSingleton<IConfiguration>(configuration)); | ||
|
||
if (enableGrpcAspNetCoreSupport.HasValue) | ||
{ | ||
tracerProviderBuilder.AddAspNetCoreInstrumentation(options => | ||
{ | ||
options.EnableGrpcAspNetCoreSupport = enableGrpcAspNetCoreSupport.Value; | ||
}); | ||
} | ||
else | ||
{ | ||
tracerProviderBuilder.AddAspNetCoreInstrumentation(); | ||
} | ||
|
||
using var tracerProvider = tracerProviderBuilder | ||
.AddInMemoryExporter(exportedItems) | ||
.Build(); | ||
|
||
var clientLoopbackAddresses = new[] { IPAddress.Loopback.ToString(), IPAddress.IPv6Loopback.ToString() }; | ||
var uri = new Uri($"http://localhost:{this.server.Port}"); | ||
|
||
using var channel = GrpcChannel.ForAddress(uri); | ||
var client = new Greeter.GreeterClient(channel); | ||
var returnMsg = client.SayHello(new HelloRequest()).Message; | ||
Assert.False(string.IsNullOrEmpty(returnMsg)); | ||
|
||
WaitForExporterToReceiveItems(exportedItems, 1); | ||
Assert.Single(exportedItems); | ||
var activity = exportedItems[0]; | ||
|
||
Assert.Equal(ActivityKind.Server, activity.Kind); | ||
|
||
if (!enableGrpcAspNetCoreSupport.HasValue || enableGrpcAspNetCoreSupport.Value) | ||
{ | ||
Assert.Equal("grpc", activity.GetTagValue(SemanticConventions.AttributeRpcSystem)); | ||
Assert.Equal("greet.Greeter", activity.GetTagValue(SemanticConventions.AttributeRpcService)); | ||
Assert.Equal("SayHello", activity.GetTagValue(SemanticConventions.AttributeRpcMethod)); | ||
Assert.Contains(activity.GetTagValue(SemanticConventions.AttributeClientAddress), clientLoopbackAddresses); | ||
Assert.NotEqual(0, activity.GetTagValue(SemanticConventions.AttributeClientPort)); | ||
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName)); | ||
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName)); | ||
Assert.Equal(0, activity.GetTagValue(SemanticConventions.AttributeRpcGrpcStatusCode)); | ||
} | ||
else | ||
{ | ||
Assert.NotNull(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName)); | ||
Assert.NotNull(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName)); | ||
} | ||
|
||
Assert.Equal(Status.Unset, activity.GetStatus()); | ||
|
||
// The following are http.* attributes that are also included on the span for the gRPC invocation. | ||
Assert.Equal("localhost", activity.GetTagValue(SemanticConventions.AttributeServerAddress)); | ||
Assert.Equal(this.server.Port, activity.GetTagValue(SemanticConventions.AttributeServerPort)); | ||
Assert.Equal("POST", activity.GetTagValue(SemanticConventions.AttributeHttpRequestMethod)); | ||
Assert.Equal("http", activity.GetTagValue(SemanticConventions.AttributeUrlScheme)); | ||
Assert.Equal("/greet.Greeter/SayHello", activity.GetTagValue(SemanticConventions.AttributeUrlPath)); | ||
Assert.Equal("2.0", activity.GetTagValue(SemanticConventions.AttributeNetworkProtocolVersion)); | ||
Assert.StartsWith("grpc-dotnet", activity.GetTagValue(SemanticConventions.AttributeUserAgentOriginal) as string); | ||
} | ||
|
||
// Tests for v1.21.0 Semantic Conventions for database client calls. | ||
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md | ||
// This test emits both the new and older attributes. | ||
// This test method can be deleted when this library is GA. | ||
[Theory] | ||
[InlineData(null)] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes_Dupe(bool? enableGrpcAspNetCoreSupport) | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new Dictionary<string, string> { [SemanticConventionOptInKeyName] = "http/dup" }) | ||
.Build(); | ||
|
||
var exportedItems = new List<Activity>(); | ||
var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder() | ||
.ConfigureServices(services => services.AddSingleton<IConfiguration>(configuration)); | ||
|
||
if (enableGrpcAspNetCoreSupport.HasValue) | ||
{ | ||
tracerProviderBuilder.AddAspNetCoreInstrumentation(options => | ||
{ | ||
options.EnableGrpcAspNetCoreSupport = enableGrpcAspNetCoreSupport.Value; | ||
}); | ||
} | ||
else | ||
{ | ||
tracerProviderBuilder.AddAspNetCoreInstrumentation(); | ||
} | ||
|
||
using var tracerProvider = tracerProviderBuilder | ||
.AddInMemoryExporter(exportedItems) | ||
.Build(); | ||
|
||
var clientLoopbackAddresses = new[] { IPAddress.Loopback.ToString(), IPAddress.IPv6Loopback.ToString() }; | ||
var uri = new Uri($"http://localhost:{this.server.Port}"); | ||
|
||
using var channel = GrpcChannel.ForAddress(uri); | ||
var client = new Greeter.GreeterClient(channel); | ||
var returnMsg = client.SayHello(new HelloRequest()).Message; | ||
Assert.False(string.IsNullOrEmpty(returnMsg)); | ||
|
||
WaitForExporterToReceiveItems(exportedItems, 1); | ||
Assert.Single(exportedItems); | ||
var activity = exportedItems[0]; | ||
|
||
Assert.Equal(ActivityKind.Server, activity.Kind); | ||
|
||
// OLD | ||
if (!enableGrpcAspNetCoreSupport.HasValue || enableGrpcAspNetCoreSupport.Value) | ||
{ | ||
Assert.Equal("grpc", activity.GetTagValue(SemanticConventions.AttributeRpcSystem)); | ||
Assert.Equal("greet.Greeter", activity.GetTagValue(SemanticConventions.AttributeRpcService)); | ||
Assert.Equal("SayHello", activity.GetTagValue(SemanticConventions.AttributeRpcMethod)); | ||
Assert.Contains(activity.GetTagValue(SemanticConventions.AttributeNetPeerIp), clientLoopbackAddresses); | ||
Assert.NotEqual(0, activity.GetTagValue(SemanticConventions.AttributeNetPeerPort)); | ||
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName)); | ||
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName)); | ||
Assert.Equal(0, activity.GetTagValue(SemanticConventions.AttributeRpcGrpcStatusCode)); | ||
} | ||
else | ||
{ | ||
Assert.NotNull(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName)); | ||
Assert.NotNull(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName)); | ||
} | ||
|
||
Assert.Equal(Status.Unset, activity.GetStatus()); | ||
|
||
// The following are http.* attributes that are also included on the span for the gRPC invocation. | ||
Assert.Equal("localhost", activity.GetTagValue(SemanticConventions.AttributeNetHostName)); | ||
Assert.Equal(this.server.Port, activity.GetTagValue(SemanticConventions.AttributeNetHostPort)); | ||
Assert.Equal("POST", activity.GetTagValue(SemanticConventions.AttributeHttpMethod)); | ||
Assert.Equal("/greet.Greeter/SayHello", activity.GetTagValue(SemanticConventions.AttributeHttpTarget)); | ||
Assert.Equal($"http://localhost:{this.server.Port}/greet.Greeter/SayHello", activity.GetTagValue(SemanticConventions.AttributeHttpUrl)); | ||
Assert.StartsWith("grpc-dotnet", activity.GetTagValue(SemanticConventions.AttributeHttpUserAgent) as string); | ||
|
||
// NEW | ||
if (!enableGrpcAspNetCoreSupport.HasValue || enableGrpcAspNetCoreSupport.Value) | ||
{ | ||
Assert.Equal("grpc", activity.GetTagValue(SemanticConventions.AttributeRpcSystem)); | ||
Assert.Equal("greet.Greeter", activity.GetTagValue(SemanticConventions.AttributeRpcService)); | ||
Assert.Equal("SayHello", activity.GetTagValue(SemanticConventions.AttributeRpcMethod)); | ||
Assert.Contains(activity.GetTagValue(SemanticConventions.AttributeClientAddress), clientLoopbackAddresses); | ||
Assert.NotEqual(0, activity.GetTagValue(SemanticConventions.AttributeClientPort)); | ||
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName)); | ||
Assert.Null(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName)); | ||
Assert.Equal(0, activity.GetTagValue(SemanticConventions.AttributeRpcGrpcStatusCode)); | ||
} | ||
else | ||
{ | ||
Assert.NotNull(activity.GetTagValue(GrpcTagHelper.GrpcMethodTagName)); | ||
Assert.NotNull(activity.GetTagValue(GrpcTagHelper.GrpcStatusCodeTagName)); | ||
} | ||
|
||
Assert.Equal(Status.Unset, activity.GetStatus()); | ||
|
||
// The following are http.* attributes that are also included on the span for the gRPC invocation. | ||
Assert.Equal("localhost", activity.GetTagValue(SemanticConventions.AttributeServerAddress)); | ||
Assert.Equal(this.server.Port, activity.GetTagValue(SemanticConventions.AttributeServerPort)); | ||
Assert.Equal("POST", activity.GetTagValue(SemanticConventions.AttributeHttpRequestMethod)); | ||
Assert.Equal("http", activity.GetTagValue(SemanticConventions.AttributeUrlScheme)); | ||
Assert.Equal("/greet.Greeter/SayHello", activity.GetTagValue(SemanticConventions.AttributeUrlPath)); | ||
Assert.Equal("2.0", activity.GetTagValue(SemanticConventions.AttributeNetworkProtocolVersion)); | ||
Assert.StartsWith("grpc-dotnet", activity.GetTagValue(SemanticConventions.AttributeUserAgentOriginal) as string); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
[Theory(Skip = "Skipping for .NET 6 and higher due to bug #3023")] | ||
#endif | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add a comment saying this replaces
net.peer.ip
in specific cases.