Skip to content

Commit 8316350

Browse files
authored
Stop OpenTelemetry log spam in tests (#136)
* Add type hints and silence OpenTelemetry logs * Remove redundant test file t.py * Remove redundant type ignore comment
1 parent 455fbd2 commit 8316350

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

microbootstrap/instruments/opentelemetry_instrument.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22
import dataclasses
3+
import logging
34
import os
45
import threading
56
import typing
@@ -35,7 +36,6 @@
3536
from opentelemetry.metrics import Meter, MeterProvider
3637
from opentelemetry.trace import TracerProvider
3738

38-
3939
OpentelemetryConfigT = typing.TypeVar("OpentelemetryConfigT", bound="OpentelemetryConfig")
4040

4141

@@ -101,7 +101,7 @@ class BaseOpentelemetryInstrument(Instrument[OpentelemetryConfigT]):
101101
ready_condition = "Provide all necessary config parameters"
102102

103103
def _load_instrumentors(self) -> None:
104-
for entry_point in entry_points(group="opentelemetry_instrumentor"): # type: ignore[no-untyped-call]
104+
for entry_point in entry_points(group="opentelemetry_instrumentor"):
105105
if entry_point.name in self.instrument_config.opentelemetry_disabled_instrumentations:
106106
continue
107107

@@ -131,7 +131,9 @@ def teardown(self) -> None:
131131
instrumentor_with_params.instrumentor.uninstrument(**instrumentor_with_params.additional_params)
132132

133133
def bootstrap(self) -> None:
134-
attributes = {
134+
logging.getLogger("opentelemetry.instrumentation.instrumentor").disabled = True
135+
logging.getLogger("opentelemetry.trace").disabled = True
136+
attributes: typing.Final = {
135137
ResourceAttributes.SERVICE_NAME: self.instrument_config.opentelemetry_service_name
136138
or self.instrument_config.service_name,
137139
ResourceAttributes.TELEMETRY_SDK_LANGUAGE: "python",
@@ -169,7 +171,7 @@ def bootstrap(self) -> None:
169171

170172
class OpentelemetryInstrument(BaseOpentelemetryInstrument[OpentelemetryConfig]):
171173
def define_exclude_urls(self) -> list[str]:
172-
exclude_urls = [*self.instrument_config.opentelemetry_exclude_urls]
174+
exclude_urls: typing.Final = [*self.instrument_config.opentelemetry_exclude_urls]
173175
if (
174176
not self.instrument_config.opentelemetry_generate_health_check_spans
175177
and self.instrument_config.health_checks_path
@@ -197,16 +199,16 @@ def _is_root_span(span: ReadableSpan) -> bool:
197199
class PyroscopeSpanProcessor(SpanProcessor):
198200
def on_start(self, span: Span, parent_context: Context | None = None) -> None: # noqa: ARG002
199201
if _is_root_span(span):
200-
formatted_span_id = format_span_id(span.context.span_id)
201-
thread_id = threading.get_ident()
202+
formatted_span_id: typing.Final = format_span_id(span.context.span_id)
203+
thread_id: typing.Final = threading.get_ident()
202204

203205
span.set_attribute(OTEL_PROFILE_ID_KEY, formatted_span_id)
204206
pyroscope.add_thread_tag(thread_id, PYROSCOPE_SPAN_ID_KEY, formatted_span_id)
205207
pyroscope.add_thread_tag(thread_id, PYROSCOPE_SPAN_NAME_KEY, span.name)
206208

207209
def on_end(self, span: ReadableSpan) -> None:
208210
if _is_root_span(span):
209-
thread_id = threading.get_ident()
211+
thread_id: typing.Final = threading.get_ident()
210212
pyroscope.remove_thread_tag(thread_id, PYROSCOPE_SPAN_ID_KEY, format_span_id(span.context.span_id))
211213
pyroscope.remove_thread_tag(thread_id, PYROSCOPE_SPAN_NAME_KEY, span.name)
212214

0 commit comments

Comments
 (0)