diff --git a/CHANGELOG.md b/CHANGELOG.md index ad423b7f471..d4cf4a1ccd9 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 + ([#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 ### 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..43d9bcd430b 100644 --- a/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py +++ b/exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py @@ -34,6 +34,10 @@ # pylint: disable=no-member class TestCollectorSpanExporter(unittest.TestCase): + @mock.patch( + "opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER", + None, + ) def test_constructor(self): mock_get_node = mock.Mock() patch = mock.patch( @@ -324,3 +328,46 @@ def test_export(self): self.assertEqual( getattr(output_identifier, "host_name"), "testHostName" ) + + @mock.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")