Skip to content

Commit

Permalink
Optimize gRPC client instrumentation for SuppressInstrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla committed Mar 12, 2021
1 parent c404e19 commit 69ec569
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ public GrpcClientDiagnosticListener(GrpcClientInstrumentationOptions options)

public override void OnStartActivity(Activity activity, object payload)
{
// The overall flow of what GrpcClient library does is as below:
// Activity.Start()
// DiagnosticSource.WriteEvent("Start", payload)
// DiagnosticSource.WriteEvent("Stop", payload)
// Activity.Stop()

// This method is in the WriteEvent("Start", payload) path.
// By this time, samplers have already run and
// activity.IsAllDataRequested populated accordingly.

if (Sdk.SuppressInstrumentation)
{
return;
}

// Ensure context propagation irrespective of sampling decision
if (!this.startRequestFetcher.TryFetch(payload, out HttpRequestMessage request) || request == null)
{
GrpcInstrumentationEventSource.Log.NullPayload(nameof(GrpcClientDiagnosticListener), nameof(this.OnStartActivity));
Expand Down Expand Up @@ -78,15 +94,15 @@ public override void OnStartActivity(Activity activity, object payload)
HttpRequestMessageContextPropagation.HeaderValueSetter);
}

var grpcMethod = GrpcTagHelper.GetGrpcMethodFromActivity(activity);
if (activity.IsAllDataRequested)
{
ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Client);

activity.DisplayName = grpcMethod?.Trim('/');
var grpcMethod = GrpcTagHelper.GetGrpcMethodFromActivity(activity);

ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
ActivityInstrumentationHelper.SetKindProperty(activity, ActivityKind.Client);
activity.DisplayName = grpcMethod?.Trim('/');

if (activity.IsAllDataRequested)
{
activity.SetTag(SemanticConventions.AttributeRpcSystem, GrpcTagHelper.RpcSystemGrpc);

if (GrpcTagHelper.TryParseRpcServiceAndRpcMethod(grpcMethod, out var rpcService, out var rpcMethod))
Expand Down

0 comments on commit 69ec569

Please sign in to comment.