Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add another test
  • Loading branch information
srikanthccv committed Feb 2, 2023
commit 548db263a20b6014f0c347b8d8e60f8ab131ba0d
89 changes: 41 additions & 48 deletions opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ def should_sample(
class CustomRatioSampler(TraceIdRatioBased):
def __init__(self, ratio):
if not isinstance(ratio, float):
raise ValueError(
"CustomRatioSampler ratio argument is not a float."
)
raise ValueError("CustomRatioSampler ratio argument is not a float.")
self.ratio = ratio
super().__init__(ratio)

Expand Down Expand Up @@ -302,9 +300,7 @@ def tearDown(self):
self.set_provider_patcher.stop()

# pylint: disable=protected-access
@patch.dict(
environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"}
)
@patch.dict(environ, {"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-test-service"})
def test_trace_init_default(self):

auto_resource = Resource.create(
Expand All @@ -324,9 +320,7 @@ def test_trace_init_default(self):
self.assertIsInstance(provider.id_generator, RandomIdGenerator)
self.assertIsInstance(provider.processor, Processor)
self.assertIsInstance(provider.processor.exporter, Exporter)
self.assertEqual(
provider.processor.exporter.service_name, "my-test-service"
)
self.assertEqual(provider.processor.exporter.service_name, "my-test-service")
self.assertEqual(
provider.resource.attributes.get("telemetry.auto.version"),
"test-version",
Expand All @@ -337,9 +331,7 @@ def test_trace_init_default(self):
{"OTEL_RESOURCE_ATTRIBUTES": "service.name=my-otlp-test-service"},
)
def test_trace_init_otlp(self):
_init_tracing(
{"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator()
)
_init_tracing({"otlp": OTLPSpanExporter}, id_generator=RandomIdGenerator())

self.assertEqual(self.set_provider_mock.call_count, 1)
provider = self.set_provider_mock.call_args[0][0]
Expand All @@ -358,19 +350,15 @@ def test_trace_init_otlp(self):
@patch("opentelemetry.sdk._configuration.iter_entry_points")
def test_trace_init_custom_id_generator(self, mock_iter_entry_points):
mock_iter_entry_points.configure_mock(
return_value=[
IterEntryPoint("custom_id_generator", CustomIdGenerator)
]
return_value=[IterEntryPoint("custom_id_generator", CustomIdGenerator)]
)
id_generator_name = _get_id_generator()
id_generator = _import_id_generator(id_generator_name)
_init_tracing({}, id_generator=id_generator)
provider = self.set_provider_mock.call_args[0][0]
self.assertIsInstance(provider.id_generator, CustomIdGenerator)

@patch.dict(
"os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"}
)
@patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "non_existent_entry_point"})
def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self):
sampler_name = _get_sampler()
sampler = _import_sampler(sampler_name)
Expand Down Expand Up @@ -447,9 +435,7 @@ def test_trace_init_custom_sampler_with_env_unused_arg(
OTEL_TRACES_SAMPLER_ARG: "0.5",
},
)
def test_trace_init_custom_ratio_sampler_with_env(
self, mock_iter_entry_points
):
def test_trace_init_custom_ratio_sampler_with_env(self, mock_iter_entry_points):
mock_iter_entry_points.configure_mock(
return_value=[
IterEntryPoint(
Expand Down Expand Up @@ -614,9 +600,7 @@ def test_logging_init_exporter(self):
"otlp-service",
)
self.assertIsInstance(provider.processor, DummyLogRecordProcessor)
self.assertIsInstance(
provider.processor.exporter, DummyOTLPLogExporter
)
self.assertIsInstance(provider.processor.exporter, DummyOTLPLogExporter)
logging.getLogger(__name__).error("hello")
self.assertTrue(provider.processor.exporter.export_called)

Expand Down Expand Up @@ -645,6 +629,31 @@ def test_logging_init_enable_env(self, logging_mock, tracing_mock):
self.assertEqual(logging_mock.call_count, 1)
self.assertEqual(tracing_mock.call_count, 1)

@patch.dict(
environ,
{
"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service",
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "True",
},
)
@patch("opentelemetry.sdk._configuration._init_tracing")
@patch("opentelemetry.sdk._configuration._init_logging")
@patch("opentelemetry.sdk._configuration._init_metrics")
def test_initialize_components_resource(
self, metrics_mock, logging_mock, tracing_mock
):
_initialize_components("auto-version")
self.assertEqual(logging_mock.call_count, 1)
self.assertEqual(tracing_mock.call_count, 1)
self.assertEqual(metrics_mock.call_count, 1)

logging_resource = logging_mock.call_args.args[1]
tracing_resource = tracing_mock.call_args.kwargs["resource"]
metrics_resource = metrics_mock.call_args.args[1]
self.assertEqual(logging_resource, tracing_resource)
self.assertEqual(logging_resource, metrics_resource)
self.assertEqual(tracing_resource, metrics_resource)


class TestMetricsInit(TestCase):
def setUp(self):
Expand Down Expand Up @@ -681,9 +690,7 @@ def test_metrics_init_empty(self):
self.assertIsInstance(provider, DummyMeterProvider)
self.assertIsInstance(provider._sdk_config.resource, Resource)
self.assertEqual(
provider._sdk_config.resource.attributes.get(
"telemetry.auto.version"
),
provider._sdk_config.resource.attributes.get("telemetry.auto.version"),
"auto-version",
)

Expand Down Expand Up @@ -717,15 +724,9 @@ class TestExporterNames(TestCase):
},
)
def test_otlp_exporter(self):
self.assertEqual(
_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC]
)
self.assertEqual(
_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]
)
self.assertEqual(
_get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP]
)
self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC])
self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC])
self.assertEqual(_get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP])

@patch.dict(
environ,
Expand All @@ -737,12 +738,8 @@ def test_otlp_exporter(self):
},
)
def test_otlp_custom_exporter(self):
self.assertEqual(
_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]
)
self.assertEqual(
_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]
)
self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP])
self.assertEqual(_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC])

@patch.dict(
environ,
Expand All @@ -756,9 +753,7 @@ def test_otlp_custom_exporter(self):
def test_otlp_exporter_conflict(self):
# Verify that OTEL_*_EXPORTER is used, and a warning is logged
with self.assertLogs(level="WARNING") as logs_context:
self.assertEqual(
_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]
)
self.assertEqual(_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP])
assert len(logs_context.output) == 1

with self.assertLogs(level="WARNING") as logs_context:
Expand All @@ -769,9 +764,7 @@ def test_otlp_exporter_conflict(self):

@patch.dict(environ, {"OTEL_TRACES_EXPORTER": "jaeger,zipkin"})
def test_multiple_exporters(self):
self.assertEqual(
sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"]
)
self.assertEqual(sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"])

@patch.dict(environ, {"OTEL_TRACES_EXPORTER": "none"})
def test_none_exporters(self):
Expand Down