Skip to content

Commit b417f85

Browse files
committed
factor out span processor creating in fastapi case
1 parent 1080ee0 commit b417f85

File tree

1 file changed

+10
-5
lines changed
  • packages/service-library/src/servicelib/fastapi

1 file changed

+10
-5
lines changed

packages/service-library/src/servicelib/fastapi/tracing.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
1414
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
1515
from opentelemetry.sdk.resources import Resource
16-
from opentelemetry.sdk.trace import TracerProvider
16+
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
1717
from opentelemetry.sdk.trace.export import BatchSpanProcessor
1818
from servicelib.logging_utils import log_context
1919
from servicelib.tracing import get_trace_id_header
@@ -72,6 +72,11 @@
7272
HAS_AIOPIKA_INSTRUMENTOR = False
7373

7474

75+
def _create_span_processor(tracing_destination: str) -> SpanProcessor:
76+
otlp_exporter = OTLPSpanExporterHTTP(endpoint=tracing_destination)
77+
return BatchSpanProcessor(otlp_exporter)
78+
79+
7580
def _startup(tracing_settings: TracingSettings, service_name: str) -> None:
7681
if (
7782
not tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT
@@ -98,10 +103,10 @@ def _startup(tracing_settings: TracingSettings, service_name: str) -> None:
98103
service_name,
99104
tracing_destination,
100105
)
101-
# Configure OTLP exporter to send spans to the collector
102-
otlp_exporter = OTLPSpanExporterHTTP(endpoint=tracing_destination)
103-
span_processor = BatchSpanProcessor(otlp_exporter)
104-
global_tracer_provider.add_span_processor(span_processor)
106+
# Add the span processor to the tracer provider
107+
global_tracer_provider.add_span_processor(
108+
_create_span_processor(tracing_destination)
109+
)
105110

106111
if HAS_AIOPG:
107112
with log_context(

0 commit comments

Comments
 (0)