Skip to content

Commit 3cb5723

Browse files
authored
Refactor Pyroscope tags to drop thread ID usage (#139)
1 parent 8824867 commit 3cb5723

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

microbootstrap/instruments/opentelemetry_instrument.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import dataclasses
33
import logging
44
import os
5-
import threading
65
import typing
76

87
import pydantic
@@ -200,17 +199,14 @@ class PyroscopeSpanProcessor(SpanProcessor):
200199
def on_start(self, span: Span, parent_context: Context | None = None) -> None: # noqa: ARG002
201200
if _is_root_span(span):
202201
formatted_span_id: typing.Final = format_span_id(span.context.span_id)
203-
thread_id: typing.Final = threading.get_ident()
204-
205202
span.set_attribute(OTEL_PROFILE_ID_KEY, formatted_span_id)
206-
pyroscope.add_thread_tag(thread_id, PYROSCOPE_SPAN_ID_KEY, formatted_span_id)
207-
pyroscope.add_thread_tag(thread_id, PYROSCOPE_SPAN_NAME_KEY, span.name)
203+
pyroscope.add_thread_tag(PYROSCOPE_SPAN_ID_KEY, formatted_span_id)
204+
pyroscope.add_thread_tag(PYROSCOPE_SPAN_NAME_KEY, span.name)
208205

209206
def on_end(self, span: ReadableSpan) -> None:
210207
if _is_root_span(span):
211-
thread_id: typing.Final = threading.get_ident()
212-
pyroscope.remove_thread_tag(thread_id, PYROSCOPE_SPAN_ID_KEY, format_span_id(span.context.span_id))
213-
pyroscope.remove_thread_tag(thread_id, PYROSCOPE_SPAN_NAME_KEY, span.name)
208+
pyroscope.remove_thread_tag(PYROSCOPE_SPAN_ID_KEY, format_span_id(span.context.span_id))
209+
pyroscope.remove_thread_tag(PYROSCOPE_SPAN_NAME_KEY, span.name)
214210

215211
def force_flush(self, timeout_millis: int = 30000) -> bool: # noqa: ARG002 # pragma: no cover
216212
return True

tests/instruments/test_pyroscope.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
try:
16-
import pyroscope # type: ignore[import-untyped] # noqa: F401
16+
import pyroscope # type: ignore[import-untyped]
1717
except ImportError: # pragma: no cover
1818
pytest.skip("pyroscope is not installed", allow_module_level=True)
1919

@@ -33,12 +33,16 @@ def test_not_ready(self) -> None:
3333
instrument = PyroscopeInstrument(PyroscopeConfig(pyroscope_endpoint=None))
3434
assert not instrument.is_ready()
3535

36-
def test_opentelemetry_includes_pyroscope_2(
36+
def test_opentelemetry_includes_pyroscope(
3737
self, monkeypatch: pytest.MonkeyPatch, minimal_opentelemetry_config: OpentelemetryConfig
3838
) -> None:
3939
monkeypatch.setattr("opentelemetry.sdk.trace.TracerProvider.shutdown", Mock())
40-
monkeypatch.setattr("pyroscope.add_thread_tag", add_thread_tag_mock := Mock())
41-
monkeypatch.setattr("pyroscope.remove_thread_tag", remove_thread_tag_mock := Mock())
40+
monkeypatch.setattr(
41+
"pyroscope.add_thread_tag", add_thread_tag_mock := Mock(side_effect=pyroscope.add_thread_tag)
42+
)
43+
monkeypatch.setattr(
44+
"pyroscope.remove_thread_tag", remove_thread_tag_mock := Mock(side_effect=pyroscope.remove_thread_tag)
45+
)
4246

4347
minimal_opentelemetry_config.pyroscope_endpoint = pydantic.HttpUrl("http://localhost:4040")
4448

@@ -53,5 +57,5 @@ async def test_handler() -> None: ...
5357
assert (
5458
add_thread_tag_mock.mock_calls
5559
== remove_thread_tag_mock.mock_calls
56-
== [mock.call(mock.ANY, "span_id", mock.ANY), mock.call(mock.ANY, "span_name", "GET /test-handler")]
60+
== [mock.call("span_id", mock.ANY), mock.call("span_name", "GET /test-handler")]
5761
)

0 commit comments

Comments
 (0)