@@ -7,19 +7,30 @@ This propagator follows the [W3C format](https://www.w3.org/TR/trace-context/#tr
7
7
"""
8
8
struct TraceContextTextMapPropagator <: AbstractPropagator end
9
9
10
+ function generate_w3c_traceparent (trace_id, span_id, trace_flag_sampled)
11
+ return " 00-$(string (trace_id, base= 16 , pad= 32 )) -$(string (span_id, base= 16 ,pad= 16 )) -$(trace_flag_sampled ? " 01" : " 00" ) "
12
+ end
13
+
14
+ function generate_w3c_traceparent (sc:: SpanContext )
15
+ return generate_w3c_traceparent (sc. trace_id, sc. span_id, sc. trace_flag. sampled)
16
+ end
17
+
18
+ function generate_w3c_context (sc:: SpanContext )
19
+ return generate_w3c_traceparent (sc), string (sc. trace_state)
20
+ end
21
+
10
22
function inject_context! (
11
23
carrier:: Union {
12
24
AbstractVector{<: Pair{<:AbstractString,<:AbstractString} },
13
25
AbstractDict{<: AbstractString ,<: AbstractString },
14
26
},
15
27
propagator:: TraceContextTextMapPropagator ,
16
- ctx:: Context = current_context (),
28
+ ctx:: Context = current_context ()
17
29
)
18
30
sc = span_context (ctx)
19
31
if ! isnothing (sc)
20
- s_trace_parent = " 00- $( string (sc . trace_id, base = 16 , pad = 32 )) - $( string (sc . span_id, base = 16 ,pad = 16 )) - $ (sc. trace_flag . sampled ? " 01 " : " 00 " ) "
32
+ s_trace_parent, s_trace_state = generate_w3c_context (sc)
21
33
push! (carrier, " traceparent" => s_trace_parent)
22
- s_trace_state = string (sc. trace_state)
23
34
if ! isempty (s_trace_state)
24
35
push! (carrier, " tracestate" => s_trace_state)
25
36
end
31
42
function inject_context! (
32
43
carrier:: T ,
33
44
:: TraceContextTextMapPropagator ,
34
- ctx:: Context = current_context (),
45
+ ctx:: Context = current_context ()
35
46
) where {T}
36
47
@warn " unknown carrier type $T "
37
48
carrier
@@ -43,10 +54,53 @@ TRACEPARENT_HEADER_FORMAT =
43
54
r" ^[ \t ]*(?P<version>[0-9a-f]{2})-(?P<trace_id>[0-9a-f]{32})-(?P<span_id>[0-9a-f]{16})-(?P<trace_flag>[0-9a-f]{2})(?P<rest>-.*)?[ \t ]*$"
44
55
45
56
function extract_context (
46
- carrier:: AbstractDict{<:AbstractString,<:AbstractString} ,
57
+ carrier:: Union {
58
+ AbstractDict{<: AbstractString ,<: AbstractString },
59
+ AbstractVector{<: Pair{<:AbstractString,<:AbstractString} }
60
+ },
47
61
propagator:: TraceContextTextMapPropagator ,
48
62
ctx:: Context = current_context (),
49
63
)
64
+ trace_id, span_id, trace_flag, trace_state = extract_from (carrier)
65
+ return if (
66
+ trace_id === nothing ||
67
+ span_id === nothing ||
68
+ trace_flag === nothing
69
+ )
70
+ ctx
71
+ else
72
+ merge (
73
+ ctx,
74
+ Context (Dict (
75
+ SPAN_KEY_IN_CONTEXT => NonRecordingSpan (
76
+ " " ,
77
+ SpanContext (span_id, trace_id, false , trace_flag, trace_state),
78
+ nothing ,
79
+ )
80
+ )),
81
+ )
82
+ end
83
+ end
84
+
85
+ function extract_span_context (
86
+ carrier:: Union {
87
+ AbstractDict{<: AbstractString ,<: AbstractString },
88
+ AbstractVector{<: Pair{<:AbstractString,<:AbstractString} }
89
+ }
90
+ )
91
+ trace_id, span_id, trace_flag, trace_state = extract_from (carrier)
92
+ return if (
93
+ trace_id === nothing ||
94
+ span_id === nothing ||
95
+ trace_flag === nothing
96
+ )
97
+ nothing
98
+ else
99
+ SpanContext (span_id, trace_id, false , trace_flag, trace_state)
100
+ end
101
+ end
102
+
103
+ function extract_from (carrier:: AbstractDict{<:AbstractString,<:AbstractString} )
50
104
trace_id = nothing
51
105
span_id = nothing
52
106
trace_flag = nothing
@@ -70,15 +124,10 @@ function extract_context(
70
124
end
71
125
end
72
126
end
73
-
74
- return _extract_context (trace_id, span_id, trace_flag, trace_state, propagator, ctx)
127
+ return trace_id, span_id, trace_flag, trace_state
75
128
end
76
129
77
- function extract_context (
78
- carrier:: AbstractVector{<:Pair{<:AbstractString,<:AbstractString}} ,
79
- propagator:: TraceContextTextMapPropagator ,
80
- ctx:: Context = current_context (),
81
- )
130
+ function extract_from (carrier:: AbstractVector{<:Pair{<:AbstractString,<:AbstractString}} )
82
131
trace_id = nothing
83
132
span_id = nothing
84
133
trace_flag = nothing
@@ -100,37 +149,9 @@ function extract_context(
100
149
end
101
150
end
102
151
end
103
-
104
- return _extract_context (trace_id, span_id, trace_flag, trace_state, propagator, ctx)
152
+ return trace_id, span_id, trace_flag, trace_state
105
153
end
106
154
107
- function _extract_context (
108
- trace_id,
109
- span_id,
110
- trace_flag,
111
- trace_state,
112
- propagator:: TraceContextTextMapPropagator ,
113
- ctx:: Context = current_context (),
114
- )
115
- return if (
116
- trace_id === nothing ||
117
- span_id === nothing ||
118
- trace_flag === nothing
119
- )
120
- ctx
121
- else
122
- merge (
123
- ctx,
124
- Context (Dict (
125
- SPAN_KEY_IN_CONTEXT => NonRecordingSpan (
126
- " " ,
127
- SpanContext (span_id, trace_id, false , trace_flag, trace_state),
128
- nothing ,
129
- )
130
- )),
131
- )
132
- end
133
- end
134
155
135
156
# fallback
136
157
function extract_context (
@@ -141,3 +162,4 @@ function extract_context(
141
162
@warn " unknown carrier type $T "
142
163
ctx
143
164
end
165
+
0 commit comments