From 0fe9096bfc8380e31caced930a1cf7bf13fbcdd0 Mon Sep 17 00:00:00 2001 From: alrex Date: Tue, 9 Feb 2021 12:31:03 -0800 Subject: [PATCH] Update span interface usage (#324) --- .../src/opentelemetry/exporter/datadog/exporter.py | 8 ++++++-- .../tests/test_datadog_exporter.py | 8 ++++---- .../tests/test_botocore_instrumentation.py | 12 ++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py index 129e0d0c48..f76231a459 100644 --- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py +++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py @@ -259,10 +259,14 @@ def _get_origin(span): def _get_sampling_rate(span): ctx = span.get_span_context() + tracer_provider = trace_api.get_tracer_provider() + if not hasattr(tracer_provider, "sampler"): + return None + sampler = tracer_provider.sampler return ( - span.sampler.rate + sampler.rate if ctx.trace_flags.sampled - and isinstance(span.sampler, sampling.TraceIdRatioBased) + and isinstance(sampler, sampling.TraceIdRatioBased) else None ) diff --git a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py index 4a576c5de1..9cc7ccbb21 100644 --- a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py +++ b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_exporter.py @@ -580,11 +580,11 @@ def test_sampling_rate(self): is_remote=False, trace_flags=trace_api.TraceFlags(trace_api.TraceFlags.SAMPLED), ) - sampler = sampling.TraceIdRatioBased(0.5) - - span = trace._Span( - name="sampled", context=context, parent=None, sampler=sampler + trace_api.get_tracer_provider().sampler = sampling.TraceIdRatioBased( + 0.5 ) + + span = trace._Span(name="sampled", context=context, parent=None) span.start() span.end() diff --git a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py index 72f5f5fea9..a0c1c4ac36 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py @@ -201,12 +201,12 @@ def test_sqs_client(self): self.assertEqual(len(spans), 1) actual = span.attributes self.assertRegex(actual["aws.request_id"], r"[A-Z0-9]{52}") - del actual["aws.request_id"] self.assertEqual( actual, { "aws.operation": "ListQueues", "aws.region": "us-east-1", + "aws.request_id": actual["aws.request_id"], "aws.service": "sqs", "retry_attempts": 0, "http.status_code": 200, @@ -232,12 +232,12 @@ def test_sqs_send_message(self): self.assertRegex( create_queue_attributes["aws.request_id"], r"[A-Z0-9]{52}" ) - del create_queue_attributes["aws.request_id"] self.assertEqual( create_queue_attributes, { "aws.operation": "CreateQueue", "aws.region": "us-east-1", + "aws.request_id": create_queue_attributes["aws.request_id"], "aws.service": "sqs", "retry_attempts": 0, "http.status_code": 200, @@ -247,13 +247,13 @@ def test_sqs_send_message(self): self.assertRegex( send_msg_attributes["aws.request_id"], r"[A-Z0-9]{52}" ) - del send_msg_attributes["aws.request_id"] self.assertEqual( send_msg_attributes, { "aws.operation": "SendMessage", "aws.queue_url": response["QueueUrl"], "aws.region": "us-east-1", + "aws.request_id": send_msg_attributes["aws.request_id"], "aws.service": "sqs", "retry_attempts": 0, "http.status_code": 200, @@ -468,13 +468,13 @@ def test_dynamodb_client(self): self.assertRegex( create_table_attributes["aws.request_id"], r"[A-Z0-9]{52}" ) - del create_table_attributes["aws.request_id"] self.assertEqual( create_table_attributes, { "aws.operation": "CreateTable", "aws.region": "us-west-2", "aws.service": "dynamodb", + "aws.request_id": create_table_attributes["aws.request_id"], "aws.table_name": "test_table_name", "retry_attempts": 0, "http.status_code": 200, @@ -484,12 +484,12 @@ def test_dynamodb_client(self): self.assertRegex( put_item_attributes["aws.request_id"], r"[A-Z0-9]{52}" ) - del put_item_attributes["aws.request_id"] self.assertEqual( put_item_attributes, { "aws.operation": "PutItem", "aws.region": "us-west-2", + "aws.request_id": put_item_attributes["aws.request_id"], "aws.service": "dynamodb", "aws.table_name": "test_table_name", "retry_attempts": 0, @@ -500,12 +500,12 @@ def test_dynamodb_client(self): self.assertRegex( get_item_attributes["aws.request_id"], r"[A-Z0-9]{52}" ) - del get_item_attributes["aws.request_id"] self.assertEqual( get_item_attributes, { "aws.operation": "GetItem", "aws.region": "us-west-2", + "aws.request_id": get_item_attributes["aws.request_id"], "aws.service": "dynamodb", "aws.table_name": "test_table_name", "retry_attempts": 0,