Instrumenting server calls
Service-side instrumentation is similar. We can use the gRPC interceptor again and this time override the UnaryServerHandler
method. Once the request is received, we should extract the context and start a new activity. It should have the server
kind, a name that follows the same pattern as for the client span – {package.service}/{method}
– and attributes very similar to those we saw on the client. Here’s the interceptor code:
server/GrpcTracingInterceptor.cs
var traceContext = _propagator.Extract(default, ctx.RequestHeaders, static (headers, k) => new[] { headers.GetValue(k) }); Baggage.Current = traceContext.Baggage; using var activity = Source.StartActivity(ctx.Method, ActivityKind.Server, traceContext.ActivityContext); if (activity?.IsAllDataRequested != true) return await continuation(request, ctx); SetRpcAttributes(activity, ctx.Host, ctx.Method); try { var response...