Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.x' into w3c/munir/fix-system-t…
Browse files Browse the repository at this point in the history
…est-edgecases
  • Loading branch information
mabdinur committed Dec 19, 2022
2 parents f236fdc + 6cb75c6 commit 23c3517
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 59 deletions.
14 changes: 7 additions & 7 deletions ddtrace/internal/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ddtrace.internal.utils.cache import cached


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


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

current_tags_len = sum(len(i) for i in tags)
for k, v in context._meta.items():
Expand All @@ -176,8 +176,8 @@ def w3c_get_dd_list_member(context):
and k not in [SAMPLING_DECISION_TRACE_TAG_KEY, USER_ID_KEY]
):
# for key replace ",", "=", and characters outside the ASCII range 0x20 to 0x7E
# for value replace ",", ";", "~" and characters outside the ASCII range 0x20 to 0x7E
next_tag = "{}~{}".format(
# for value replace ",", ";", ":" and characters outside the ASCII range 0x20 to 0x7E
next_tag = "{}:{}".format(
re.sub("_dd.p.", "t.", re.sub(r",| |=|[^\x20-\x7E]+", "_", k)),
re.sub(_W3C_TRACESTATE_INVALID_CHARS_REGEX, "_", v),
)
Expand Down
4 changes: 2 additions & 2 deletions ddtrace/propagation/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,14 @@ def _get_traceparent_values(tp):
def _get_tracestate_values(ts):
# type: (str) -> Tuple[Optional[int], Dict[str, str], Optional[str]]

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

# parse out values
if dd:
Expand Down
17 changes: 17 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ Release Notes


.. ddtrace-release-notes::
"1.7.0":
ignore_notes:
- "Fix-version-constraints-for-Py27-e3a0069f43a84c16.yaml"
- "PCF-container-UUID-0f65eed47e42f4ad.yaml"
- "asm-1-click-activation-with-RCM-6a2431949768b73b.yaml"
- "asm-add-http-route-for-flask-8b13c721e2f7543b.yaml"
- "asm-fix-ipaddress-backport-lib-4684ce71e63459e7.yaml"
- "asm-ipaddress-rfc-part-two-a546e8453cc8163a.yaml"
- "asm-tag-http-query-string-c7faba44b1402215.yaml"
- "feat-dynamic-instrumentation-4c46bb86482438c1.yaml"
- "fix-context-serializable-2c9768cbe4738b73.yaml"
- "fix-cors-errors-0fa947a1a583ad0c.yaml"
- "iast-weak-hash-2fe50c2c7da2289d.yaml"
- "ipaddress-dep-3.7-b2bf42719e5f99ec.yaml"
- "python311-tracer-a7077c0e461622d2.yaml"
- "remove-deprecated-flask-methods-8d7a4a3d32c3c69d.yaml"
- "wsgi-span-parenting-fix-22908787ac7ee6a6.yaml"
"1.1.0":
ignore_notes:
# Ignore 1.0 release notes
Expand Down
32 changes: 16 additions & 16 deletions tests/tracer/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ def test_traceparent(context, expected_traceparent):
span_id=67667974448284343,
sampling_priority=1,
meta={
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE",
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
"_dd.p.dm": "-4",
"_dd.p.usr.id": "baz64",
},
dd_origin="rum",
),
"dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE",
"dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
),
(
Context(
Expand All @@ -219,21 +219,21 @@ def test_traceparent(context, expected_traceparent):
dd_origin="rum",
meta={"tracestate": "congo=t61rcWkgMzE"},
),
"dd=s~1;o~rum,congo=t61rcWkgMzE",
"dd=s:1;o:rum,congo=t61rcWkgMzE",
),
(
Context(
trace_id=11803532876627986230,
span_id=67667974448284343,
sampling_priority=2,
meta={
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
"_dd.p.dm": "-4",
"_dd.p.usr.id": "baz64",
},
dd_origin="synthetics",
),
"dd=s~2;o~synthetics;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
"dd=s:2;o:synthetics;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE,nr=ok,s=ink",
),
(
Context(
Expand All @@ -246,68 +246,68 @@ def test_traceparent(context, expected_traceparent):
},
dd_origin="synthetics",
),
"dd=s~-1;o~synthetics;t.dm~-4;t.usr.id~baz64",
"dd=s:-1;o:synthetics;t.dm:-4;t.usr.id:baz64",
),
(
Context(
trace_id=11803532876627986230,
span_id=67667974448284343,
sampling_priority=1,
meta={
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64,congo=t61rcWkgMzE",
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64,congo=t61rcWkgMzE",
"_dd.p.dm": "-4",
"_dd.p.usr.id": "baz64",
"_dd.p.unknown": "unk",
},
dd_origin="rum",
),
"dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64;t.unknown~unk,congo=t61rcWkgMzE",
"dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64;t.unknown:unk,congo=t61rcWkgMzE",
),
(
Context(),
"",
),
( # for value replace ",", ";", "~" and characters outside the ASCII range 0x20 to 0x7E with _
( # for value replace ",", ";", ":" and characters outside the ASCII range 0x20 to 0x7E with _
Context(
trace_id=11803532876627986230,
span_id=67667974448284343,
sampling_priority=1,
meta={
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64",
"_dd.p.dm": ";5~",
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64",
"_dd.p.dm": ";5:",
"_dd.p.usr.id": "b,z64,",
"_dd.p.unk": ";2",
},
dd_origin="rum",
),
"dd=s~1;o~rum;t.dm~_5_;t.usr.id~b_z64_;t.unk~_2",
"dd=s:1;o:rum;t.dm:_5_;t.usr.id:b_z64_;t.unk:_2",
),
( # for key replace ",", "=", and characters outside the ASCII range 0x20 to 0x7E with _
Context(
trace_id=11803532876627986230,
span_id=67667974448284343,
sampling_priority=1,
meta={
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64",
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64",
"_dd.p.dm": "5",
"_dd.p.usr.id": "bz64",
"_dd.p.unk¢": "2",
},
dd_origin="rum",
),
"dd=s~1;o~rum;t.dm~5;t.usr.id~bz64;t.unk_~2",
"dd=s:1;o:rum;t.dm:5;t.usr.id:bz64;t.unk_:2",
),
(
Context(
trace_id=11803532876627986230,
span_id=67667974448284343,
sampling_priority=1,
meta={
"tracestate": "dd=s~1;o~rum;t.dm~-4;t.usr.id~baz64",
"tracestate": "dd=s:1;o:rum;t.dm:-4;t.usr.id:baz64",
},
dd_origin=";r,um=",
),
"dd=s~1;o~_r_um_",
"dd=s:1;o:_r_um_",
),
],
ids=[
Expand Down
Loading

0 comments on commit 23c3517

Please sign in to comment.