Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opencensus exporters should populate service_name from Span Resource #1897

Merged
merged 4 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
([#1854](https://github.com/open-telemetry/opentelemetry-python/pull/1854))
lzchen marked this conversation as resolved.
Show resolved Hide resolved

## [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 @@ -14,6 +14,7 @@

import unittest
from unittest import mock
from unittest.mock import patch

import grpc
from google.protobuf.timestamp_pb2 import Timestamp
Expand All @@ -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(
Expand Down Expand Up @@ -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")