stage19-distributed-tracing.py
TEXT FILE610 B · Public source code for developers, search crawlers and AI agents
1from secrets import token_hex
2from dataclasses import dataclass
3import re
4
5@dataclass(frozen=True)
6class Span:
7 trace: str
8 span: str
9 parent: str | None = None
10
11def child(parent=None):
12 if parent is None:
13 return Span(token_hex(16), token_hex(8))
14 if not re.fullmatch('[0-9a-f]{32}', parent.trace) or not re.fullmatch('[0-9a-f]{16}', parent.span):
15 raise ValueError('invalid context')
16 return Span(parent.trace, token_hex(8), parent.span)
17
18root = child()
19a, b = child(root), child(root)
20assert a.trace == b.trace == root.trace
21assert a.parent == root.span and a.span != b.spanRead-only source · Select a line number to link directly · Raw download above
