forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.py
More file actions
48 lines (38 loc) · 1.38 KB
/
telemetry.py
File metadata and controls
48 lines (38 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""OpenTelemetry trace context helpers for Copilot SDK."""
from __future__ import annotations
from collections.abc import Generator
from contextlib import contextmanager
def get_trace_context() -> dict[str, str]:
"""Get the current W3C Trace Context (traceparent/tracestate) if OpenTelemetry is available."""
try:
from opentelemetry import context, propagate
except ImportError:
return {}
carrier: dict[str, str] = {}
propagate.inject(carrier, context=context.get_current())
result: dict[str, str] = {}
if "traceparent" in carrier:
result["traceparent"] = carrier["traceparent"]
if "tracestate" in carrier:
result["tracestate"] = carrier["tracestate"]
return result
@contextmanager
def trace_context(traceparent: str | None, tracestate: str | None) -> Generator[None, None, None]:
"""Context manager that sets the trace context from W3C headers for the block's duration."""
try:
from opentelemetry import context, propagate
except ImportError:
yield
return
if not traceparent:
yield
return
carrier: dict[str, str] = {"traceparent": traceparent}
if tracestate:
carrier["tracestate"] = tracestate
ctx = propagate.extract(carrier, context=context.get_current())
token = context.attach(ctx)
try:
yield
finally:
context.detach(token)