Skip to content

Commit a6dcd1c

Browse files
committed
Fix tests
1 parent c500b15 commit a6dcd1c

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _load_credential_from_envvar(
126126
if credential_env:
127127
credentials = _import_config_component(
128128
credential_env, "opentelemetry_otlp_credential_provider"
129-
)()
129+
)
130130
if isinstance(credentials, ChannelCredentials):
131131
return ("credentials", credentials)
132132
elif isinstance(credentials, Session):
@@ -271,13 +271,13 @@ def _init_exporter(
271271
if (
272272
credential_key == "credentials"
273273
and "credentials" in params
274-
and isinstance(credential, params["credentials"].annotation)
274+
and "ChannelCredentials" in params["credentials"].annotation
275275
):
276276
return exporter_class(credentials=credential, **exporter_args_map)
277277
if (
278278
credential_key == "session"
279279
and "session" in params
280-
and isinstance(credential, params["session"].annotation)
280+
and "Session" in params["session"].annotation
281281
):
282282
return exporter_class(session=credential, **exporter_args_map)
283283
return exporter_class(**exporter_args_map)

opentelemetry-sdk/tests/test_configurator.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from opentelemetry import trace
3030
from opentelemetry.context import Context
31-
from opentelemetry.sdk.environment_variables import OTEL_PYTHON_EXPORTER_OTLP_LOGS_CREDENTIAL_PROVIDER
31+
from opentelemetry.sdk.environment_variables import OTEL_PYTHON_EXPORTER_OTLP_TRACES_CREDENTIAL_PROVIDER, OTEL_PYTHON_EXPORTER_OTLP_METRICS_CREDENTIAL_PROVIDER
3232
from opentelemetry.environment_variables import OTEL_PYTHON_ID_GENERATOR
3333
from opentelemetry.sdk._configuration import (
3434
_EXPORTER_OTLP,
@@ -45,6 +45,7 @@
4545
_init_exporter,
4646
_init_metrics,
4747
_init_tracing,
48+
_load_credential_from_envvar,
4849
_initialize_components,
4950
_OTelSDKConfigurator,
5051
)
@@ -182,7 +183,8 @@ def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
182183

183184

184185
class DummyOTLPMetricExporter:
185-
def __init__(self, compression: str | None = None, session: Session | None, *args, **kwargs):
186+
def __init__(self, compression: str | None = None, session: Session | None = None, *args, **kwargs):
187+
self.session = session
186188
self.export_called = False
187189
self.compression = compression
188190

@@ -209,6 +211,7 @@ def shutdown(self):
209211
class OTLPSpanExporter:
210212
def __init__(self, compression: str | None = None, credentials: ChannelCredentials | None = None, *args, **kwargs):
211213
self.compression = compression
214+
self.credentials = credentials
212215

213216

214217
class DummyOTLPLogExporter(LogExporter):
@@ -412,15 +415,36 @@ def test_trace_init_custom_id_generator(self, mock_entry_points):
412415
self.assertIsInstance(provider.id_generator, CustomIdGenerator)
413416

414417

415-
@patch.dict(environ, {OTEL_PYTHON_EXPORTER_OTLP_LOGS_CREDENTIAL_PROVIDER: "custom_session"})
418+
@patch.dict(environ, {OTEL_PYTHON_EXPORTER_OTLP_METRICS_CREDENTIAL_PROVIDER: "custom_session"})
416419
@patch("opentelemetry.sdk._configuration.entry_points")
417-
def check_that_credential_envvar_gets_passed_to_exporter(self, mock_entry_points):
420+
def test_that_session_gets_passed_to_exporter(self, mock_entry_points):
421+
# Should not be used, trace specific version should override.
422+
session_for_all_signals = Session()
423+
session_for_metrics_only = Session()
418424
mock_entry_points.configure_mock(
419425
return_value=[
420-
IterEntryPoint("custom_session", Session())
426+
IterEntryPoint("custom_session", session_for_metrics_only)
421427
]
422428
)
423-
exporter = _init_exporter('traces', None, OTLPSpanExporter)
429+
exporter = _init_exporter('metrics', {}, DummyOTLPMetricExporter, otlp_credential_param_for_all_signal_types=("session", session_for_all_signals))
430+
assert exporter.session is session_for_metrics_only
431+
assert exporter.session is not session_for_all_signals
432+
433+
434+
@patch.dict(environ, {OTEL_PYTHON_EXPORTER_OTLP_TRACES_CREDENTIAL_PROVIDER: "custom_credential"})
435+
@patch("opentelemetry.sdk._configuration.entry_points")
436+
def test_that_credential_gets_passed_to_exporter(self, mock_entry_points):
437+
# Should not be used, trace specific version should override.
438+
credential_for_all_signals = ChannelCredentials(None)
439+
credential_for_trace_only = ChannelCredentials(None)
440+
mock_entry_points.configure_mock(
441+
return_value=[
442+
IterEntryPoint("custom_credential", credential_for_trace_only)
443+
]
444+
)
445+
exporter = _init_exporter('traces', {}, OTLPSpanExporter, otlp_credential_param_for_all_signal_types=credential_for_all_signals)
446+
assert exporter.credentials is credential_for_trace_only
447+
assert exporter.credentials is not credential_for_all_signals
424448

425449
@patch.dict(
426450
"os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"}

0 commit comments

Comments
 (0)