Skip to content

Commit 675c1a4

Browse files
authored
opentelemetry: fix import ordering (#99)
* opentelemetry: fix import ordering * test: fix failing tests - auto_instrumentation patched away * test: add missing call to test - Results checked in the test were missing one is_ready() call * fastapi: remove unused type ignore directives * 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. * opentelemetry_instrument: mypy blooper - resources certainly have Resource!
1 parent 26b6ce3 commit 675c1a4

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

microbootstrap/bootstrappers/fastapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def bootstrap_after(self, application: ApplicationT) -> ApplicationT:
104104
class FastApiLoggingInstrument(LoggingInstrument):
105105
def bootstrap_after(self, application: ApplicationT) -> ApplicationT:
106106
if not self.instrument_config.logging_turn_off_middleware:
107-
application.add_middleware( # type: ignore[call-arg]
108-
build_fastapi_logging_middleware(self.instrument_config.logging_exclude_endpoints), # type: ignore[arg-type]
107+
application.add_middleware(
108+
build_fastapi_logging_middleware(self.instrument_config.logging_exclude_endpoints),
109109
)
110110
return application
111111

microbootstrap/instruments/opentelemetry_instrument.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
import pydantic
88
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
9+
from opentelemetry.instrumentation import auto_instrumentation
910
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined] # noqa: TC002
1011
from opentelemetry.sdk import resources
1112
from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor
1213
from opentelemetry.sdk.trace import TracerProvider as SdkTracerProvider
1314
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor
15+
from opentelemetry.semconv.resource import ResourceAttributes
1416
from opentelemetry.trace import format_span_id, set_tracer_provider
15-
from opentelemetry.instrumentation import auto_instrumentation
1617

1718
from microbootstrap.instruments.base import BaseInstrumentConfig, Instrument
1819

@@ -90,16 +91,16 @@ def bootstrap(self) -> None:
9091
auto_instrumentation.initialize()
9192

9293
attributes = {
93-
resources.SERVICE_NAME: self.instrument_config.opentelemetry_service_name
94+
ResourceAttributes.SERVICE_NAME: self.instrument_config.opentelemetry_service_name
9495
or self.instrument_config.service_name,
95-
resources.TELEMETRY_SDK_LANGUAGE: "python",
96-
resources.SERVICE_VERSION: self.instrument_config.service_version,
96+
ResourceAttributes.TELEMETRY_SDK_LANGUAGE: "python",
97+
ResourceAttributes.SERVICE_VERSION: self.instrument_config.service_version,
9798
}
9899
if self.instrument_config.opentelemetry_namespace:
99-
attributes[resources.SERVICE_NAMESPACE] = self.instrument_config.opentelemetry_namespace
100+
attributes[ResourceAttributes.SERVICE_NAMESPACE] = self.instrument_config.opentelemetry_namespace
100101
if self.instrument_config.opentelemetry_container_name:
101-
attributes[resources.CONTAINER_NAME] = self.instrument_config.opentelemetry_container_name
102-
resource: typing.Final = resources.Resource.create(attributes=attributes)
102+
attributes[ResourceAttributes.CONTAINER_NAME] = self.instrument_config.opentelemetry_container_name
103+
resource: typing.Final = resources.Resource.create(attributes=attributes) # type: ignore[attr-defined]
103104

104105
self.tracer_provider = SdkTracerProvider(resource=resource)
105106
if self.instrument_config.pyroscope_endpoint and pyroscope:

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
SentryConfig,
1717
)
1818
from microbootstrap.console_writer import ConsoleWriter
19+
from microbootstrap.instruments import opentelemetry_instrument
1920
from microbootstrap.instruments.cors_instrument import CorsConfig
2021
from microbootstrap.instruments.health_checks_instrument import HealthChecksConfig
2122
from microbootstrap.instruments.prometheus_instrument import BasePrometheusConfig
@@ -126,3 +127,8 @@ def console_writer() -> ConsoleWriter:
126127
def reset_reloaded_settings_module() -> typing.Iterator[None]:
127128
yield
128129
importlib.reload(microbootstrap.settings)
130+
131+
132+
@pytest.fixture(autouse=True)
133+
def disable_auto_instrumentation(monkeypatch: pytest.MonkeyPatch) -> None:
134+
monkeypatch.setattr(opentelemetry_instrument, "auto_instrumentation", MagicMock())

tests/test_instruments_setupper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_instruments_setupper_causes_instruments_lifespan() -> None:
4444
mock.call.is_ready(),
4545
mock.call.bootstrap(),
4646
mock.call.write_status(current_setupper.console_writer),
47+
mock.call.is_ready(),
4748
mock.call.teardown(),
4849
]
4950
assert all_mock_calls == [expected_successful_instrument_calls] * instruments_count

0 commit comments

Comments
 (0)