From 3507262d5c79fe8b79676b67dad160a911c57662 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 7 Jun 2021 11:30:30 -0700 Subject: [PATCH 1/3] opencensus --- CHANGELOG.md | 4 ++ .../opencensus/trace_exporter/__init__.py | 10 +++++ .../tests/test_otcollector_trace_exporter.py | 41 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad423b7f471..745864e6dfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.3.0-0.22b0...HEAD) +### Changed +- Updated `opentelemetry-opencensus-exporter` to use `service_name` of spans instead of resource + ([#1854](https://github.com/open-telemetry/opentelemetry-python/pull/1854)) + ## [1.3.0-0.22b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.3.0-0.22b0) - 2021-06-01 ### Added diff --git a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py index b20084888f0..f1987ffb985 100644 --- a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py @@ -66,9 +66,19 @@ def __init__( else: self.client = client + self.host_name = host_name self.node = utils.get_node(service_name, host_name) def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult: + # Populate service_name from first span + # We restrict any SpanProcessor to be only associated with a single + # TracerProvider, so it is safe to assume that all Spans in a single + # batch all originate from one TracerProvider (and in turn have all + # the same service_name) + if spans: + service_name = spans[0].resource.attributes.get(SERVICE_NAME) + if service_name: + self.node = utils.get_node(service_name, self.host_name) try: responses = self.client.Export(self.generate_span_requests(spans)) diff --git a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py index 222a94d60b3..7c530dff889 100644 --- a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py +++ b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py @@ -14,6 +14,7 @@ import unittest from unittest import mock +from unittest.mock import patch import grpc from google.protobuf.timestamp_pb2 import Timestamp @@ -34,6 +35,8 @@ # pylint: disable=no-member class TestCollectorSpanExporter(unittest.TestCase): + + @patch("opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", None) def test_constructor(self): mock_get_node = mock.Mock() patch = mock.patch( @@ -324,3 +327,41 @@ def test_export(self): self.assertEqual( getattr(output_identifier, "host_name"), "testHostName" ) + + @patch("opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", None) + def test_export_service_name(self): + trace_api.set_tracer_provider( + TracerProvider( + resource=Resource.create({SERVICE_NAME: "testServiceName"}) + ) + ) + mock_client = mock.MagicMock() + mock_export = mock.MagicMock() + mock_client.Export = mock_export + host_name = "testHostName" + collector_exporter = OpenCensusSpanExporter( + client=mock_client, host_name=host_name + ) + self.assertEqual(collector_exporter.node.service_info.name, "testServiceName") + + trace_id = 0x6E0C63257DE34C926F9EFCD03927272E + span_id = 0x34BF92DEEFC58C92 + span_context = trace_api.SpanContext( + trace_id, + span_id, + is_remote=False, + trace_flags=TraceFlags(TraceFlags.SAMPLED), + ) + resource = Resource.create({SERVICE_NAME: "test"}) + otel_spans = [ + trace._Span( + name="test1", + context=span_context, + kind=trace_api.SpanKind.CLIENT, + resource=resource, + ) + ] + + result_status = collector_exporter.export(otel_spans) + self.assertEqual(SpanExportResult.SUCCESS, result_status) + self.assertEqual(collector_exporter.node.service_info.name, "test") From 5cee0f036d35c7f2e7e7c848ef481ace6faf5e9b Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 7 Jun 2021 15:39:14 -0700 Subject: [PATCH 2/3] lint --- CHANGELOG.md | 2 +- .../tests/test_otcollector_trace_exporter.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 745864e6dfe..d4cf4a1ccd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Updated `opentelemetry-opencensus-exporter` to use `service_name` of spans instead of resource - ([#1854](https://github.com/open-telemetry/opentelemetry-python/pull/1854)) + ([#1897](https://github.com/open-telemetry/opentelemetry-python/pull/1897)) ## [1.3.0-0.22b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.3.0-0.22b0) - 2021-06-01 diff --git a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py index 7c530dff889..f1e8581f18c 100644 --- a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py +++ b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py @@ -35,8 +35,10 @@ # pylint: disable=no-member class TestCollectorSpanExporter(unittest.TestCase): - - @patch("opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", None) + @patch( + "opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", + None, + ) def test_constructor(self): mock_get_node = mock.Mock() patch = mock.patch( @@ -328,7 +330,10 @@ def test_export(self): getattr(output_identifier, "host_name"), "testHostName" ) - @patch("opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", None) + @patch( + "opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", + None, + ) def test_export_service_name(self): trace_api.set_tracer_provider( TracerProvider( @@ -342,7 +347,9 @@ def test_export_service_name(self): collector_exporter = OpenCensusSpanExporter( client=mock_client, host_name=host_name ) - self.assertEqual(collector_exporter.node.service_info.name, "testServiceName") + self.assertEqual( + collector_exporter.node.service_info.name, "testServiceName" + ) trace_id = 0x6E0C63257DE34C926F9EFCD03927272E span_id = 0x34BF92DEEFC58C92 From f28bf4b113e882c6210bbf1f0fec2d47e6bd88cd Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 7 Jun 2021 15:47:52 -0700 Subject: [PATCH 3/3] lint --- .../tests/test_otcollector_trace_exporter.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py index f1e8581f18c..43d9bcd430b 100644 --- a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py +++ b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py @@ -14,7 +14,6 @@ import unittest from unittest import mock -from unittest.mock import patch import grpc from google.protobuf.timestamp_pb2 import Timestamp @@ -35,7 +34,7 @@ # pylint: disable=no-member class TestCollectorSpanExporter(unittest.TestCase): - @patch( + @mock.patch( "opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", None, ) @@ -330,7 +329,7 @@ def test_export(self): getattr(output_identifier, "host_name"), "testHostName" ) - @patch( + @mock.patch( "opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", None, )