11#!/usr/bin/env python3
22
33import os
4+ from collections .abc import Generator
45from contextlib import contextmanager
5- from typing import Generator
66
77from opentelemetry import trace
88from opentelemetry .exporter .otlp .proto .http .trace_exporter import OTLPSpanExporter
9- from opentelemetry .sdk .resources import SERVICE_NAME , DEPLOYMENT_ENVIRONMENT , Resource
9+ from opentelemetry .sdk .resources import DEPLOYMENT_ENVIRONMENT , SERVICE_NAME , Resource
1010from opentelemetry .sdk .trace import TracerProvider
1111from opentelemetry .sdk .trace .export import BatchSpanProcessor
1212
1717
1818MCP_SERVICE_NAME = "mcp"
1919
20- top_level_span : trace .Span | None = None
2120
22-
23- def get_trace_endpoint () -> (str , str ):
21+ def get_trace_endpoint () -> tuple [str , str ]:
2422 """Get the appropriate trace endpoint based on environment."""
2523 env = os .environ .get ("ENVIRONMENT" , "dev" ).lower ()
26-
24+
2725 if env == "prod" :
2826 return (DEFAULT_TRACE_ENDPOINT , "prod" )
2927 elif env == "local" :
3028 return (DEFAULT_LOCAL_ENDPOINT , "local" )
3129 else :
3230 return (DEFAULT_DEV_ENDPOINT , "dev" )
3331
32+
3433@contextmanager
35- def initialize_tracing (name : str ) -> Generator [trace .Span , None , None ]:
34+ def start_tracing (name : str ) -> Generator [trace .Span , None , None ]:
3635 """Initialize OpenTelemetry tracing."""
37-
3836 (endpoint , env ) = get_trace_endpoint ()
39-
37+
4038 # Create resource with basic attributes
41- resource = Resource .create ({
42- SERVICE_NAME : MCP_SERVICE_NAME ,
43- DEPLOYMENT_ENVIRONMENT : env ,
44- })
39+ resource = Resource .create (
40+ {
41+ SERVICE_NAME : MCP_SERVICE_NAME ,
42+ DEPLOYMENT_ENVIRONMENT : env ,
43+ }
44+ )
4545 # Create tracer provider
4646 provider = TracerProvider (resource = resource )
47-
47+
4848 # Create OTLP exporter
4949 exporter = OTLPSpanExporter (endpoint = endpoint )
50-
50+
5151 # Create span processor
5252 processor = BatchSpanProcessor (exporter )
5353 provider .add_span_processor (processor )
54-
54+
5555 # Set the global tracer provider
5656 trace .set_tracer_provider (provider )
57-
57+
5858 # Get tracer instance
5959 tracer = trace .get_tracer (MCP_SERVICE_NAME )
6060
6161 with tracer .start_as_current_span (name ) as span :
62- top_level_span = span
63- trace_id = trace .format_trace_id (top_level_span .get_span_context ().trace_id )
62+ trace_id = trace .format_trace_id (span .get_span_context ().trace_id )
6463 # TODO: use logging
6564 print ("Tracing initialized" )
6665 print (f"Tracing initialized with trace ID: { trace_id } " )
67-
66+
6867 yield span
6968
7069
7170@contextmanager
72- def trace_span (
73- name : str ,
71+ def with_span (
72+ parent_span : trace .Span ,
73+ name : str ,
7474) -> Generator [trace .Span , None , None ]:
7575 tracer = trace .get_tracer (MCP_SERVICE_NAME )
76-
77- with tracer .start_as_current_span (name ) as span :
78- yield span
76+
77+ context = trace .set_span_in_context (parent_span )
78+ with tracer .start_span (name , context = context ) as span :
79+ yield span
0 commit comments