|
1 | 1 | from __future__ import annotations |
2 | 2 | import dataclasses |
3 | 3 | import os |
| 4 | +import threading |
4 | 5 | import typing |
5 | 6 |
|
6 | 7 | import pydantic |
7 | 8 | from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter |
8 | 9 | from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined] # noqa: TC002 |
9 | 10 | from opentelemetry.sdk import resources |
10 | | -from opentelemetry.sdk.trace import ReadableSpan |
| 11 | +from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor |
11 | 12 | from opentelemetry.sdk.trace import TracerProvider as SdkTracerProvider |
12 | 13 | from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor |
13 | | -from opentelemetry.trace import set_tracer_provider |
| 14 | +from opentelemetry.trace import format_span_id, set_tracer_provider |
14 | 15 |
|
15 | 16 | from microbootstrap.instruments.base import BaseInstrumentConfig, Instrument |
16 | 17 |
|
17 | 18 |
|
18 | 19 | try: |
19 | | - from pyroscope.otel import PyroscopeSpanProcessor # type: ignore[import-untyped] |
| 20 | + import pyroscope # type: ignore[import-untyped] |
20 | 21 | except ImportError: # pragma: no cover |
21 | | - PyroscopeSpanProcessor = None |
| 22 | + pyroscope = None |
22 | 23 |
|
23 | 24 |
|
24 | 25 | if typing.TYPE_CHECKING: |
25 | 26 | import faststream |
| 27 | + from opentelemetry.context import Context |
26 | 28 | from opentelemetry.metrics import Meter, MeterProvider |
27 | 29 | from opentelemetry.trace import TracerProvider |
28 | 30 |
|
@@ -97,7 +99,7 @@ def bootstrap(self) -> None: |
97 | 99 | resource: typing.Final = resources.Resource.create(attributes=attributes) |
98 | 100 |
|
99 | 101 | self.tracer_provider = SdkTracerProvider(resource=resource) |
100 | | - if self.instrument_config.pyroscope_endpoint and PyroscopeSpanProcessor: |
| 102 | + if self.instrument_config.pyroscope_endpoint and pyroscope: |
101 | 103 | self.tracer_provider.add_span_processor(PyroscopeSpanProcessor()) |
102 | 104 |
|
103 | 105 | if self.instrument_config.service_debug: |
@@ -129,3 +131,34 @@ def define_exclude_urls(self) -> list[str]: |
129 | 131 | @classmethod |
130 | 132 | def get_config_type(cls) -> type[OpentelemetryConfig]: |
131 | 133 | return OpentelemetryConfig |
| 134 | + |
| 135 | + |
| 136 | +OTEL_PROFILE_ID_KEY: typing.Final = "pyroscope.profile.id" |
| 137 | +PYROSCOPE_SPAN_ID_KEY: typing.Final = "span_id" |
| 138 | +PYROSCOPE_SPAN_NAME_KEY: typing.Final = "span_name" |
| 139 | + |
| 140 | + |
| 141 | +def _is_root_span(span: ReadableSpan) -> bool: |
| 142 | + return span.parent is None or span.parent.is_remote |
| 143 | + |
| 144 | + |
| 145 | +# Extended `pyroscope-otel` span processor: https://github.com/grafana/otel-profiling-python/blob/990662d416943e992ab70036b35b27488c98336a/src/pyroscope/otel/__init__.py |
| 146 | +# Includes `span_name` to identify if it makes sense to go to profiles from traces. |
| 147 | +class PyroscopeSpanProcessor(SpanProcessor): |
| 148 | + def on_start(self, span: Span, parent_context: Context | None = None) -> None: # noqa: ARG002 |
| 149 | + if _is_root_span(span): |
| 150 | + formatted_span_id = format_span_id(span.context.span_id) |
| 151 | + thread_id = threading.get_ident() |
| 152 | + |
| 153 | + span.set_attribute(OTEL_PROFILE_ID_KEY, formatted_span_id) |
| 154 | + pyroscope.add_thread_tag(thread_id, PYROSCOPE_SPAN_ID_KEY, formatted_span_id) |
| 155 | + pyroscope.add_thread_tag(thread_id, PYROSCOPE_SPAN_NAME_KEY, span.name) |
| 156 | + |
| 157 | + def on_end(self, span: ReadableSpan) -> None: |
| 158 | + if _is_root_span(span): |
| 159 | + thread_id = threading.get_ident() |
| 160 | + pyroscope.remove_thread_tag(thread_id, PYROSCOPE_SPAN_ID_KEY, format_span_id(span.context.span_id)) |
| 161 | + pyroscope.remove_thread_tag(thread_id, PYROSCOPE_SPAN_NAME_KEY, span.name) |
| 162 | + |
| 163 | + def force_flush(self, timeout_millis: int = 30000) -> bool: # noqa: ARG002 # pragma: no cover |
| 164 | + return True |
0 commit comments