Skip to content

Zipkin exporter to get service_name from span instead of global tracer_provider #1696

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

Merged
merged 9 commits into from
Mar 16, 2021
Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1671](https://github.com/open-telemetry/opentelemetry-python/pull/1671))
- Initial documentation for environment variables
([#1680](https://github.com/open-telemetry/opentelemetry-python/pull/1680))
- Change Zipkin exporter to obtain service.name from span
([#1696](https://github.com/open-telemetry/opentelemetry-python/pull/1696))

### Removed
- Removed unused `get_hexadecimal_trace_id` and `get_hexadecimal_span_id` methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,19 @@

import requests

from opentelemetry.exporter.zipkin.encoder import Encoder, Protocol
from opentelemetry.exporter.zipkin.encoder import (
DEFAULT_MAX_TAG_VALUE_LENGTH,
Encoder,
Protocol,
)
from opentelemetry.exporter.zipkin.encoder.v1.json import JsonV1Encoder
from opentelemetry.exporter.zipkin.encoder.v2.json import JsonV2Encoder
from opentelemetry.exporter.zipkin.encoder.v2.protobuf import ProtobufEncoder
from opentelemetry.exporter.zipkin.node_endpoint import IpInput, NodeEndpoint
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_ZIPKIN_ENDPOINT,
)
from opentelemetry.sdk.resources import SERVICE_NAME
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
from opentelemetry.trace import Span

Expand Down Expand Up @@ -116,6 +121,15 @@ def __init__(
self.encoder = ProtobufEncoder(max_tag_value_length)

def export(self, spans: Sequence[Span]) -> 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.local_node.service_name = service_name
result = requests.post(
url=self.endpoint,
data=self.encoder.serialize(spans, self.local_node),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,26 @@ def _extract_tags_from_dict(
logger.warning("Could not serialize tag %s", attribute_key)
continue

if self.max_tag_value_length > 0:
if (
self.max_tag_value_length is not None
and self.max_tag_value_length > 0
):
value = value[: self.max_tag_value_length]
tags[attribute_key] = value
return tags

def _extract_tag_value_string_from_sequence(self, sequence: Sequence):
if self.max_tag_value_length == 1:
if self.max_tag_value_length and self.max_tag_value_length == 1:
return None

tag_value_elements = []
running_string_length = (
2 # accounts for array brackets in output string
)
defined_max_tag_value_length = self.max_tag_value_length > 0
defined_max_tag_value_length = (
self.max_tag_value_length is not None
and self.max_tag_value_length > 0
)

for element in sequence:
if isinstance(element, bool):
Expand Down Expand Up @@ -214,7 +220,11 @@ def _extract_annotations_from_events(
for event in events:
attrs = {}
for key, value in event.attributes.items():
if isinstance(value, str) and self.max_tag_value_length > 0:
if (
isinstance(value, str)
and self.max_tag_value_length is not None
and self.max_tag_value_length > 0
):
value = value[: self.max_tag_value_length]
attrs[key] = value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
OTEL_EXPORTER_ZIPKIN_ENDPOINT,
)
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace import TracerProvider, _Span
from opentelemetry.sdk.trace.export import SpanExportResult

TEST_SERVICE_NAME = "test_service"
Expand Down Expand Up @@ -122,13 +122,37 @@ def test_constructor_all_params_and_env_vars(self):
self.assertEqual(exporter.local_node.port, local_node_port)

@patch("requests.post")
def test_invalid_response(self, mock_post):
def test_export_success(self, mock_post):
mock_post.return_value = MockResponse(200)
spans = []
exporter = ZipkinExporter(Protocol.V2_PROTOBUF)
status = exporter.export(spans)
self.assertEqual(SpanExportResult.SUCCESS, status)

@patch("requests.post")
def test_export_invalid_response(self, mock_post):
mock_post.return_value = MockResponse(404)
spans = []
exporter = ZipkinExporter(Protocol.V2_PROTOBUF)
status = exporter.export(spans)
self.assertEqual(SpanExportResult.FAILURE, status)

@patch("requests.post")
def test_export_span_service_name(self, mock_post):
mock_post.return_value = MockResponse(200)
resource = Resource.create({SERVICE_NAME: "test"})
context = trace.SpanContext(
trace_id=0x000000000000000000000000DEADBEEF,
span_id=0x00000000DEADBEF0,
is_remote=False,
)
span = _Span("test_span", context=context, resource=resource)
span.start()
span.end()
exporter = ZipkinExporter(Protocol.V2_PROTOBUF)
exporter.export([span])
self.assertEqual(exporter.local_node.service_name, "test")


class TestZipkinNodeEndpoint(unittest.TestCase):
def test_constructor_default(self):
Expand Down