-
Notifications
You must be signed in to change notification settings - Fork 771
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
Add gRPC attributes for ASP.NET Core #803
Merged
cijothomas
merged 25 commits into
open-telemetry:master
from
alanwest:alanwest/grpc-aspnetcore
Aug 5, 2020
Merged
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b64eb1b
Change gRPC test project name and namespace
alanwest 8d019fb
Use SpanAttributeConstants in GrpcClientTests
alanwest 46fa7bb
Add test for Grpc.AspNetCore instrumentation
alanwest 752e995
Add gRPC attributes for ASP.NET Core
alanwest 9854dc4
Merge branch 'master' into alanwest/grpc-aspnetcore
cijothomas b002ed2
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest c41fba8
Prevent parallel execution of gRPC client and server test suites
alanwest d319068
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest 144cbce
Add wait logic from AspNetCore tests ensuring end callback gets invoked
alanwest 2e0e76d
Partial class for gRPC test to resolve test conflicts
alanwest 9a76a77
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest 219654a
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest 2f9b16d
Make "grpc" a const
alanwest a638d3f
Set http.status_code attribute even on a gRPC invocation
alanwest 8d57617
Fix GrpcFixture
alanwest 8232d33
Add checks for http.* attributes to ASP.NET Core gRPC test
alanwest 191401a
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest 72257ca
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest f4c149c
Merge branch 'master' into alanwest/grpc-aspnetcore
cijothomas 07f3625
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest 234fafd
Merge branch 'alanwest/grpc-aspnetcore' of github.com:alanwest/opente…
alanwest 863b8d2
Fix merge snafu
alanwest df6d428
Merge branch 'master' into alanwest/grpc-aspnetcore
alanwest cda4766
Update changelog
alanwest a9edc7a
Merge branch 'master' into alanwest/grpc-aspnetcore
cijothomas 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
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
File renamed without changes.
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
File renamed without changes.
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
89 changes: 89 additions & 0 deletions
89
test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// <copyright file="GrpcTests.server.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading; | ||
using Greet; | ||
using Grpc.Net.Client; | ||
using Moq; | ||
using OpenTelemetry.Instrumentation.Grpc.Tests.Services; | ||
using OpenTelemetry.Trace; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Instrumentation.Grpc.Tests | ||
{ | ||
public partial class GrpcTests : IClassFixture<GrpcFixture<GreeterService>> | ||
{ | ||
private GrpcFixture<GreeterService> fixture; | ||
|
||
public GrpcTests(GrpcFixture<GreeterService> fixture) | ||
{ | ||
this.fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes() | ||
{ | ||
var clientLoopbackAddresses = new[] { IPAddress.Loopback.ToString(), IPAddress.IPv6Loopback.ToString() }; | ||
var uri = new Uri($"http://localhost:{this.fixture.Port}"); | ||
var spanProcessor = this.fixture.GrpcServerSpanProcessor; | ||
|
||
var channel = GrpcChannel.ForAddress(uri); | ||
var client = new Greeter.GreeterClient(channel); | ||
var rs = client.SayHello(new HelloRequest()); | ||
|
||
WaitForProcessorInvocations(spanProcessor, 2); | ||
|
||
Assert.Equal(2, spanProcessor.Invocations.Count); // begin and end was called | ||
var span = (Activity)spanProcessor.Invocations[1].Arguments[0]; | ||
|
||
Assert.Equal(ActivityKind.Server, span.Kind); | ||
Assert.Equal("grpc", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeRpcSystem).Value); | ||
Assert.Equal("greet.Greeter", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeRpcService).Value); | ||
Assert.Equal("SayHello", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeRpcMethod).Value); | ||
Assert.Contains(span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeNetPeerIp).Value, clientLoopbackAddresses); | ||
Assert.True(!string.IsNullOrEmpty(span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeNetPeerPort).Value)); | ||
Assert.Equal(Status.Ok, span.GetStatus()); | ||
|
||
// The following are http.* attributes that are also included on the span for the gRPC invocation. | ||
Assert.Equal($"localhost:{this.fixture.Port}", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpHost).Value); | ||
Assert.Equal("POST", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpMethod).Value); | ||
Assert.Equal("/greet.Greeter/SayHello", span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpPathKey).Value); | ||
Assert.Equal($"http://localhost:{this.fixture.Port}/greet.Greeter/SayHello", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpUrl).Value); | ||
Assert.StartsWith("grpc-dotnet", span.Tags.FirstOrDefault(i => i.Key == SemanticConventions.AttributeHttpUserAgent).Value); | ||
|
||
// This attribute is added by the gRPC for .NET library. There is a discussion of having the OTel instrumentation remove it. | ||
// See: https://github.com/open-telemetry/opentelemetry-dotnet/issues/482#issuecomment-655753756 | ||
Assert.Equal($"0", span.Tags.FirstOrDefault(i => i.Key == "grpc.status_code").Value); | ||
} | ||
|
||
private static void WaitForProcessorInvocations(Mock<ActivityProcessor> spanProcessor, int invocationCount) | ||
{ | ||
// We need to let End callback execute as it is executed AFTER response was returned. | ||
// In unit tests environment there may be a lot of parallel unit tests executed, so | ||
// giving some breezing room for the End callback to complete | ||
Assert.True(SpinWait.SpinUntil( | ||
() => | ||
{ | ||
Thread.Sleep(10); | ||
return spanProcessor.Invocations.Count >= invocationCount; | ||
}, | ||
TimeSpan.FromSeconds(1))); | ||
} | ||
} | ||
} |
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
File renamed without changes.
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
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 looks like different tags are added depending on whether the GrpcMethod can be retrieved, is that correct?
For example:
DisplayName (currently commented out, AttributeRpcSystem, service, method, etc
vs
AttributeHttpStatusCode, Status
Should we drop the else so the later tags are always added?
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.
My decision here was intentional, though I did raise the question earlier whether all the
http.*
attributes should be omitted from the span in the context of a gRPC invocation.Two things occur in the else clause:
http.status_code
attribute is setAt a minimum we should not set the span's status based on the HTTP status because the gRPC status should be used instead. Though, I could see an argument for leaving the
http.status_code
attribute. Otherhttp.*
attributes are added inOnStartActivity
. Given that this PR currently leaves those untouched, then it probably makes sense to leavehttp.status_code
.I'm ok coming back to this question of keeping vs. omitting the
http.*
attributes and iterating on this instrumentation later, though I think I need to make sure the test actually reflects these decisions.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.
I expanded the test to reflect this conversation.
opentelemetry-dotnet/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs
Lines 63 to 72 in 8232d33
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.
I think we discussed a long time ago, but I personally like having both the http and grpc tags in there. GRPC is not limited to working over HTTP, even though not many alternatives exist. I may be considered unnecessary details for now though 🤷
@cijothomas @CodeBlanch What are your thoughts?
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.
I agree that it probably makes sense to leave them in. I'm not necessarily advocating for one way or another at this point, but just to highlight some things for ongoing consideration:
Yes, true, and I definitely see value in reflecting transport type on calls both server-side and client-side. This may be a thing that needs to be continued to be pushed at the spec level. I personally have experience with instrumenting WCF - which is a complex beast, and I've confronted a lot of these same considerations with it.
Slight tangent, but with WCF and perhaps eventually with gRPC, transport type used affects whether we can do auto-context propagation. For example, if the transport is a raw TCP channel, then there exist no structured headers mechanism to inject context - it has to be done manually.