Skip to content

Commit 25e9257

Browse files
committed
revert models.py changes, use raw dicts without modifying codegen
The generated models are unchanged. Python dataclasses don't enforce field types at runtime, so parent fields like BatchSpanProcessor.exporter naturally accept raw dicts (from the YAML loader) alongside typed instances. Tests updated to pass raw dicts consistently. Assisted-by: Claude Opus 4.6
1 parent f2d964b commit 25e9257

4 files changed

Lines changed: 70 additions & 79 deletions

File tree

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/models.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,12 @@ class SeverityNumber(Enum):
350350
fatal4 = "fatal4"
351351

352352

353-
# Diverges from codegen: SpanExporter is typed as dict[str, Any] rather than
354-
# a dataclass so that unknown exporter names (plugin/custom exporters) are
355-
# preserved as dict keys through the config pipeline.
356-
SpanExporter: TypeAlias = dict[str, Any]
353+
@dataclass
354+
class SpanExporter:
355+
otlp_http: OtlpHttpExporter | None = None
356+
otlp_grpc: OtlpGrpcExporter | None = None
357+
otlp_file_development: ExperimentalOtlpFileExporter | None = None
358+
console: ConsoleExporter | None = None
357359

358360

359361
class SpanKind(Enum):
@@ -522,10 +524,12 @@ class ExperimentalTracerConfigurator:
522524
tracers: list[ExperimentalTracerMatcherAndConfig] | None = None
523525

524526

525-
# Diverges from codegen: LogRecordExporter is typed as dict[str, Any] rather
526-
# than a dataclass so that unknown exporter names (plugin/custom exporters)
527-
# are preserved as dict keys through the config pipeline.
528-
LogRecordExporter: TypeAlias = dict[str, Any]
527+
@dataclass
528+
class LogRecordExporter:
529+
otlp_http: OtlpHttpExporter | None = None
530+
otlp_grpc: OtlpGrpcExporter | None = None
531+
otlp_file_development: ExperimentalOtlpFileExporter | None = None
532+
console: ConsoleExporter | None = None
529533

530534

531535
@dataclass
@@ -545,10 +549,12 @@ class PullMetricReader:
545549
cardinality_limits: CardinalityLimits | None = None
546550

547551

548-
# Diverges from codegen: PushMetricExporter is typed as dict[str, Any] rather
549-
# than a dataclass so that unknown exporter names (plugin/custom exporters)
550-
# are preserved as dict keys through the config pipeline.
551-
PushMetricExporter: TypeAlias = dict[str, Any]
552+
@dataclass
553+
class PushMetricExporter:
554+
otlp_http: OtlpHttpMetricExporter | None = None
555+
otlp_grpc: OtlpGrpcMetricExporter | None = None
556+
otlp_file_development: ExperimentalOtlpFileMetricExporter | None = None
557+
console: ConsoleMetricExporter | None = None
552558

553559

554560
@dataclass

opentelemetry-sdk/tests/_configuration/test_logger_provider.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
from opentelemetry.sdk._configuration.models import (
4040
LoggerProvider as LoggerProviderConfig,
4141
)
42-
from opentelemetry.sdk._configuration.models import (
43-
LogRecordExporter as LogRecordExporterConfig,
44-
)
4542
from opentelemetry.sdk._configuration.models import (
4643
LogRecordLimits as LogRecordLimitsConfig,
4744
)
@@ -100,7 +97,7 @@ def _make_batch_config(
10097
max_export_batch_size=None,
10198
):
10299
if exporter_config is None:
103-
exporter_config = LogRecordExporterConfig(console={})
100+
exporter_config = {"console": {}}
104101
return BatchLogRecordProcessorConfig(
105102
exporter=exporter_config,
106103
schedule_delay=schedule_delay,
@@ -184,9 +181,7 @@ def test_batch_processor_uses_console_exporter(self):
184181
)
185182

186183
def test_simple_processor_uses_console_exporter(self):
187-
config = SimpleLogRecordProcessorConfig(
188-
exporter=LogRecordExporterConfig(console={})
189-
)
184+
config = SimpleLogRecordProcessorConfig(exporter={"console": {}})
190185
processor = _create_simple_log_record_processor(config)
191186
self.assertIsInstance(processor, SimpleLogRecordProcessor)
192187
self.assertIsInstance(processor._exporter, ConsoleLogRecordExporter)
@@ -198,9 +193,7 @@ def test_batch_processor_dispatched_from_processor_config(self):
198193

199194
def test_simple_processor_dispatched_from_processor_config(self):
200195
config = LogRecordProcessorConfig(
201-
simple=SimpleLogRecordProcessorConfig(
202-
exporter=LogRecordExporterConfig(console={})
203-
)
196+
simple=SimpleLogRecordProcessorConfig(exporter={"console": {}})
204197
)
205198
processor = _create_log_record_processor(config)
206199
self.assertIsInstance(processor, SimpleLogRecordProcessor)
@@ -224,12 +217,12 @@ def test_batch_processor_suppresses_env_var(self):
224217

