Skip to content

Commit d1aba9b

Browse files
committed
propagate tracing_data
1 parent bbc5d12 commit d1aba9b

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Final
66

77
from aiohttp import web
8-
from opentelemetry import trace
98
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
109
OTLPSpanExporter as OTLPSpanExporterHTTP,
1110
)
@@ -15,15 +14,13 @@
1514
from opentelemetry.instrumentation.aiohttp_server import (
1615
middleware as aiohttp_server_opentelemetry_middleware, # pylint:disable=no-name-in-module
1716
)
18-
from opentelemetry.sdk.resources import Resource
19-
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
17+
from opentelemetry.sdk.trace import SpanProcessor
2018
from opentelemetry.sdk.trace.export import BatchSpanProcessor
21-
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased
2219
from settings_library.tracing import TracingSettings
2320
from yarl import URL
2421

2522
from ..logging_utils import log_context
26-
from ..tracing import get_trace_id_header
23+
from ..tracing import TracingData, get_trace_id_header
2724

2825
_logger = logging.getLogger(__name__)
2926
try:
@@ -77,7 +74,7 @@ def _startup(
7774
*,
7875
app: web.Application,
7976
tracing_settings: TracingSettings,
80-
service_name: str,
77+
tracing_data: TracingData,
8178
add_response_trace_id_header: bool = False,
8279
) -> None:
8380
"""
@@ -100,26 +97,19 @@ def _startup(
10097
"unset. Provide both or remove both."
10198
)
10299
raise RuntimeError(msg)
103-
resource = Resource(attributes={"service.name": service_name})
104-
sampler = ParentBased(
105-
root=TraceIdRatioBased(tracing_settings.TRACING_SAMPLING_PROBABILITY)
106-
)
107-
tracer_provider = TracerProvider(resource=resource, sampler=sampler)
108-
trace.set_tracer_provider(tracer_provider=tracer_provider)
109-
tracer_provider: trace.TracerProvider = trace.get_tracer_provider()
110100

111101
tracing_destination: str = (
112102
f"{URL(opentelemetry_collector_endpoint).with_port(opentelemetry_collector_port).with_path('/v1/traces')}"
113103
)
114104

115105
_logger.info(
116106
"Trying to connect service %s to tracing collector at %s.",
117-
service_name,
107+
tracing_data.service_name,
118108
tracing_destination,
119109
)
120110

121111
# Add the span processor to the tracer provider
122-
tracer_provider.add_span_processor(_create_span_processor(tracing_destination)) # type: ignore[attr-defined] # https://github.com/open-telemetry/opentelemetry-python/issues/3713
112+
tracing_data.tracer_provider.add_span_processor(_create_span_processor(tracing_destination)) # type: ignore[attr-defined] # https://github.com/open-telemetry/opentelemetry-python/issues/3713
123113
# Instrument aiohttp server
124114
# Explanation for custom middleware call DK 10/2024:
125115
# OpenTelemetry Aiohttp autoinstrumentation is meant to be used by only calling `AioHttpServerInstrumentor().instrument()`
@@ -140,43 +130,51 @@ def _startup(
140130
# - opentelemetry-instrumentation==0.48b0
141131

142132
# Instrument aiohttp client
143-
AioHttpClientInstrumentor().instrument()
133+
AioHttpClientInstrumentor().instrument(tracer_provier=tracing_data.tracer_provider)
144134
if HAS_AIOPG:
145135
with log_context(
146136
_logger,
147137
logging.INFO,
148138
msg="Attempting to add aio-pg opentelemetry autoinstrumentation...",
149139
):
150-
AiopgInstrumentor().instrument()
140+
AiopgInstrumentor().instrument(tracer_provider=tracing_data.tracer_provider)
151141
if HAS_ASYNCPG:
152142
with log_context(
153143
_logger,
154144
logging.INFO,
155145
msg="Attempting to add asyncpg opentelemetry autoinstrumentation...",
156146
):
157-
AsyncPGInstrumentor().instrument()
147+
AsyncPGInstrumentor().instrument(
148+
tracer_provider=tracing_data.tracer_provider
149+
)
158150
if HAS_BOTOCORE:
159151
with log_context(
160152
_logger,
161153
logging.INFO,
162154
msg="Attempting to add botocore opentelemetry autoinstrumentation...",
163155
):
164-
BotocoreInstrumentor().instrument()
156+
BotocoreInstrumentor().instrument(
157+
tracer_provider=tracing_data.tracer_provider
158+
)
165159
if HAS_REQUESTS:
166160
with log_context(
167161
_logger,
168162
logging.INFO,
169163
msg="Attempting to add requests opentelemetry autoinstrumentation...",
170164
):
171-
RequestsInstrumentor().instrument()
165+
RequestsInstrumentor().instrument(
166+
tracer_provider=tracing_data.tracer_provider
167+
)
172168

173169
if HAS_AIO_PIKA:
174170
with log_context(
175171
_logger,
176172
logging.INFO,
177173
msg="Attempting to add aio_pika opentelemetry autoinstrumentation...",
178174
):
179-
AioPikaInstrumentor().instrument()
175+
AioPikaInstrumentor().instrument(
176+
tracer_provider=tracing_data.tracer_provider
177+
)
180178

181179

182180
@web.middleware
@@ -194,8 +192,9 @@ async def response_trace_id_header_middleware(request: web.Request, handler):
194192
return response
195193

196194

197-
def _shutdown() -> None:
195+
def _shutdown(tracing_data: TracingData) -> None:
198196
"""Uninstruments all opentelemetry instrumentors that were instrumented."""
197+
assert tracing_data # nosec
199198
try:
200199
AioHttpClientInstrumentor().uninstrument()
201200
except Exception: # pylint:disable=broad-exception-caught
@@ -231,19 +230,19 @@ def get_tracing_lifespan(
231230
*,
232231
app: web.Application,
233232
tracing_settings: TracingSettings,
234-
service_name: str,
233+
tracing_data: TracingData,
235234
add_response_trace_id_header: bool = False,
236235
) -> Callable[[web.Application], AsyncIterator]:
237236
_startup(
238237
app=app,
239238
tracing_settings=tracing_settings,
240-
service_name=service_name,
239+
tracing_data=tracing_data,
241240
add_response_trace_id_header=add_response_trace_id_header,
242241
)
243242

244243
async def tracing_lifespan(app: web.Application):
245244
assert app # nosec
246245
yield
247-
_shutdown()
246+
_shutdown(tracing_data=tracing_data)
248247

249248
return tracing_lifespan

packages/service-library/tests/aiohttp/test_tracing.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
1717
from pydantic import ValidationError
1818
from servicelib.aiohttp.tracing import get_tracing_lifespan
19-
from servicelib.tracing import _OSPARC_TRACE_ID_HEADER
19+
from servicelib.tracing import _OSPARC_TRACE_ID_HEADER, TracingData
2020
from settings_library.tracing import TracingSettings
2121

2222

@@ -70,8 +70,11 @@ async def test_valid_tracing_settings(
7070
app = web.Application()
7171
service_name = "simcore_service_webserver"
7272
tracing_settings = TracingSettings()
73+
tracing_data = TracingData.create(
74+
tracing_settings=tracing_settings, service_name=service_name
75+
)
7376
async for _ in get_tracing_lifespan(
74-
app=app, service_name=service_name, tracing_settings=tracing_settings
77+
app=app, tracing_settings=tracing_settings, tracing_data=tracing_data
7578
)(app):
7679
pass
7780

@@ -147,16 +150,15 @@ async def test_tracing_setup_package_detection(
147150
app = web.Application()
148151
service_name = "simcore_service_webserver"
149152
tracing_settings = TracingSettings()
153+
tracing_data = TracingData.create(
154+
tracing_settings=tracing_settings, service_name=service_name
155+
)
150156
async for _ in get_tracing_lifespan(
151-
app=app,
152-
service_name=service_name,
153-
tracing_settings=tracing_settings,
157+
app=app, tracing_settings=tracing_settings, tracing_data=tracing_data
154158
)(app):
155159
# idempotency
156160
async for _ in get_tracing_lifespan(
157-
app=app,
158-
service_name=service_name,
159-
tracing_settings=tracing_settings,
161+
app=app, tracing_settings=tracing_settings, tracing_data=tracing_data
160162
)(app):
161163
pass
162164

@@ -181,6 +183,9 @@ async def test_trace_id_in_response_header(
181183
app = web.Application()
182184
service_name = "simcore_service_webserver"
183185
tracing_settings = TracingSettings()
186+
tracing_data = TracingData.create(
187+
tracing_settings=tracing_settings, service_name=service_name
188+
)
184189

185190
async def handler(handler_data: dict, request: web.Request) -> web.Response:
186191
current_span = trace.get_current_span()
@@ -196,8 +201,8 @@ async def handler(handler_data: dict, request: web.Request) -> web.Response:
196201

197202
async for _ in get_tracing_lifespan(
198203
app=app,
199-
service_name=service_name,
200204
tracing_settings=tracing_settings,
205+
tracing_data=tracing_data,
201206
add_response_trace_id_header=True,
202207
)(app):
203208
client = await aiohttp_client(app)
@@ -234,16 +239,17 @@ async def test_tracing_sampling_probability_effective(
234239
app = web.Application()
235240
service_name = "simcore_service_webserver"
236241
tracing_settings = TracingSettings()
242+
tracing_data = TracingData.create(
243+
tracing_settings=tracing_settings, service_name=service_name
244+
)
237245

238246
async def handler(request: web.Request) -> web.Response:
239247
return web.Response(text="ok")
240248

241249
app.router.add_get("/", handler)
242250

243251
async for _ in get_tracing_lifespan(
244-
app=app,
245-
service_name=service_name,
246-
tracing_settings=tracing_settings,
252+
app=app, tracing_settings=tracing_settings, tracing_data=tracing_data
247253
)(app):
248254
client = await aiohttp_client(app)
249255

services/web/server/src/simcore_service_webserver/tracing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from aiohttp import web
44
from servicelib.aiohttp.tracing import get_tracing_lifespan
5+
from servicelib.tracing import TracingData
56
from settings_library.tracing import TracingSettings
67

8+
from ._meta import APP_NAME
79
from .application_keys import APP_SETTINGS_APPKEY
810
from .application_settings import get_application_settings
911
from .application_setup import ModuleCategory, app_setup_func
@@ -34,12 +36,15 @@ def setup_app_tracing(app: web.Application):
3436

3537
app_settings = get_application_settings(app)
3638
tracing_settings: TracingSettings = get_plugin_settings(app)
39+
tracing_data = TracingData.create(
40+
tracing_settings=tracing_settings, service_name=APP_NAME
41+
)
3742

3843
app.cleanup_ctx.append(
3944
get_tracing_lifespan(
4045
app=app,
4146
tracing_settings=tracing_settings,
42-
service_name=app_settings.APP_NAME,
4347
add_response_trace_id_header=True,
48+
tracing_data=tracing_data,
4449
)
4550
)

0 commit comments

Comments
 (0)