@@ -43,6 +43,22 @@ public GrpcClientDiagnosticListener(GrpcClientInstrumentationOptions options)
4343
4444 public override void OnStartActivity ( Activity activity , object payload )
4545 {
46+ // The overall flow of what GrpcClient library does is as below:
47+ // Activity.Start()
48+ // DiagnosticSource.WriteEvent("Start", payload)
49+ // DiagnosticSource.WriteEvent("Stop", payload)
50+ // Activity.Stop()
51+
52+ // This method is in the WriteEvent("Start", payload) path.
53+ // By this time, samplers have already run and
54+ // activity.IsAllDataRequested populated accordingly.
55+
56+ if ( Sdk . SuppressInstrumentation )
57+ {
58+ return ;
59+ }
60+
61+ // Ensure context propagation irrespective of sampling decision
4662 if ( ! this . startRequestFetcher . TryFetch ( payload , out HttpRequestMessage request ) || request == null )
4763 {
4864 GrpcInstrumentationEventSource . Log . NullPayload ( nameof ( GrpcClientDiagnosticListener ) , nameof ( this . OnStartActivity ) ) ;
@@ -78,15 +94,15 @@ public override void OnStartActivity(Activity activity, object payload)
7894 HttpRequestMessageContextPropagation . HeaderValueSetter ) ;
7995 }
8096
81- var grpcMethod = GrpcTagHelper . GetGrpcMethodFromActivity ( activity ) ;
97+ if ( activity . IsAllDataRequested )
98+ {
99+ ActivityInstrumentationHelper . SetActivitySourceProperty ( activity , ActivitySource ) ;
100+ ActivityInstrumentationHelper . SetKindProperty ( activity , ActivityKind . Client ) ;
82101
83- activity . DisplayName = grpcMethod ? . Trim ( '/' ) ;
102+ var grpcMethod = GrpcTagHelper . GetGrpcMethodFromActivity ( activity ) ;
84103
85- ActivityInstrumentationHelper . SetActivitySourceProperty ( activity , ActivitySource ) ;
86- ActivityInstrumentationHelper . SetKindProperty ( activity , ActivityKind . Client ) ;
104+ activity . DisplayName = grpcMethod ? . Trim ( '/' ) ;
87105
88- if ( activity . IsAllDataRequested )
89- {
90106 activity . SetTag ( SemanticConventions . AttributeRpcSystem , GrpcTagHelper . RpcSystemGrpc ) ;
91107
92108 if ( GrpcTagHelper . TryParseRpcServiceAndRpcMethod ( grpcMethod , out var rpcService , out var rpcMethod ) )
0 commit comments