225218
class TestCreateLogRecordExporters(unittest.TestCase):
226219
def test_console_exporter(self):
227-
config = LogRecordExporterConfig(console={})
220+
config = {"console": {}}
228221
exporter = _create_log_record_exporter(config)
229222
self.assertIsInstance(exporter, ConsoleLogRecordExporter)
230223

231224
def test_no_exporter_type_raises(self):
232-
config = LogRecordExporterConfig()
225+
config = {}
233226
with self.assertRaises(ConfigurationError):
234227
_create_log_record_exporter(config)
235228

@@ -252,9 +245,11 @@ def test_unknown_log_exporter_raises_configuration_error(self):
252245
_create_log_record_exporter({"no_such_exporter": {}})
253246

254247
def test_otlp_http_missing_package_raises(self):
255-
config = LogRecordExporterConfig(
256-
otlp_http=OtlpHttpExporterConfig(endpoint="http://localhost:4318")
257-
)
248+
config = {
249+
"otlp_http": OtlpHttpExporterConfig(
250+
endpoint="http://localhost:4318"
251+
)
252+
}
258253
with patch.dict(
259254
sys.modules,
260255
{
@@ -266,9 +261,11 @@ def test_otlp_http_missing_package_raises(self):
266261
_create_log_record_exporter(config)
267262

268263
def test_otlp_grpc_missing_package_raises(self):
269-
config = LogRecordExporterConfig(
270-
otlp_grpc=OtlpGrpcExporterConfig(endpoint="http://localhost:4317")
271-
)
264+
config = {
265+
"otlp_grpc": OtlpGrpcExporterConfig(
266+
endpoint="http://localhost:4317"
267+
)
268+
}
272269
with patch.dict(
273270
sys.modules,
274271
{
@@ -296,12 +293,12 @@ def test_otlp_http_exporter_endpoint(self):
296293
"opentelemetry.exporter.otlp.proto.http._log_exporter": mock_log_module,
297294
},
298295
):
299-
config = LogRecordExporterConfig(
300-
otlp_http=OtlpHttpExporterConfig(
296+
config = {
297+
"otlp_http": OtlpHttpExporterConfig(
301298
endpoint="http://collector:4318",
302299
timeout=5000,
303300
)
304-
)
301+
}
305302
_create_log_record_exporter(config)
306303

307304
mock_exporter_cls.assert_called_once()
@@ -324,13 +321,13 @@ def test_otlp_http_exporter_headers(self):
324321
"opentelemetry.exporter.otlp.proto.http._log_exporter": mock_log_module,
325322
},
326323
):
327-
config = LogRecordExporterConfig(
328-
otlp_http=OtlpHttpExporterConfig(
324+
config = {
325+
"otlp_http": OtlpHttpExporterConfig(
329326
headers=[
330327
NameStringValuePair(name="x-api-key", value="secret")
331328
]
332329
)
333-
)
330+
}
334331
_create_log_record_exporter(config)
335332

336333
call_kwargs = mock_exporter_cls.call_args.kwargs
@@ -350,12 +347,12 @@ def test_otlp_grpc_exporter_endpoint(self):
350347
"opentelemetry.exporter.otlp.proto.grpc._log_exporter": mock_grpc_log_module,
351348
},
352349
):
353-
config = LogRecordExporterConfig(
354-
otlp_grpc=OtlpGrpcExporterConfig(
350+
config = {
351+
"otlp_grpc": OtlpGrpcExporterConfig(
355352
endpoint="http://collector:4317",
356353
timeout=10000,
357354
)
358-
)
355+
}
359356
_create_log_record_exporter(config)
360357

361358
mock_exporter_cls.assert_called_once()

opentelemetry-sdk/tests/_configuration/test_meter_provider.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@
6363
from opentelemetry.sdk._configuration.models import (
6464
PeriodicMetricReader as PeriodicMetricReaderConfig,
6565
)
66-
from opentelemetry.sdk._configuration.models import (
67-
PushMetricExporter as PushMetricExporterConfig,
68-
)
6966
from opentelemetry.sdk._configuration.models import (
7067
View as ViewConfig,
7168
)
@@ -132,9 +129,7 @@ def test_none_config_does_not_read_interval_env_var(self):
132129
readers=[
133130
MetricReaderConfig(
134131
periodic=PeriodicMetricReaderConfig(
135-
exporter=PushMetricExporterConfig(
136-
console=ConsoleMetricExporterConfig()
137-
)
132+
exporter={"console": ConsoleMetricExporterConfig()}
138133
)
139134
)
140135
]
@@ -188,7 +183,7 @@ def _make_periodic_config(exporter_config, interval=None, timeout=None):
188183

189184
def test_console_exporter(self):
190185
config = self._make_periodic_config(
191-
PushMetricExporterConfig(console=ConsoleMetricExporterConfig())
186+
{"console": ConsoleMetricExporterConfig()}
192187
)
193188
provider = create_meter_provider(config)
194189
reader = provider._sdk_config.metric_readers[0]
@@ -197,23 +192,23 @@ def test_console_exporter(self):
197192

198193
def test_periodic_reader_default_interval(self):
199194
config = self._make_periodic_config(
200-
PushMetricExporterConfig(console=ConsoleMetricExporterConfig())
195+
{"console": ConsoleMetricExporterConfig()}
201196
)
202197
provider = create_meter_provider(config)
203198
reader = provider._sdk_config.metric_readers[0]
204199
self.assertEqual(reader._export_interval_millis, 60000.0)
205200

206201
def test_periodic_reader_default_timeout(self):
207202
config = self._make_periodic_config(
208-
PushMetricExporterConfig(console=ConsoleMetricExporterConfig())
203+
{"console": ConsoleMetricExporterConfig()}
209204
)
210205
provider = create_meter_provider(config)
211206
reader = provider._sdk_config.metric_readers[0]
212207
self.assertEqual(reader._export_timeout_millis, 30000.0)
213208

214209
def test_periodic_reader_explicit_interval(self):
215210
config = self._make_periodic_config(
216-
PushMetricExporterConfig(console=ConsoleMetricExporterConfig()),
211+
{"console": ConsoleMetricExporterConfig()},
217212
interval=5000,
218213
)
219214
provider = create_meter_provider(config)
@@ -222,7 +217,7 @@ def test_periodic_reader_explicit_interval(self):
222217

223218
def test_periodic_reader_explicit_timeout(self):
224219
config = self._make_periodic_config(
225-
PushMetricExporterConfig(console=ConsoleMetricExporterConfig()),
220+
{"console": ConsoleMetricExporterConfig()},
226221
timeout=10000,
227222
)
228223
provider = create_meter_provider(config)
@@ -231,7 +226,7 @@ def test_periodic_reader_explicit_timeout(self):
231226

232227
def test_otlp_http_missing_package_raises(self):
233228
config = self._make_periodic_config(
234-
PushMetricExporterConfig(otlp_http=OtlpHttpMetricExporterConfig())
229+
{"otlp_http": OtlpHttpMetricExporterConfig()}
235230
)
236231
with patch.dict(
237232
sys.modules,
@@ -260,11 +255,11 @@ def test_otlp_http_created_with_endpoint(self):
260255
},
261256
):
262257
config = self._make_periodic_config(
263-
PushMetricExporterConfig(
264-
otlp_http=OtlpHttpMetricExporterConfig(
258+
{
259+
"otlp_http": OtlpHttpMetricExporterConfig(
265260
endpoint="http://localhost:4318"
266261
)
267-
)
262+
}
268263
)
269264
create_meter_provider(config)
270265

@@ -276,7 +271,7 @@ def test_otlp_http_created_with_endpoint(self):
276271

277272
def test_otlp_grpc_missing_package_raises(self):
278273
config = self._make_periodic_config(
279-
PushMetricExporterConfig(otlp_grpc=OtlpGrpcMetricExporterConfig())
274+
{"otlp_grpc": OtlpGrpcMetricExporterConfig()}
280275
)
281276
with patch.dict(
282277
sys.modules,
@@ -302,7 +297,7 @@ def test_no_reader_type_raises(self):
302297
create_meter_provider(config)
303298

304299
def test_no_exporter_type_raises(self):
305-
config = self._make_periodic_config(PushMetricExporterConfig())
300+
config = self._make_periodic_config({})
306301
with self.assertRaises(ConfigurationError):
307302
create_meter_provider(config)
308303

@@ -331,16 +326,12 @@ def test_multiple_readers(self):
331326
readers=[
332327
MetricReaderConfig(
333328
periodic=PeriodicMetricReaderConfig(
334-
exporter=PushMetricExporterConfig(
335-
console=ConsoleMetricExporterConfig()
336-
)
329+
exporter={"console": ConsoleMetricExporterConfig()}
337330
)
338331
),
339332
MetricReaderConfig(
340333
periodic=PeriodicMetricReaderConfig(
341-
exporter=PushMetricExporterConfig(
342-
console=ConsoleMetricExporterConfig()
343-
)
334+
exporter={"console": ConsoleMetricExporterConfig()}
344335
)
345336
),
346337
]
@@ -356,12 +347,12 @@ def _make_console_config(temporality=None, histogram_agg=None):
356347
readers=[
357348
MetricReaderConfig(
358349
periodic=PeriodicMetricReaderConfig(
359-
exporter=PushMetricExporterConfig(
360-
console=ConsoleMetricExporterConfig(
350+
exporter={
351+
"console": ConsoleMetricExporterConfig(
361352
temporality_preference=temporality,
362353
default_histogram_aggregation=histogram_agg,
363354
)
364-
)
355+
}
365356
)
366357
)
367358
]

0 commit comments

Comments
 (0)