Skip to content

Commit cc4bac9

Browse files
authored
Revert "chore(w3c): allow colons in datadog tags propagated in tracestate" (backport #4799 to 1.7) (#4804)
Reverts #4741. `:` should delimit datadog tracestate tags not `~`. #### Note When injecting/extracting ddtrace tags from headers `=` characters should be replaced with `~`. This fix will be introduced in a future PR.
1 parent 1a308d1 commit cc4bac9

File tree

5 files changed

+59
-59
lines changed

5 files changed

+59
-59
lines changed

ddtrace/internal/utils/http.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from ddtrace.internal.utils.cache import cached
2020

2121

22-
_W3C_TRACESTATE_INVALID_CHARS_REGEX = r",|;|~|[^\x20-\x7E]+"
22+
_W3C_TRACESTATE_INVALID_CHARS_REGEX = r",|;|:|[^\x20-\x7E]+"
2323

2424

2525
Connector = Callable[[], ContextManager[compat.httplib.HTTPConnection]]
@@ -155,17 +155,17 @@ def w3c_get_dd_list_member(context):
155155
# Context -> str
156156
tags = []
157157
if context.sampling_priority is not None:
158-
tags.append("{}~{}".format(W3C_TRACESTATE_SAMPLING_PRIORITY_KEY, context.sampling_priority))
158+
tags.append("{}:{}".format(W3C_TRACESTATE_SAMPLING_PRIORITY_KEY, context.sampling_priority))
159159
if context.dd_origin:
160160
# the origin value has specific values that are allowed.
161-
tags.append("{}~{}".format(W3C_TRACESTATE_ORIGIN_KEY, re.sub(r",|;|=|[^\x20-\x7E]+", "_", context.dd_origin)))
161+
tags.append("{}:{}".format(W3C_TRACESTATE_ORIGIN_KEY, re.sub(r",|;|=|[^\x20-\x7E]+", "_", context.dd_origin)))
162162
sampling_decision = context._meta.get(SAMPLING_DECISION_TRACE_TAG_KEY)
163163
if sampling_decision:
164-
tags.append("t.dm~{}".format(re.sub(_W3C_TRACESTATE_INVALID_CHARS_REGEX, "_", sampling_decision)))
164+
tags.append("t.dm:{}".format(re.sub(_W3C_TRACESTATE_INVALID_CHARS_REGEX, "_", sampling_decision)))
165165
# since this can change, we need to grab the value off the current span
166166
usr_id_key = context._meta.get(USER_ID_KEY)
167167
if usr_id_key:
168-
tags.append("t.usr.id~{}".format(re.sub(_W3C_TRACESTATE_INVALID_CHARS_REGEX, "_", usr_id_key)))
168+
tags.append("t.usr.id:{}".format(re.sub(_W3C_TRACESTATE_INVALID_CHARS_REGEX, "_", usr_id_key)))
169169

170170
current_tags_len = sum(len(i) for i in tags)
171171
for k, v in context._meta.items():
@@ -176,8 +176,8 @@ def w3c_get_dd_list_member(context):
176176
and k not in [SAMPLING_DECISION_TRACE_TAG_KEY, USER_ID_KEY]
177177
):
178178
# for key replace ",", "=", and characters outside the ASCII range 0x20 to 0x7E
179-
# for value replace ",", ";", "~" and characters outside the ASCII range 0x20 to 0x7E
180-
next_tag = "{}~{}".format(
179+
# for value replace ",", ";", ":" and characters outside the ASCII range 0x20 to 0x7E
180+
next_tag = "{}:{}".format(
181181
re.sub("_dd.p.", "t.", re.sub(r",| |=|[^\x20-\x7E]+", "_", k)),
182182
re.sub(_W3C_TRACESTATE_INVALID_CHARS_REGEX, "_", v),
183183
)

ddtrace/propagation/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,14 +586,14 @@ def _get_traceparent_values(tp):
586586
def _get_tracestate_values(ts):
587587
# type: (str) -> Tuple[Optional[int], Dict[str, str], Optional[str]]
588588

589-
# tracestate parsing, example: dd=s~2;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE
589+
# tracestate parsing, example: dd=s:2;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE
590590
dd = None
591591
ts_l = ts.strip().split(",")
592592
for list_mem in ts_l:
593593
if list_mem.startswith("dd="):
594594
# cut out dd= before turning into dict
595595
list_mem = list_mem[3:]
596-
dd = dict(item.split("~") for item in list_mem.split(";"))
596+
dd = dict(item.split(":") for item in list_mem.split(";"))
597597

598598
# parse out values
599599
if dd:

tests/tracer/test_context.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ def test_traceparent(context, expected_traceparent):
203203
span_id=67667974448284343,
204204
sampling_priority=1,
205205
meta={
206-
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE",
206+
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
207207
"_dd.p.dm": "-4",
208208
"_dd.p.usr.id": "baz64",
209209
},
210210
dd_origin="rum",
211211
),
212-
"dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE",
212+
"dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
213213
),
214214
(
215215
Context(
@@ -219,21 +219,21 @@ def test_traceparent(context, expected_traceparent):
219219
dd_origin="rum",
220220
meta={"tracestate": "congo=t61rcWkgMzE"},
221221
),
222-
"dd=s~1;o~rum,congo=t61rcWkgMzE",
222+
"dd=s:1;o:rum,congo=t61rcWkgMzE",
223223
),
224224
(
225225
Context(
226226
trace_id=11803532876627986230,
227227
span_id=67667974448284343,
228228
sampling_priority=2,
229229
meta={
230-
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
230+
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
231231
"_dd.p.dm": "-4",
232232
"_dd.p.usr.id": "baz64",
233233
},
234234
dd_origin="synthetics",
235235
),
236-
"dd=s~2;o~synthetics;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
236+
"dd=s:2;o:synthetics;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
237237
),
238238
(
239239
Context(
@@ -246,68 +246,68 @@ def test_traceparent(context, expected_traceparent):
246246
},
247247
dd_origin="synthetics",
248248
),
249-
"dd=s~-1;o~synthetics;t.dm~-4;t.usr.id~baz64",
249+
"dd=s:-1;o:synthetics;t.dm:-4;t.usr.id:baz64",
250250
),
251251
(
252252
Context(
253253
trace_id=11803532876627986230,
254254
span_id=67667974448284343,
255255
sampling_priority=1,
256256
meta={
257-
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE",
257+
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
258258
"_dd.p.dm": "-4",
259259
"_dd.p.usr.id": "baz64",
260260
"_dd.p.unknown": "unk",
261261
},
262262
dd_origin="rum",
263263
),
264-
"dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64;t.unknown~unk,congo=t61rcWkgMzE",
264+
"dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64;t.unknown:unk,congo=t61rcWkgMzE",
265265
),
266266
(
267267
Context(),
268268
"",
269269
),
270-
( # for value replace ",", ";", "~" and characters outside the ASCII range 0x20 to 0x7E with _
270+
( # for value replace ",", ";", ":" and characters outside the ASCII range 0x20 to 0x7E with _
271271
Context(
272272
trace_id=11803532876627986230,
273273
span_id=67667974448284343,
274274
sampling_priority=1,
275275
meta={
276-
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64",
277-
"_dd.p.dm": ";5~",
276+
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64",
277+
"_dd.p.dm": ";5:",
278278
"_dd.p.usr.id": "b,z64,",
279279
"_dd.p.unk": ";2",
280280
},
281281
dd_origin="rum",
282282
),
283-
"dd=s~1;o~rum;t.dm~_5_;t.usr.id~b_z64_;t.unk~_2",
283+
"dd=s:1;o:rum;t.dm:_5_;t.usr.id:b_z64_;t.unk:_2",
284284
),
285285
( # for key replace ",", "=", and characters outside the ASCII range 0x20 to 0x7E with _
286286
Context(
287287
trace_id=11803532876627986230,
288288
span_id=67667974448284343,
289289
sampling_priority=1,
290290
meta={
291-
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64",
291+
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64",
292292
"_dd.p.dm": "5",
293293
"_dd.p.usr.id": "bz64",
294294
"_dd.p.unk¢": "2",
295295
},
296296
dd_origin="rum",
297297
),
298-
"dd=s~1;o~rum;t.dm~5;t.usr.id~bz64;t.unk_~2",
298+
"dd=s:1;o:rum;t.dm:5;t.usr.id:bz64;t.unk_:2",
299299
),
300300
(
301301
Context(
302302
trace_id=11803532876627986230,
303303
span_id=67667974448284343,
304304
sampling_priority=1,
305305
meta={
306-
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64",
306+
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64",
307307
},
308308
dd_origin=";r,um=",
309309
),
310-
"dd=s~1;o~_r_um_",
310+
"dd=s:1;o:_r_um_",
311311
),
312312
],
313313
ids=[

0 commit comments

Comments
 (0)