Skip to content

Commit c255809

Browse files
committed
Handle span parent_id being undefined
Use the new [`TypedDict`](https://peps.python.org/pep-0655/) implementation that supports `NotRequired` and `Required` to better type the data that the agent can receive from clients. Fixes #65.
1 parent 6f2954c commit c255809

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

ddapm_test_agent/trace.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from typing import Optional
1010
from typing import OrderedDict
1111
from typing import Tuple
12-
from typing import TypedDict
1312
from typing import Union
1413
from typing import cast
1514

1615
import msgpack
16+
from typing_extensions import NotRequired
17+
from typing_extensions import TypedDict
1718

1819

1920
SpanId = int
@@ -46,19 +47,19 @@
4647
MetricType = Union[int, float]
4748

4849

49-
class Span(TypedDict, total=False):
50+
class Span(TypedDict):
5051
name: str
5152
span_id: SpanId
5253
trace_id: TraceId
5354
start: int
5455
duration: int
55-
parent_id: int # TODO: is this actually optional...it could be?
56-
service: Optional[str]
57-
resource: Optional[str]
58-
type: Optional[str] # noqa
59-
error: Optional[int]
60-
meta: Dict[str, str]
61-
metrics: Dict[str, MetricType]
56+
parent_id: NotRequired[Optional[int]]
57+
service: NotRequired[Optional[str]]
58+
resource: NotRequired[Optional[str]]
59+
type: NotRequired[Optional[str]] # noqa
60+
error: NotRequired[Optional[int]]
61+
meta: NotRequired[Dict[str, str]]
62+
metrics: NotRequired[Dict[str, MetricType]]
6263

6364

6465
SpanAttr = Literal[
@@ -140,10 +141,12 @@ def child_map(trace: Trace) -> Dict[int, List[Span]]:
140141
# Initialize the map with all possible ids
141142
for s in trace:
142143
cmap[s["span_id"]] = []
143-
cmap[s["parent_id"]] = []
144+
parent_id = s.get("parent_id") or 0
145+
cmap[parent_id] = []
144146

145147
for s in trace:
146-
cmap[s["parent_id"]].append(s)
148+
parent_id = s.get("parent_id") or 0
149+
cmap[parent_id].append(s)
147150

148151
# Sort the children ascending by their start time
149152
for span_id in cmap:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Handle span ``parent_id`` being undefined.

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"aiohttp",
3131
"ddsketch",
3232
"msgpack",
33+
"typing_extensions",
3334
],
3435
tests_require=testing_deps,
3536
setup_requires=["setuptools_scm"],

tests/test_trace.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def test_decode_v04_bad(content_type, payload):
5959
@pytest.mark.parametrize(
6060
"trace",
6161
[
62+
[{"name": "root"}],
6263
[{"name": "root", "parent_id": 0}],
6364
[{"name": "root", "parent_id": None}],
6465
[{"name": "root"}],
@@ -81,6 +82,10 @@ def test_root_span(trace):
8182
[{"span_id": 1, "parent_id": 0, "start": 0}],
8283
[{"span_id": 1, "parent_id": 0, "start": 0}],
8384
),
85+
(
86+
[{"span_id": 1, "start": 0}],
87+
[{"span_id": 1, "start": 0}],
88+
),
8489
(
8590
[
8691
{"span_id": 2, "parent_id": 1, "start": 1},

0 commit comments

Comments
 (0)