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

exporter/zipkin: adding support for env var OTEL_EXPORTER_ZIPKIN_ENDPOINT #1064

Merged
merged 8 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions exporter/opentelemetry-exporter-zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

- Add support for OTEL_EXPORTER_ZIPKIN_ENDPOINT env var. As part of this change, the
configuration of the ZipkinSpanExporter exposes a `url` argument to replace `host_name`,
`port`, `protocol`, `endpoint`. This brings this implementation inline with other
implementations.
([#1064](https://github.com/open-telemetry/opentelemetry-python/pull/1064))

## Version 0.12b0

Released 2020-08-14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

.. _Zipkin: https://zipkin.io/
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
.. _Specification: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/sdk-environment-variables.md#zipkin-exporter

.. code:: python

Expand All @@ -39,10 +40,7 @@
zipkin_exporter = zipkin.ZipkinSpanExporter(
service_name="my-helloworld-service",
# optional:
# host_name="localhost",
# port=9411,
# endpoint="/api/v2/spans",
# protocol="http",
# url="http://localhost:9411/api/v2/spans",
# ipv4="",
# ipv6="",
# retry=False,
Expand All @@ -57,24 +55,25 @@
with tracer.start_as_current_span("foo"):
print("Hello world!")

The exporter supports endpoint configuration via the OTEL_EXPORTER_ZIPKIN_ENDPOINT environment variables as defined in the `Specification`_

API
---
"""

import json
import logging
import os
from typing import Optional, Sequence
from urllib.parse import urlparse

import requests

from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
from opentelemetry.trace import Span, SpanContext, SpanKind

DEFAULT_ENDPOINT = "/api/v2/spans"
DEFAULT_HOST_NAME = "localhost"
DEFAULT_PORT = 9411
DEFAULT_PROTOCOL = "http"
DEFAULT_RETRY = False
DEFAULT_URL = "http://localhost:9411/api/v2/spans"
ZIPKIN_HEADERS = {"Content-Type": "application/json"}

SPAN_KIND_MAP = {
Expand All @@ -96,10 +95,7 @@ class ZipkinSpanExporter(SpanExporter):
Args:
service_name: Service that logged an annotation in a trace.Classifier
when query for spans.
host_name: The host name of the Zipkin server
port: The port of the Zipkin server
endpoint: The endpoint of the Zipkin server
protocol: The protocol used for the request.
url: The Zipkin endpoint URL
ipv4: Primary IPv4 address associated with this connection.
ipv6: Primary IPv6 address associated with this connection.
retry: Set to True to configure the exporter to retry on failure.
Expand All @@ -108,22 +104,21 @@ class ZipkinSpanExporter(SpanExporter):
def __init__(
self,
service_name: str,
host_name: str = DEFAULT_HOST_NAME,
port: int = DEFAULT_PORT,
endpoint: str = DEFAULT_ENDPOINT,
protocol: str = DEFAULT_PROTOCOL,
url: str = None,
ipv4: Optional[str] = None,
ipv6: Optional[str] = None,
retry: Optional[str] = DEFAULT_RETRY,
):
self.service_name = service_name
self.host_name = host_name
self.port = port
self.endpoint = endpoint
self.protocol = protocol
self.url = "{}://{}:{}{}".format(
self.protocol, self.host_name, self.port, self.endpoint
)
if url is None:
self.url = os.environ.get(
"OTEL_EXPORTER_ZIPKIN_ENDPOINT", DEFAULT_URL
)
else:
self.url = url

self.port = urlparse(self.url).port

self.ipv4 = ipv4
self.ipv6 = ipv6
self.retry = retry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import json
import os
import unittest
from unittest.mock import MagicMock, patch

Expand Down Expand Up @@ -43,54 +44,56 @@ def setUp(self):
self._test_span.start()
self._test_span.end()

def tearDown(self):
if "OTEL_EXPORTER_ZIPKIN_ENDPOINT" in os.environ:
del os.environ["OTEL_EXPORTER_ZIPKIN_ENDPOINT"]

def test_constructor_env_var(self):
"""Test the default values assigned by constructor."""
url = "https://foo:9911/path"
os.environ["OTEL_EXPORTER_ZIPKIN_ENDPOINT"] = url
service_name = "my-service-name"
port = 9911
exporter = ZipkinSpanExporter(service_name)
ipv4 = None
ipv6 = None

self.assertEqual(exporter.service_name, service_name)
self.assertEqual(exporter.ipv4, ipv4)
self.assertEqual(exporter.ipv6, ipv6)
self.assertEqual(exporter.url, url)
self.assertEqual(exporter.port, port)

def test_constructor_default(self):
"""Test the default values assigned by constructor."""
service_name = "my-service-name"
host_name = "localhost"
port = 9411
endpoint = "/api/v2/spans"
exporter = ZipkinSpanExporter(service_name)
ipv4 = None
ipv6 = None
protocol = "http"
url = "http://localhost:9411/api/v2/spans"

self.assertEqual(exporter.service_name, service_name)
self.assertEqual(exporter.host_name, host_name)
self.assertEqual(exporter.port, port)
self.assertEqual(exporter.endpoint, endpoint)
self.assertEqual(exporter.ipv4, ipv4)
self.assertEqual(exporter.ipv6, ipv6)
self.assertEqual(exporter.protocol, protocol)
self.assertEqual(exporter.url, url)

def test_constructor_explicit(self):
"""Test the constructor passing all the options."""
service_name = "my-opentelemetry-zipkin"
host_name = "opentelemetry.io"
port = 15875
endpoint = "/myapi/traces?format=zipkin"
ipv4 = "1.2.3.4"
ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
protocol = "https"
url = "https://opentelemetry.io:15875/myapi/traces?format=zipkin"
exporter = ZipkinSpanExporter(
service_name=service_name,
host_name=host_name,
port=port,
endpoint=endpoint,
ipv4=ipv4,
ipv6=ipv6,
protocol=protocol,
service_name=service_name, url=url, ipv4=ipv4, ipv6=ipv6,
)

self.assertEqual(exporter.service_name, service_name)
self.assertEqual(exporter.host_name, host_name)
self.assertEqual(exporter.port, port)
self.assertEqual(exporter.endpoint, endpoint)
self.assertEqual(exporter.ipv4, ipv4)
self.assertEqual(exporter.ipv6, ipv6)
self.assertEqual(exporter.protocol, protocol)
self.assertEqual(exporter.url, url)

# pylint: disable=too-many-locals
Expand Down