1+ import pytest
2+ from opentelemetry .exporter .otlp .proto .grpc .trace_exporter import OTLPSpanExporter
3+ from unittest .mock import patch
4+
5+ import agentops
6+ from agentops .telemetry .config import OTELConfig
7+ from agentops .config import Configuration
8+
9+
10+ def test_configuration_with_otel ():
11+ """Test that Configuration properly stores OTEL config"""
12+ exporter = OTLPSpanExporter (endpoint = "http://localhost:4317" )
13+ otel_config = OTELConfig (additional_exporters = [exporter ])
14+
15+ config = Configuration ()
16+ config .configure (None , otel = otel_config )
17+
18+ assert config .otel == otel_config
19+ assert config .otel .additional_exporters == [exporter ]
20+
21+
22+ def test_init_accepts_telemetry_config ():
23+ """Test that init accepts telemetry configuration"""
24+ exporter = OTLPSpanExporter (endpoint = "http://localhost:4317" )
25+ telemetry = OTELConfig (additional_exporters = [exporter ])
26+
27+ agentops .init (
28+ api_key = "test-key" ,
29+ telemetry = telemetry
30+ )
31+
32+ # Verify exporter was configured
33+ client = agentops .Client ()
34+ assert client .telemetry .config .additional_exporters == [exporter ]
35+
36+
37+ def test_init_with_env_var_endpoint ():
38+ """Test configuring exporter endpoint via env var"""
39+ with patch ('os.environ.get' ) as mock_env :
40+ mock_env .return_value = "http://custom:4317"
41+
42+ agentops .init (api_key = "test-key" )
43+
44+ client = agentops .Client ()
45+ assert client .telemetry .config is not None
46+
47+ # Should have created an OTLPSpanExporter with the env var endpoint
48+ exporters = client .telemetry .config .additional_exporters
49+ assert len (exporters ) == 1
50+ assert isinstance (exporters [0 ], OTLPSpanExporter )
51+ assert exporters [0 ].endpoint == "http://custom:4317"
52+
53+
54+ def test_telemetry_config_overrides_env_vars ():
55+ """Test that explicit telemetry config takes precedence over env vars"""
56+ custom_exporter = OTLPSpanExporter (endpoint = "http://explicit:4317" )
57+ telemetry = OTELConfig (additional_exporters = [custom_exporter ])
58+
59+ with patch ('os.environ.get' ) as mock_env :
60+ mock_env .return_value = "http://fromenv:4317"
61+
62+ agentops .init (
63+ api_key = "test-key" ,
64+ telemetry = telemetry
65+ )
66+
67+ client = agentops .Client ()
68+ assert client .telemetry .config .additional_exporters == [custom_exporter ]
69+
70+
71+ def test_multiple_exporters_in_config ():
72+ """Test configuration with multiple exporters"""
73+ exporter1 = OTLPSpanExporter (endpoint = "http://first:4317" )
74+ exporter2 = OTLPSpanExporter (endpoint = "http://second:4317" )
75+
76+ telemetry = OTELConfig (additional_exporters = [exporter1 , exporter2 ])
77+ config = Configuration ()
78+ config .configure (None , otel = telemetry )
79+
80+ assert len (config .otel .additional_exporters ) == 2
81+ assert config .otel .additional_exporters == [exporter1 , exporter2 ]
0 commit comments