From c854ba8f604656f6946da35b652a4faeaf421cc4 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 1 Feb 2021 20:50:08 -0800 Subject: [PATCH 1/3] resource are immutable --- .../instrumentation/boto/__init__.py | 12 +--- .../tests/test_boto_instrumentation.py | 59 ++++++------------- 2 files changed, 19 insertions(+), 52 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py b/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py index 58dee0ba7f..c144ac59fb 100644 --- a/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py @@ -122,18 +122,10 @@ def _common_request( # pylint: disable=too-many-locals with self._tracer.start_as_current_span( "{}.command".format(endpoint_name), kind=SpanKind.CONSUMER, ) as span: + span.set_attribute("endpoint", endpoint_name) if args: http_method = args[0] - span.resource = Resource( - attributes={ - "endpoint": endpoint_name, - "http_method": http_method.lower(), - } - ) - else: - span.resource = Resource( - attributes={"endpoint": endpoint_name} - ) + span.set_attribute("http_method", http_method.lower()) # Original func returns a boto.connection.HTTPResponse object result = original_func(*args, **kwargs) diff --git a/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py b/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py index cb45514c79..bbdfb34d69 100644 --- a/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py @@ -71,12 +71,8 @@ def test_ec2_client(self): span = spans[1] self.assertEqual(span.attributes["aws.operation"], "RunInstances") assert_span_http_status_code(span, 200) - self.assertEqual( - span.resource, - Resource( - attributes={"endpoint": "ec2", "http_method": "runinstances"} - ), - ) + self.assertEqual(span.attributes["endpoint"], "ec2") + self.assertEqual(span.attributes["http_method"], "runinstances") self.assertEqual(span.attributes["http.method"], "POST") self.assertEqual(span.attributes["aws.region"], "us-west-2") self.assertEqual(span.name, "ec2.command") @@ -147,10 +143,8 @@ def test_s3_client(self): self.assertEqual(len(spans), 3) span = spans[2] assert_span_http_status_code(span, 200) - self.assertEqual( - span.resource, - Resource(attributes={"endpoint": "s3", "http_method": "head"}), - ) + self.assertEqual(span.attributes["endpoint"], "s3") + self.assertEqual(span.attributes["http_method"], "head") self.assertEqual(span.attributes["http.method"], "HEAD") self.assertEqual(span.attributes["aws.operation"], "head_bucket") self.assertEqual(span.name, "s3.command") @@ -162,10 +156,8 @@ def test_s3_client(self): spans = self.memory_exporter.get_finished_spans() assert spans span = spans[2] - self.assertEqual( - span.resource, - Resource(attributes={"endpoint": "s3", "http_method": "head"}), - ) + self.assertEqual(spans[2].attributes["endpoint"], "s3") + self.assertEqual(spans[2].attributes["http_method"], "head") @mock_s3_deprecated def test_s3_put(self): @@ -182,24 +174,18 @@ def test_s3_put(self): self.assertEqual(len(spans), 3) self.assertEqual(spans[0].attributes["aws.operation"], "create_bucket") assert_span_http_status_code(spans[0], 200) - self.assertEqual( - spans[0].resource, - Resource(attributes={"endpoint": "s3", "http_method": "put"}), - ) + self.assertEqual(spans[0].attributes["endpoint"], "s3") + self.assertEqual(spans[0].attributes["http_method"], "put") # get bucket self.assertEqual(spans[1].attributes["aws.operation"], "head_bucket") - self.assertEqual( - spans[1].resource, - Resource(attributes={"endpoint": "s3", "http_method": "head"}), - ) + self.assertEqual(spans[1].attributes["endpoint"], "s3") + self.assertEqual(spans[1].attributes["http_method"], "head") # put object self.assertEqual( spans[2].attributes["aws.operation"], "_send_file_internal" ) - self.assertEqual( - spans[2].resource, - Resource(attributes={"endpoint": "s3", "http_method": "put"}), - ) + self.assertEqual(spans[2].attributes["endpoint"], "s3") + self.assertEqual(spans[2].attributes["http_method"], "put") @mock_lambda_deprecated def test_unpatch(self): @@ -239,10 +225,8 @@ def test_lambda_client(self): self.assertEqual(len(spans), 2) span = spans[0] assert_span_http_status_code(span, 200) - self.assertEqual( - span.resource, - Resource(attributes={"endpoint": "lambda", "http_method": "get"}), - ) + self.assertEqual(span.attributes["endpoint"], "lambda") + self.assertEqual(span.attributes["http_method"], "get") self.assertEqual(span.attributes["http.method"], "GET") self.assertEqual(span.attributes["aws.region"], "us-east-2") self.assertEqual(span.attributes["aws.operation"], "list_functions") @@ -256,15 +240,8 @@ def test_sts_client(self): spans = self.memory_exporter.get_finished_spans() assert spans span = spans[0] - self.assertEqual( - span.resource, - Resource( - attributes={ - "endpoint": "sts", - "http_method": "getfederationtoken", - } - ), - ) + self.assertEqual(span.attributes["endpoint"], "sts") + self.assertEqual(span.attributes["http_method"], "getfederationtoken") self.assertEqual(span.attributes["aws.region"], "us-west-2") self.assertEqual( span.attributes["aws.operation"], "GetFederationToken" @@ -288,7 +265,5 @@ def test_elasticache_client(self): spans = self.memory_exporter.get_finished_spans() assert spans span = spans[0] - self.assertEqual( - span.resource, Resource(attributes={"endpoint": "elasticcache"}) - ) + self.assertEqual(span.attributes["endpoint"], "elasticcache") self.assertEqual(span.attributes["aws.region"], "us-west-2") From 2379533256b9ab3d0483412e6be660a9deb2db86 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 1 Feb 2021 20:59:11 -0800 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index adfaace015..7e018a1f4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Remove `component` span attribute in instrumentations. `opentelemetry-instrumentation-aiopg`, `opentelemetry-instrumentation-dbapi` Remove unused `database_type` parameter from `trace_integration` function. ([#301](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/301)) +- `opentelemetry-instrumentation-boto` updated to set span attributes instead of overriding the resource. + ([#310](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/310)) ## [0.17b0](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.17b0) - 2021-01-20 From ac999f59db5d7b224af0adfa17a65cdda2a0692a Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Thu, 4 Feb 2021 08:20:33 -0800 Subject: [PATCH 3/3] fix lint --- .../tests/test_boto_instrumentation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py b/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py index bbdfb34d69..25ff4b0d46 100644 --- a/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py @@ -28,7 +28,6 @@ ) from opentelemetry.instrumentation.boto import BotoInstrumentor -from opentelemetry.sdk.resources import Resource from opentelemetry.test.test_base import TestBase