From e8f4c2c0cc5efd310e2c5ffcce676beea70d8d0d Mon Sep 17 00:00:00 2001 From: Nathaniel Ruiz Nowell Date: Wed, 23 Sep 2020 20:22:00 -0700 Subject: [PATCH] Update tests to use new ids generator --- .../tests/test_flask_example.py | 6 ++--- .../tests/test_datadog_format.py | 9 ++++--- .../tests/trace/propagation/test_b3_format.py | 25 +++++++++++++------ opentelemetry-sdk/tests/trace/test_trace.py | 9 ++++--- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/docs/examples/opentelemetry-example-app/tests/test_flask_example.py b/docs/examples/opentelemetry-example-app/tests/test_flask_example.py index bc903a9c60c..124a79c0eff 100644 --- a/docs/examples/opentelemetry-example-app/tests/test_flask_example.py +++ b/docs/examples/opentelemetry-example-app/tests/test_flask_example.py @@ -21,7 +21,6 @@ import opentelemetry_example_app.flask_example as flask_example from opentelemetry import trace -from opentelemetry.sdk import trace as trace_sdk class TestFlaskExample(unittest.TestCase): @@ -46,7 +45,8 @@ def tearDown(self): self.send_patcher.stop() def test_full_path(self): - trace_id = trace_sdk.generate_trace_id() + ids_generator = trace.RandomIdsGenerator() + trace_id = ids_generator.generate_trace_id() # We need to use the Werkzeug test app because # The headers are injected at the wsgi layer. # The flask test app will not include these, and @@ -58,7 +58,7 @@ def test_full_path(self): headers={ "traceparent": "00-{:032x}-{:016x}-{:02x}".format( trace_id, - trace_sdk.generate_span_id(), + ids_generator.generate_span_id(), trace.TraceFlags.SAMPLED, ) }, diff --git a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py index 1a398745b8d..994cac2d602 100644 --- a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py +++ b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py @@ -30,11 +30,12 @@ def get_as_list(dict_object, key): class TestDatadogFormat(unittest.TestCase): @classmethod def setUpClass(cls): + ids_generator = trace_api.RandomIdsGenerator() cls.serialized_trace_id = propagator.format_trace_id( - trace.generate_trace_id() + ids_generator.generate_trace_id() ) cls.serialized_parent_id = propagator.format_span_id( - trace.generate_span_id() + ids_generator.generate_span_id() ) cls.serialized_origin = "origin-service" @@ -107,7 +108,7 @@ def test_context_propagation(self): "child", trace_api.SpanContext( parent_context.trace_id, - trace.generate_span_id(), + trace_api.RandomIdsGenerator().generate_span_id(), is_remote=False, trace_flags=parent_context.trace_flags, trace_state=parent_context.trace_state, @@ -152,7 +153,7 @@ def test_sampling_priority_auto_reject(self): "child", trace_api.SpanContext( parent_context.trace_id, - trace.generate_span_id(), + trace_api.RandomIdsGenerator().generate_span_id(), is_remote=False, trace_flags=parent_context.trace_flags, trace_state=parent_context.trace_state, diff --git a/opentelemetry-sdk/tests/trace/propagation/test_b3_format.py b/opentelemetry-sdk/tests/trace/propagation/test_b3_format.py index 77834adec97..07b3010087a 100644 --- a/opentelemetry-sdk/tests/trace/propagation/test_b3_format.py +++ b/opentelemetry-sdk/tests/trace/propagation/test_b3_format.py @@ -38,7 +38,7 @@ def get_child_parent_new_carrier(old_carrier): "child", trace_api.SpanContext( parent_context.trace_id, - trace.generate_span_id(), + trace_api.RandomIdsGenerator().generate_span_id(), is_remote=False, trace_flags=parent_context.trace_flags, trace_state=parent_context.trace_state, @@ -56,14 +56,15 @@ def get_child_parent_new_carrier(old_carrier): class TestB3Format(unittest.TestCase): @classmethod def setUpClass(cls): + ids_generator = trace_api.RandomIdsGenerator() cls.serialized_trace_id = b3_format.format_trace_id( - trace.generate_trace_id() + ids_generator.generate_trace_id() ) cls.serialized_span_id = b3_format.format_span_id( - trace.generate_span_id() + ids_generator.generate_span_id() ) cls.serialized_parent_id = b3_format.format_span_id( - trace.generate_span_id() + ids_generator.generate_span_id() ) def test_extract_multi_header(self): @@ -246,8 +247,12 @@ def test_missing_trace_id(self): span_context = trace_api.get_current_span(ctx).get_context() self.assertEqual(span_context.trace_id, trace_api.INVALID_TRACE_ID) - @patch("opentelemetry.sdk.trace.propagation.b3_format.generate_trace_id") - @patch("opentelemetry.sdk.trace.propagation.b3_format.generate_span_id") + @patch( + "opentelemetry.sdk.trace.propagation.b3_format.trace.RandomIdsGenerator.generate_trace_id" + ) + @patch( + "opentelemetry.sdk.trace.propagation.b3_format.trace.RandomIdsGenerator.generate_span_id" + ) def test_invalid_trace_id( self, mock_generate_span_id, mock_generate_trace_id ): @@ -268,8 +273,12 @@ def test_invalid_trace_id( self.assertEqual(span_context.trace_id, 1) self.assertEqual(span_context.span_id, 2) - @patch("opentelemetry.sdk.trace.propagation.b3_format.generate_trace_id") - @patch("opentelemetry.sdk.trace.propagation.b3_format.generate_span_id") + @patch( + "opentelemetry.sdk.trace.propagation.b3_format.trace.RandomIdsGenerator.generate_trace_id" + ) + @patch( + "opentelemetry.sdk.trace.propagation.b3_format.trace.RandomIdsGenerator.generate_span_id" + ) def test_invalid_span_id( self, mock_generate_span_id, mock_generate_trace_id ): diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index fdf85ef19b5..8c5544578d4 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -615,14 +615,15 @@ def test_invalid_event_attributes(self): self.assertEqual(root.events[3].attributes, {"attr2": (1, 2)}) def test_links(self): + ids_generator = trace_api.RandomIdsGenerator() other_context1 = trace_api.SpanContext( - trace_id=trace.generate_trace_id(), - span_id=trace.generate_span_id(), + trace_id=ids_generator.generate_trace_id(), + span_id=ids_generator.generate_span_id(), is_remote=False, ) other_context2 = trace_api.SpanContext( - trace_id=trace.generate_trace_id(), - span_id=trace.generate_span_id(), + trace_id=ids_generator.generate_trace_id(), + span_id=ids_generator.generate_span_id(), is_remote=False, )