Skip to content

Commit

Permalink
Opencensus exporters should populate service_name from Span Resource (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Jun 8, 2021
1 parent 3ae6c2e commit d26699d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")

0 comments on commit d26699d

Please sign in to comment.