From 4ce4fa50a21deb55306a474024795fc5fe21e283 Mon Sep 17 00:00:00 2001 From: Daniel Berendeev Date: Wed, 28 May 2025 19:59:27 +0000 Subject: [PATCH 1/6] opentelemetry: fix import ordering --- microbootstrap/instruments/opentelemetry_instrument.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/microbootstrap/instruments/opentelemetry_instrument.py b/microbootstrap/instruments/opentelemetry_instrument.py index 99659f8..d40221e 100644 --- a/microbootstrap/instruments/opentelemetry_instrument.py +++ b/microbootstrap/instruments/opentelemetry_instrument.py @@ -6,13 +6,13 @@ import pydantic from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter +from opentelemetry.instrumentation import auto_instrumentation from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined] # noqa: TC002 from opentelemetry.sdk import resources from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor from opentelemetry.sdk.trace import TracerProvider as SdkTracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor from opentelemetry.trace import format_span_id, set_tracer_provider -from opentelemetry.instrumentation import auto_instrumentation from microbootstrap.instruments.base import BaseInstrumentConfig, Instrument From 70c21b09ab6ab7d5a7c77652e2e5ed0433a5ba67 Mon Sep 17 00:00:00 2001 From: Daniel Berendeev Date: Mon, 9 Jun 2025 18:04:52 +0000 Subject: [PATCH 2/6] test: fix failing tests - auto_instrumentation patched away --- tests/conftest.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index c9fa765..23bf169 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,6 +16,7 @@ SentryConfig, ) from microbootstrap.console_writer import ConsoleWriter +from microbootstrap.instruments import opentelemetry_instrument from microbootstrap.instruments.cors_instrument import CorsConfig from microbootstrap.instruments.health_checks_instrument import HealthChecksConfig from microbootstrap.instruments.prometheus_instrument import BasePrometheusConfig @@ -126,3 +127,8 @@ def console_writer() -> ConsoleWriter: def reset_reloaded_settings_module() -> typing.Iterator[None]: yield importlib.reload(microbootstrap.settings) + + +@pytest.fixture(autouse=True) +def disable_auto_instrumentation(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(opentelemetry_instrument, "auto_instrumentation", MagicMock()) From 51cbed2bf148587b5ea83bf629fcf0a0fb29196d Mon Sep 17 00:00:00 2001 From: Daniel Berendeev Date: Mon, 9 Jun 2025 18:25:11 +0000 Subject: [PATCH 3/6] test: add missing call to test - Results checked in the test were missing one is_ready() call --- tests/test_instruments_setupper.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_instruments_setupper.py b/tests/test_instruments_setupper.py index f473da3..def93a2 100644 --- a/tests/test_instruments_setupper.py +++ b/tests/test_instruments_setupper.py @@ -44,6 +44,7 @@ def test_instruments_setupper_causes_instruments_lifespan() -> None: mock.call.is_ready(), mock.call.bootstrap(), mock.call.write_status(current_setupper.console_writer), + mock.call.is_ready(), mock.call.teardown(), ] assert all_mock_calls == [expected_successful_instrument_calls] * instruments_count From 80244a278564d76324aac8a8cd6d1e72b153204c Mon Sep 17 00:00:00 2001 From: Daniel Berendeev Date: Mon, 9 Jun 2025 18:48:34 +0000 Subject: [PATCH 4/6] fastapi: remove unused type ignore directives --- microbootstrap/bootstrappers/fastapi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/microbootstrap/bootstrappers/fastapi.py b/microbootstrap/bootstrappers/fastapi.py index 655c59c..b567bf4 100644 --- a/microbootstrap/bootstrappers/fastapi.py +++ b/microbootstrap/bootstrappers/fastapi.py @@ -104,8 +104,8 @@ def bootstrap_after(self, application: ApplicationT) -> ApplicationT: class FastApiLoggingInstrument(LoggingInstrument): def bootstrap_after(self, application: ApplicationT) -> ApplicationT: if not self.instrument_config.logging_turn_off_middleware: - application.add_middleware( # type: ignore[call-arg] - build_fastapi_logging_middleware(self.instrument_config.logging_exclude_endpoints), # type: ignore[arg-type] + application.add_middleware( + build_fastapi_logging_middleware(self.instrument_config.logging_exclude_endpoints), ) return application From 49248af6c944fc816d0fab53564ed498a6127d8e Mon Sep 17 00:00:00 2001 From: Daniel Berendeev Date: Mon, 9 Jun 2025 19:11:29 +0000 Subject: [PATCH 5/6] openetelemetry_instrument: use ResourceAttributes instead of aliases - resources module uses constants that are just aliases to ResourceAttributes module. The module has a @deprecated message though. But it doesn't appear to matter at the moment. --- .../instruments/opentelemetry_instrument.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/microbootstrap/instruments/opentelemetry_instrument.py b/microbootstrap/instruments/opentelemetry_instrument.py index d40221e..1ea99e1 100644 --- a/microbootstrap/instruments/opentelemetry_instrument.py +++ b/microbootstrap/instruments/opentelemetry_instrument.py @@ -12,6 +12,7 @@ from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor from opentelemetry.sdk.trace import TracerProvider as SdkTracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor +from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.trace import format_span_id, set_tracer_provider from microbootstrap.instruments.base import BaseInstrumentConfig, Instrument @@ -90,15 +91,15 @@ def bootstrap(self) -> None: auto_instrumentation.initialize() attributes = { - resources.SERVICE_NAME: self.instrument_config.opentelemetry_service_name + ResourceAttributes.SERVICE_NAME: self.instrument_config.opentelemetry_service_name or self.instrument_config.service_name, - resources.TELEMETRY_SDK_LANGUAGE: "python", - resources.SERVICE_VERSION: self.instrument_config.service_version, + ResourceAttributes.TELEMETRY_SDK_LANGUAGE: "python", + ResourceAttributes.SERVICE_VERSION: self.instrument_config.service_version, } if self.instrument_config.opentelemetry_namespace: - attributes[resources.SERVICE_NAMESPACE] = self.instrument_config.opentelemetry_namespace + attributes[ResourceAttributes.SERVICE_NAMESPACE] = self.instrument_config.opentelemetry_namespace if self.instrument_config.opentelemetry_container_name: - attributes[resources.CONTAINER_NAME] = self.instrument_config.opentelemetry_container_name + attributes[ResourceAttributes.CONTAINER_NAME] = self.instrument_config.opentelemetry_container_name resource: typing.Final = resources.Resource.create(attributes=attributes) self.tracer_provider = SdkTracerProvider(resource=resource) From a31dcddedd2ba0724e0d1d8ac571629ff0f61196 Mon Sep 17 00:00:00 2001 From: Daniel Berendeev Date: Mon, 9 Jun 2025 19:12:52 +0000 Subject: [PATCH 6/6] opentelemetry_instrument: mypy blooper - resources certainly have Resource! --- microbootstrap/instruments/opentelemetry_instrument.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/microbootstrap/instruments/opentelemetry_instrument.py b/microbootstrap/instruments/opentelemetry_instrument.py index 1ea99e1..88c0560 100644 --- a/microbootstrap/instruments/opentelemetry_instrument.py +++ b/microbootstrap/instruments/opentelemetry_instrument.py @@ -100,7 +100,7 @@ def bootstrap(self) -> None: attributes[ResourceAttributes.SERVICE_NAMESPACE] = self.instrument_config.opentelemetry_namespace if self.instrument_config.opentelemetry_container_name: attributes[ResourceAttributes.CONTAINER_NAME] = self.instrument_config.opentelemetry_container_name - resource: typing.Final = resources.Resource.create(attributes=attributes) + resource: typing.Final = resources.Resource.create(attributes=attributes) # type: ignore[attr-defined] self.tracer_provider = SdkTracerProvider(resource=resource) if self.instrument_config.pyroscope_endpoint and pyroscope: