Skip to content

Commit 5160d11

Browse files
committed
Fix aiohttp server autoinstrumentation with a hack
1 parent f22215c commit 5160d11

File tree

1 file changed

+8
-5
lines changed
  • packages/service-library/src/servicelib/aiohttp

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
OTLPSpanExporter as OTLPSpanExporterHTTP,
1010
)
1111
from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor
12-
from opentelemetry.instrumentation.aiohttp_server import AioHttpServerInstrumentor
12+
from opentelemetry.instrumentation.aiohttp_server import (
13+
AioHttpServerInstrumentor,
14+
middleware,
15+
)
1316
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
1417
from opentelemetry.instrumentation.requests import RequestsInstrumentor
1518
from opentelemetry.sdk.resources import Resource
@@ -21,7 +24,7 @@
2124

2225

2326
def setup_tracing(
24-
app: web.Application, # pylint: disable=unused-argument
27+
app: web.Application,
2528
tracing_settings: TracingSettings,
2629
service_name: str,
2730
instrument_aiopg: bool = False, # noqa: FBT001, FBT002
@@ -37,9 +40,8 @@ def setup_tracing(
3740
log.warning("Skipping opentelemetry tracing setup")
3841
return
3942
if not opentelemetry_collector_endpoint or not opentelemetry_collector_port:
40-
raise RuntimeError(
41-
f"Variable opentelemetry_collector_endpoint [{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}] or opentelemetry_collector_port [{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT}] unset. Tracing options incomplete."
42-
)
43+
msg = f"Variable opentelemetry_collector_endpoint [{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}] or opentelemetry_collector_port [{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT}] unset. Tracing options incomplete."
44+
raise RuntimeError(msg)
4345
resource = Resource(attributes={"service.name": service_name})
4446
trace.set_tracer_provider(TracerProvider(resource=resource))
4547
tracer_provider: trace.TracerProvider = trace.get_tracer_provider()
@@ -61,6 +63,7 @@ def setup_tracing(
6163
tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter)) # type: ignore[attr-defined] # https://github.com/open-telemetry/opentelemetry-python/issues/3713
6264
# Instrument aiohttp server and client
6365
AioHttpServerInstrumentor().instrument()
66+
app.middlewares.append(middleware)
6467
AioHttpClientInstrumentor().instrument()
6568
if instrument_aiopg:
6669
AiopgInstrumentor().instrument()

0 commit comments

Comments
 (0)