|
50 | 50 | MetricReader,
|
51 | 51 | PeriodicExportingMetricReader,
|
52 | 52 | )
|
53 |
| -from opentelemetry.sdk.resources import Resource |
| 53 | +from opentelemetry.sdk.resources import Attributes, Resource |
54 | 54 | from opentelemetry.sdk.trace import TracerProvider
|
55 | 55 | from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter
|
56 | 56 | from opentelemetry.sdk.trace.id_generator import IdGenerator
|
@@ -354,37 +354,61 @@ def _import_id_generator(id_generator_name: str) -> IdGenerator:
|
354 | 354 | raise RuntimeError(f"{id_generator_name} is not an IdGenerator")
|
355 | 355 |
|
356 | 356 |
|
357 |
| -def _initialize_components(auto_instrumentation_version): |
358 |
| - trace_exporters, metric_exporters, log_exporters = _import_exporters( |
359 |
| - _get_exporter_names("traces"), |
360 |
| - _get_exporter_names("metrics"), |
361 |
| - _get_exporter_names("logs"), |
| 357 | +def _initialize_components( |
| 358 | + auto_instrumentation_version: Optional[str] = None, |
| 359 | + trace_exporter_names: Optional[List[str]] = None, |
| 360 | + metric_exporter_names: Optional[List[str]] = None, |
| 361 | + log_exporter_names: Optional[List[str]] = None, |
| 362 | + sampler: Optional[Sampler] = None, |
| 363 | + resource_attributes: Optional[Attributes] = None, |
| 364 | + id_generator: IdGenerator = None, |
| 365 | + logging_enabled: Optional[bool] = None, |
| 366 | +): |
| 367 | + if trace_exporter_names is None: |
| 368 | + trace_exporter_names = [] |
| 369 | + if metric_exporter_names is None: |
| 370 | + metric_exporter_names = [] |
| 371 | + if log_exporter_names is None: |
| 372 | + log_exporter_names = [] |
| 373 | + span_exporters, metric_exporters, log_exporters = _import_exporters( |
| 374 | + trace_exporter_names + _get_exporter_names("traces"), |
| 375 | + metric_exporter_names + _get_exporter_names("metrics"), |
| 376 | + log_exporter_names + _get_exporter_names("logs"), |
362 | 377 | )
|
363 |
| - sampler_name = _get_sampler() |
364 |
| - sampler = _import_sampler(sampler_name) |
365 |
| - id_generator_name = _get_id_generator() |
366 |
| - id_generator = _import_id_generator(id_generator_name) |
367 |
| - # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name |
368 |
| - # from the env variable else defaults to "unknown_service" |
369 |
| - auto_resource = {} |
| 378 | + if sampler is None: |
| 379 | + sampler_name = _get_sampler() |
| 380 | + sampler = _import_sampler(sampler_name) |
| 381 | + if id_generator is None: |
| 382 | + id_generator_name = _get_id_generator() |
| 383 | + id_generator = _import_id_generator(id_generator_name) |
| 384 | + if resource_attributes is None: |
| 385 | + resource_attributes = {} |
370 | 386 | # populate version if using auto-instrumentation
|
371 | 387 | if auto_instrumentation_version:
|
372 |
| - auto_resource[ResourceAttributes.TELEMETRY_AUTO_VERSION] = ( |
| 388 | + resource_attributes[ResourceAttributes.TELEMETRY_AUTO_VERSION] = ( |
373 | 389 | auto_instrumentation_version
|
374 | 390 | )
|
375 |
| - resource = Resource.create(auto_resource) |
| 391 | + # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name |
| 392 | + # from the env variable else defaults to "unknown_service" |
| 393 | + resource = Resource.create(resource_attributes) |
376 | 394 |
|
377 | 395 | _init_tracing(
|
378 |
| - exporters=trace_exporters, |
| 396 | + exporters=span_exporters, |
379 | 397 | id_generator=id_generator,
|
380 | 398 | sampler=sampler,
|
381 | 399 | resource=resource,
|
382 | 400 | )
|
383 | 401 | _init_metrics(metric_exporters, resource)
|
384 |
| - logging_enabled = os.getenv( |
385 |
| - _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false" |
386 |
| - ) |
387 |
| - if logging_enabled.strip().lower() == "true": |
| 402 | + if logging_enabled is None: |
| 403 | + logging_enabled = ( |
| 404 | + os.getenv( |
| 405 | + _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false" |
| 406 | + ) |
| 407 | + .strip() |
| 408 | + .lower() |
| 409 | + == "true" |
| 410 | + ) |
| 411 | + if logging_enabled: |
388 | 412 | _init_logging(log_exporters, resource)
|
389 | 413 |
|
390 | 414 |
|
@@ -428,4 +452,4 @@ class _OTelSDKConfigurator(_BaseConfigurator):
|
428 | 452 | """
|
429 | 453 |
|
430 | 454 | def _configure(self, **kwargs):
|
431 |
| - _initialize_components(kwargs.get("auto_instrumentation_version")) |
| 455 | + _initialize_components(**kwargs) |
0 commit comments