public static Scope?CreateServerSpan <TTarget>(Tracer tracer, TTarget target, IMetadata?metadata) { var serverHandler = target.DuckCast <ServerCallHandlerStruct>(); var method = serverHandler.Method; Scope?scope = null; try { var tags = new GrpcServerTags(); // Grpc.Core server tags are typically the root span, so use enabledWithGlobalSetting=true GrpcCommon.AddGrpcTags(tags, tracer, method.GrpcType, name: method.Name, path: method.FullName, serviceName: method.ServiceName, analyticsEnabledWithGlobalSetting: true); // If we have a local span (e.g. from aspnetcore) then use that as the parent // Otherwise, use the distributed context as the parent var spanContext = tracer.ActiveScope?.Span.Context; if (spanContext is null) { spanContext = ExtractPropagatedContext(metadata); } var serviceName = tracer.DefaultServiceName ?? "grpc-server"; scope = tracer.StartActiveInternal(GrpcCommon.OperationName, parent: spanContext, tags: tags, serviceName: serviceName); var span = scope.Span; span.Type = SpanTypes.Grpc; span.ResourceName = method.FullName; if (metadata?.Count > 0) { span.SetHeaderTags(new MetadataHeadersCollection(metadata), tracer.Settings.GrpcTags, GrpcCommon.RequestMetadataTagPrefix); } } catch (Exception ex) { Log.Error(ex, "Error creating server span for GRPC call"); } return(scope); }
public static Scope?CreateServerSpan <T>(Tracer tracer, T instance, HttpRequest requestMessage) { if (!tracer.Settings.IsIntegrationEnabled(IntegrationId.Grpc)) { return(null); } Scope?scope = null; var method = instance.DuckCast <ServerCallHandlerBaseStruct>().MethodInvoker.Method; try { var tags = new GrpcServerTags(); GrpcCommon.AddGrpcTags(tags, tracer, method.GrpcType, name: method.Name, path: method.FullName, serviceName: method.ServiceName); // If we have a local span (e.g. from aspnetcore) then use that as the parent // Otherwise, use the distributed context as the parent var spanContext = tracer.ActiveScope?.Span.Context; if (spanContext is null) { spanContext = ExtractPropagatedContext(requestMessage); } var serviceName = tracer.DefaultServiceName ?? "grpc-server"; scope = tracer.StartActiveInternal(GrpcCommon.OperationName, parent: spanContext, tags: tags, serviceName: serviceName); var span = scope.Span; span.Type = SpanTypes.Grpc; span.ResourceName = method.FullName; tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId.Grpc); } catch (Exception ex) { Log.Error(ex, "Error creating server span for GRPC call"); } return(scope); }