2023-08-26 15:04:00 +00:00
|
|
|
from typing import Optional
|
2024-10-10 10:59:18 -07:00
|
|
|
|
2023-08-26 15:04:00 +00:00
|
|
|
from opentelemetry.baggage import get_all as get_all_baggage
|
|
|
|
|
from opentelemetry.context import Context
|
2023-11-02 21:09:58 +00:00
|
|
|
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter
|
2024-10-10 10:59:18 -07:00
|
|
|
from opentelemetry.trace import Span
|
2023-08-26 15:04:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class BatchedBaggageSpanProcessor(BatchSpanProcessor):
|
|
|
|
|
"""
|
|
|
|
|
The BatchedBaggageSpanProcessor is combination between a baggage propagator and the BatchSpanProcessor.
|
|
|
|
|
|
|
|
|
|
The BatchedBaggageSpanProcessor reads entries stored in Baggage
|
|
|
|
|
from the parent context and adds the baggage entries' keys and
|
|
|
|
|
values to the span as attributes on span start.
|
|
|
|
|
|
2023-11-28 04:56:25 +00:00
|
|
|
Use this to propogate attributes you want in every nested span.
|
2023-08-26 15:04:00 +00:00
|
|
|
"""
|
|
|
|
|
|
2023-11-02 21:09:58 +00:00
|
|
|
def __init__(self, span_exporter: SpanExporter):
|
|
|
|
|
# Lower the max_export_batch_size because we were hitting sizes too big for evergreen
|
|
|
|
|
super().__init__(span_exporter, max_export_batch_size=100)
|
|
|
|
|
|
2023-08-26 15:04:00 +00:00
|
|
|
def on_start(self, span: "Span", parent_context: Optional[Context] = None) -> None:
|
|
|
|
|
baggage = get_all_baggage(parent_context)
|
|
|
|
|
for key, value in baggage.items():
|
|
|
|
|
span.set_attribute(key, value)
|
|
|
|
|
super().on_start(span, parent_context)
|