11from typing import TYPE_CHECKING , Dict , Optional , Union
22from uuid import UUID
3+ import os
34
45from opentelemetry .sdk .trace import TracerProvider
5- from opentelemetry .sdk .trace .export import BatchSpanProcessor
6+ from opentelemetry .sdk .trace .export import BatchSpanProcessor , SpanExporter
67
78from agentops .config import Configuration
89from agentops .log_config import logger
@@ -23,40 +24,54 @@ def __init__(self):
2324 self ._otel_manager : Optional [OTELManager ] = None
2425 self ._tracer_provider : Optional [TracerProvider ] = None
2526 self ._session_exporters : Dict [UUID , ExportManager ] = {}
26- self ._otel_config : Optional [OTELConfig ] = None
27+ self .config : Optional [OTELConfig ] = None
2728
28- def initialize (self , config : Configuration , otel_config : Optional [ OTELConfig ] = None ) -> None :
29+ def initialize (self , config : Configuration ) -> None :
2930 """Initialize telemetry components"""
30- # Create OTEL config from Configuration if needed
31- if otel_config is None :
32- logger .warning ("OTEL config is not provided, using default EMPTY config" )
33- otel_config = OTELConfig ()
34-
31+ # Check for environment variables if no exporters configured
32+ if not config .otel .additional_exporters :
33+ endpoint = os .environ .get ("OTEL_EXPORTER_OTLP_ENDPOINT" )
34+ service_name = os .environ .get ("OTEL_SERVICE_NAME" )
35+
36+ if service_name and not config .otel .resource_attributes :
37+ config .otel .resource_attributes = {"service.name" : service_name }
38+
39+ if endpoint :
40+ from opentelemetry .exporter .otlp .proto .grpc .trace_exporter import OTLPSpanExporter
41+ config .otel .additional_exporters = [OTLPSpanExporter (endpoint = endpoint )]
42+ logger .info ("Using OTEL configuration from environment variables" )
43+
44+ # Validate exporters
45+ if config .otel .additional_exporters :
46+ for exporter in config .otel .additional_exporters :
47+ if not isinstance (exporter , SpanExporter ):
48+ raise ValueError (f"Invalid exporter type: { type (exporter )} . Must be a SpanExporter" )
49+
3550 # Create the OTEL manager instance
3651 self ._otel_manager = OTELManager (
37- config = otel_config ,
38- exporters = otel_config .additional_exporters ,
39- resource_attributes = otel_config .resource_attributes ,
40- sampler = otel_config .sampler
52+ config = config . otel ,
53+ exporters = config . otel .additional_exporters ,
54+ resource_attributes = config . otel .resource_attributes ,
55+ sampler = config . otel .sampler
4156 )
42- self ._otel_config = otel_config
57+ self .config = config
4358
4459 # Initialize the tracer provider with global service info
4560 self ._tracer_provider = self ._otel_manager .initialize (
4661 service_name = "agentops" ,
47- session_id = "global" # Use a global session ID for client-level telemetry
62+ session_id = "global"
4863 )
4964
50- def get_session_tracer (self , session_id : UUID , config , jwt : str ):
65+ def get_session_tracer (self , session_id : UUID , jwt : str ):
5166 """Get or create a tracer for a specific session"""
5267 # Create session-specific exporter
5368 exporter = ExportManager (
5469 session_id = session_id ,
55- endpoint = config .endpoint ,
70+ endpoint = self . config .endpoint ,
5671 jwt = jwt ,
57- api_key = config .api_key ,
58- retry_config = self ._otel_config .retry_config if self ._otel_config else None ,
59- custom_formatters = self ._otel_config .custom_formatters if self ._otel_config else None ,
72+ api_key = self . config .api_key ,
73+ retry_config = self .config .retry_config if self .config else None ,
74+ custom_formatters = self .config .custom_formatters if self .config else None ,
6075 )
6176
6277 # Store exporter reference
@@ -65,11 +80,11 @@ def get_session_tracer(self, session_id: UUID, config, jwt: str):
6580 # Add both batch and in-flight processors
6681 batch_processor = BatchSpanProcessor (
6782 exporter ,
68- max_queue_size = config .max_queue_size ,
83+ max_queue_size = self . config .max_queue_size ,
6984 schedule_delay_millis = config .max_wait_time ,
7085 max_export_batch_size = min (
71- max (config .max_queue_size // 20 , 1 ),
72- min (config .max_queue_size , 32 ),
86+ max (self . config .max_queue_size // 20 , 1 ),
87+ min (self . config .max_queue_size , 32 ),
7388 ),
7489 export_timeout_millis = 20000 ,
7590 )
0 commit comments