-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathGrpcLegacyServerCommon.cs
82 lines (69 loc) · 3.31 KB
/
GrpcLegacyServerCommon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// <copyright file="GrpcLegacyServerCommon.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using Datadog.Trace.Configuration;
using Datadog.Trace.DuckTyping;
using Datadog.Trace.Logging;
using Datadog.Trace.Propagators;
using Datadog.Trace.Tagging;
#nullable enable
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Grpc.GrpcLegacy.Server
{
internal class GrpcLegacyServerCommon
{
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(GrpcLegacyServerCommon));
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)
{
var extractedContext = ExtractPropagatedContext(tracer, metadata).MergeBaggageInto(Baggage.Current);
spanContext = extractedContext.SpanContext;
}
var serviceName = tracer.DefaultServiceName ?? "grpc-server";
string operationName = tracer.CurrentTraceSettings.Schema.Server.GetOperationNameForProtocol("grpc");
scope = tracer.StartActiveInternal(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);
}
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId.Grpc);
}
catch (Exception ex)
{
Log.Error(ex, "Error creating server span for GRPC call");
}
return scope;
}
private static PropagationContext ExtractPropagatedContext(Tracer tracer, IMetadata? metadata)
{
try
{
if (metadata is not null)
{
return tracer.TracerManager.SpanContextPropagator.Extract(new MetadataHeadersCollection(metadata));
}
}
catch (Exception ex)
{
Log.Error(ex, "Error extracting propagated HTTP headers.");
}
return default;
}
}
}