-
-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathTelemetryPropagator.cs
More file actions
52 lines (42 loc) · 1.62 KB
/
TelemetryPropagator.cs
File metadata and controls
52 lines (42 loc) · 1.62 KB
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
namespace Core.OpenTelemetry;
public static class TelemetryPropagator
{
private static TextMapPropagator propagator = Propagators.DefaultTextMapPropagator;
public static void UseDefaultCompositeTextMapPropagator()
{
propagator =
new CompositeTextMapPropagator([
new TraceContextPropagator(), new BaggagePropagator()
]);
}
public static void Inject<T>(
this PropagationContext context,
T carrier,
Action<T, string, string> setter
) =>
propagator.Inject(context, carrier, setter);
public static PropagationContext Extract<T>(
T carrier,
Func<T, string, IEnumerable<string>> getter
) =>
propagator.Extract(default, carrier, getter);
public static PropagationContext Extract<T>(
PropagationContext context,
T carrier,
Func<T, string, IEnumerable<string>> getter
) =>
propagator.Extract(context, carrier, getter);
public static PropagationContext? Propagate<T>(this Activity? activity, T carrier, Action<T, string, string> setter)
{
if (activity?.Context == null) return null;
var propagationContext = new PropagationContext(activity.Context, Baggage.Current);
propagationContext.Inject(carrier, setter);
return propagationContext;
}
public static PropagationContext? GetPropagationContext(Activity? activity = null)
{
var activityContext = (activity?? Activity.Current)?.Context;
if (!activityContext.HasValue) return null;
return new PropagationContext(activityContext.Value, Baggage.Current);
}
}