Skip to content
Merged
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
54 changes: 15 additions & 39 deletions quotientai/tracing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from opentelemetry.sdk.trace import TracerProvider, SpanProcessor, Span
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import Resource

from opentelemetry.trace import (
set_tracer_provider,
Expand Down Expand Up @@ -43,38 +44,6 @@ class QuotientAttributes(str, Enum):
detections = "quotient.detections"


class QuotientAttributesSpanProcessor(SpanProcessor):
"""
Processor that adds Quotient-specific attributes to all spans, including:

- `app_name`
- `environment`

Which are all required for tracing.
"""

app_name: str
environment: str
detections: str

def __init__(self, app_name: str, environment: str, detections: Optional[str] = None):
self.app_name = app_name
self.environment = environment
self.detections = detections

def on_start(self, span: Span, parent_context: Optional[otel_context.Context] = None) -> None:
attributes = {
QuotientAttributes.app_name: self.app_name,
QuotientAttributes.environment: self.environment,
}

if self.detections is not None:
attributes[QuotientAttributes.detections] = self.detections

span.set_attributes(attributes)
super().on_start(span, parent_context)


class TracingResource:

def __init__(self, client):
Expand Down Expand Up @@ -165,7 +134,20 @@ def _setup_auto_collector(self, app_name: str, environment: str, instruments: Op

# Only set up if not already configured (avoid double setup)
if not hasattr(current_provider, '_span_processors') or not current_provider._span_processors:
tracer_provider = TracerProvider()

# Create resource with quotient attributes
resource_attributes = {
QuotientAttributes.app_name: app_name,
QuotientAttributes.environment: environment,
}

if detections is not None:
resource_attributes[QuotientAttributes.detections] = detections

resource = Resource.create(resource_attributes)

# Create TracerProvider with the resource
tracer_provider = TracerProvider(resource=resource)

# Get collector endpoint from environment or use default
exporter_endpoint = os.environ.get(
Expand All @@ -191,12 +173,6 @@ def _setup_auto_collector(self, app_name: str, environment: str, instruments: Op

# Use batch processor for better performance
span_processor = BatchSpanProcessor(otlp_exporter)
quotient_attributes_span_processor = QuotientAttributesSpanProcessor(
app_name=app_name,
environment=environment,
detections=detections,
)
tracer_provider.add_span_processor(quotient_attributes_span_processor)
tracer_provider.add_span_processor(span_processor)

# Set the global tracer provider
Expand Down