Skip to content

Commit 8e3a187

Browse files
committed
add sampling based strategy to fast api tracing instrumentation
1 parent 29c5cbc commit 8e3a187

File tree

1 file changed

+6
-1
lines changed
  • packages/service-library/src/servicelib/fastapi

1 file changed

+6
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from opentelemetry.sdk.resources import Resource
1616
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
1717
from opentelemetry.sdk.trace.export import BatchSpanProcessor
18+
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased
1819
from settings_library.tracing import TracingSettings
1920
from starlette.middleware.base import BaseHTTPMiddleware
2021
from yarl import URL
@@ -87,7 +88,11 @@ def _startup(tracing_settings: TracingSettings, service_name: str) -> None:
8788
return
8889
# Set up the tracer provider
8990
resource = Resource(attributes={"service.name": service_name})
90-
trace.set_tracer_provider(TracerProvider(resource=resource))
91+
sampler = ParentBased(
92+
root=TraceIdRatioBased(tracing_settings.TRACING_SAMPLING_PROBABILITY)
93+
)
94+
trace_provider = TracerProvider(resource=resource, sampler=sampler)
95+
trace.set_tracer_provider(trace_provider)
9196
global_tracer_provider = trace.get_tracer_provider()
9297
assert isinstance(global_tracer_provider, TracerProvider) # nosec
9398

0 commit comments

Comments
 (0)