spakky-tracing¶
분산 트레이싱 추상화 — TraceContext, ITracePropagator, W3C Propagator
컨텍스트¶
spakky.tracing.context
¶
TraceContext — contextvars-based distributed trace context.
TraceContext
¶
W3C Trace Context Level 2 compatible trace context.
Attributes:
| Name | Type | Description |
|---|---|---|
trace_id |
str
|
32-character hex string (128-bit). |
span_id |
str
|
16-character hex string (64-bit). |
parent_span_id |
str | None
|
Parent span ID, or None for root spans. |
trace_flags |
int
|
Trace flags (0x01 = sampled). |
generate_trace_id()
classmethod
¶
generate_span_id()
classmethod
¶
to_traceparent()
¶
Serialize to W3C traceparent header format.
Returns:
| Type | Description |
|---|---|
str
|
Traceparent string: |
Source code in core/spakky-tracing/src/spakky/tracing/context.py
from_traceparent(header)
classmethod
¶
Parse a W3C traceparent header.
Format: {version}-{trace_id}-{span_id}-{trace_flags}
Example: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header
|
str
|
The traceparent header value. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
Parsed TraceContext. |
Raises:
| Type | Description |
|---|---|
InvalidTraceparentError
|
If the header format is invalid. |
Source code in core/spakky-tracing/src/spakky/tracing/context.py
new_root()
classmethod
¶
Create a new root trace (generates trace_id and span_id).
Returns:
| Type | Description |
|---|---|
Self
|
A new TraceContext with fresh IDs. |
Source code in core/spakky-tracing/src/spakky/tracing/context.py
child()
¶
Create a child span under this context.
Returns:
| Type | Description |
|---|---|
Self
|
A new TraceContext sharing the same trace_id, with a new span_id |
Self
|
and parent_span_id set to the current span_id. |
Source code in core/spakky-tracing/src/spakky/tracing/context.py
get()
classmethod
¶
Get the current execution context's TraceContext.
Returns:
| Type | Description |
|---|---|
Self | None
|
The current TraceContext, or None if not set. |
set(ctx)
classmethod
¶
Set the TraceContext for the current execution context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Self
|
The TraceContext to set. |
required |
options: show_root_heading: false
Propagator 인터페이스¶
spakky.tracing.propagator
¶
ITracePropagator — trace context propagation interface.
ITracePropagator
¶
Bases: ABC
Interface for trace context propagation across service boundaries.
Implementations read/write the ambient TraceContext (stored in
contextvars) from/to carrier dictionaries such as HTTP headers
or message metadata.
Terminology follows the OpenTelemetry TextMapPropagator convention:
- inject — read the ambient
TraceContextand write it into an outbound carrier. Does not create a new trace; it serializes whatever context is currently active. If no context is active the carrier is left unchanged. - extract — read an inbound carrier and reconstruct a
TraceContext. The caller is responsible for activating it viaTraceContext.set().
inject(carrier)
abstractmethod
¶
Read the ambient TraceContext and write it into the carrier.
This is an upsert on the carrier: if a context is active its header fields are added (or overwritten); if no context is active the carrier is left untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrier
|
dict[str, str]
|
Mutable header dictionary. After the call, keys such
as |
required |
Source code in core/spakky-tracing/src/spakky/tracing/propagator.py
extract(carrier)
abstractmethod
¶
Reconstruct a TraceContext from the carrier.
The returned context is not automatically activated. Call
TraceContext.set() to make it the ambient context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrier
|
dict[str, str]
|
Read-only header dictionary. |
required |
Returns:
| Type | Description |
|---|---|
TraceContext | None
|
The restored TraceContext, or None if the required headers are |
TraceContext | None
|
missing or malformed. |
Source code in core/spakky-tracing/src/spakky/tracing/propagator.py
fields()
abstractmethod
¶
Return the header field names used by this propagator.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of header names, e.g., |
options: show_root_heading: false
W3C Propagator¶
spakky.tracing.w3c_propagator
¶
W3CTracePropagator — W3C Trace Context Level 2 propagator.
W3CTracePropagator
¶
Bases: ITracePropagator
W3C Trace Context Level 2 standard propagator.
Pure Python implementation with no external dependencies.
traceparent format: {version:2}-{trace_id:32}-{span_id:16}-{trace_flags:2}
inject(carrier)
¶
Write the current TraceContext as a traceparent header into the carrier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrier
|
dict[str, str]
|
Mutable header dictionary. |
required |
Source code in core/spakky-tracing/src/spakky/tracing/w3c_propagator.py
extract(carrier)
¶
Restore a TraceContext from the traceparent header in the carrier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrier
|
dict[str, str]
|
Read-only header dictionary. |
required |
Returns:
| Type | Description |
|---|---|
TraceContext | None
|
The restored TraceContext, or None if the header is missing or invalid. |
Source code in core/spakky-tracing/src/spakky/tracing/w3c_propagator.py
fields()
¶
Return the header field names used by this propagator.
Returns:
| Type | Description |
|---|---|
list[str]
|
|
options: show_root_heading: false
에러¶
spakky.tracing.error
¶
options: show_root_heading: false