-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing.py
More file actions
55 lines (44 loc) · 1.69 KB
/
Copy pathtracing.py
File metadata and controls
55 lines (44 loc) · 1.69 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
49
50
51
52
53
54
55
"""
tracing.py —
OpenTelemetry distributed tracing for ReFrame bot.
Optional — enabled via FF_TRACING=true environment variable.
"""
import logging
from config import FEATURE_FLAGS, CONFIG
logger = logging.getLogger(__name__)
_tracer = None
def init_tracing(service_name: str = "reframe-bot"):
global _tracer
if not FEATURE_FLAGS.get("enable_tracing"):
return
try:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import Resource
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
resource = Resource.create({"service.name": service_name})
provider = TracerProvider(resource=resource)
exporter = OTLPSpanExporter()
provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(provider)
_tracer = trace.get_tracer(service_name)
logger.info(f"OpenTelemetry tracing initialized → {service_name}")
except ImportError:
logger.warning("opentelemetry packages not installed — tracing disabled")
except Exception as exc:
logger.error(f"Tracing init failed: {exc}")
def trace_span(name: str):
"""Context manager for creating a trace span. No-op if tracing disabled."""
if _tracer is None:
return _NoopSpan()
return _tracer.start_as_current_span(name)
class _NoopSpan:
def __enter__(self):
return self
def __exit__(self, *args):
pass
def set_attribute(self, key, value):
pass
def add_event(self, name, attributes=None):
pass