Skip to content

opentelemetry: fix import ordering #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions microbootstrap/bootstrappers/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 8 additions & 7 deletions microbootstrap/instruments/opentelemetry_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

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.semconv.resource import ResourceAttributes
from opentelemetry.trace import format_span_id, set_tracer_provider
from opentelemetry.instrumentation import auto_instrumentation

from microbootstrap.instruments.base import BaseInstrumentConfig, Instrument

Expand Down Expand Up @@ -90,16 +91,16 @@ 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
resource: typing.Final = resources.Resource.create(attributes=attributes)
attributes[ResourceAttributes.CONTAINER_NAME] = self.instrument_config.opentelemetry_container_name
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:
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
1 change: 1 addition & 0 deletions tests/test_instruments_setupper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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