Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 370cc6b

Browse files
author
alrex
authored
exporter/zipkin: adding support for env var OTEL_EXPORTER_ZIPKIN_ENDPOINT (open-telemetry#1064)
* add support for env var: OTEL_EXPORTER_ZIPKIN_ENDPOINT * update method signature to use url
1 parent f6e0705 commit 370cc6b

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

exporter/opentelemetry-exporter-zipkin/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

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

713
Released 2020-08-14

exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2525
.. _Zipkin: https://zipkin.io/
2626
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
27+
.. _Specification: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/sdk-environment-variables.md#zipkin-exporter
2728
2829
.. code:: python
2930
@@ -39,10 +40,7 @@
3940
zipkin_exporter = zipkin.ZipkinSpanExporter(
4041
service_name="my-helloworld-service",
4142
# optional:
42-
# host_name="localhost",
43-
# port=9411,
44-
# endpoint="/api/v2/spans",
45-
# protocol="http",
43+
# url="http://localhost:9411/api/v2/spans",
4644
# ipv4="",
4745
# ipv6="",
4846
# retry=False,
@@ -57,24 +55,25 @@
5755
with tracer.start_as_current_span("foo"):
5856
print("Hello world!")
5957
58+
The exporter supports endpoint configuration via the OTEL_EXPORTER_ZIPKIN_ENDPOINT environment variables as defined in the `Specification`_
59+
6060
API
6161
---
6262
"""
6363

6464
import json
6565
import logging
66+
import os
6667
from typing import Optional, Sequence
68+
from urllib.parse import urlparse
6769

6870
import requests
6971

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

73-
DEFAULT_ENDPOINT = "/api/v2/spans"
74-
DEFAULT_HOST_NAME = "localhost"
75-
DEFAULT_PORT = 9411
76-
DEFAULT_PROTOCOL = "http"
7775
DEFAULT_RETRY = False
76+
DEFAULT_URL = "http://localhost:9411/api/v2/spans"
7877
ZIPKIN_HEADERS = {"Content-Type": "application/json"}
7978

8079
SPAN_KIND_MAP = {
@@ -96,10 +95,7 @@ class ZipkinSpanExporter(SpanExporter):
9695
Args:
9796
service_name: Service that logged an annotation in a trace.Classifier
9897
when query for spans.
99-
host_name: The host name of the Zipkin server
100-
port: The port of the Zipkin server
101-
endpoint: The endpoint of the Zipkin server
102-
protocol: The protocol used for the request.
98+
url: The Zipkin endpoint URL
10399
ipv4: Primary IPv4 address associated with this connection.
104100
ipv6: Primary IPv6 address associated with this connection.
105101
retry: Set to True to configure the exporter to retry on failure.
@@ -108,22 +104,21 @@ class ZipkinSpanExporter(SpanExporter):
108104
def __init__(
109105
self,
110106
service_name: str,
111-
host_name: str = DEFAULT_HOST_NAME,
112-
port: int = DEFAULT_PORT,
113-
endpoint: str = DEFAULT_ENDPOINT,
114-
protocol: str = DEFAULT_PROTOCOL,
107+
url: str = None,
115108
ipv4: Optional[str] = None,
116109
ipv6: Optional[str] = None,
117110
retry: Optional[str] = DEFAULT_RETRY,
118111
):
119112
self.service_name = service_name
120-
self.host_name = host_name
121-
self.port = port
122-
self.endpoint = endpoint
123-
self.protocol = protocol
124-
self.url = "{}://{}:{}{}".format(
125-
self.protocol, self.host_name, self.port, self.endpoint
126-
)
113+
if url is None:
114+
self.url = os.environ.get(
115+
"OTEL_EXPORTER_ZIPKIN_ENDPOINT", DEFAULT_URL
116+
)
117+
else:
118+
self.url = url
119+
120+
self.port = urlparse(self.url).port
121+
127122
self.ipv4 = ipv4
128123
self.ipv6 = ipv6
129124
self.retry = retry

exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import json
16+
import os
1617
import unittest
1718
from unittest.mock import MagicMock, patch
1819

@@ -43,54 +44,56 @@ def setUp(self):
4344
self._test_span.start()
4445
self._test_span.end()
4546

47+
def tearDown(self):
48+
if "OTEL_EXPORTER_ZIPKIN_ENDPOINT" in os.environ:
49+
del os.environ["OTEL_EXPORTER_ZIPKIN_ENDPOINT"]
50+
51+
def test_constructor_env_var(self):
52+
"""Test the default values assigned by constructor."""
53+
url = "https://foo:9911/path"
54+
os.environ["OTEL_EXPORTER_ZIPKIN_ENDPOINT"] = url
55+
service_name = "my-service-name"
56+
port = 9911
57+
exporter = ZipkinSpanExporter(service_name)
58+
ipv4 = None
59+
ipv6 = None
60+
61+
self.assertEqual(exporter.service_name, service_name)
62+
self.assertEqual(exporter.ipv4, ipv4)
63+
self.assertEqual(exporter.ipv6, ipv6)
64+
self.assertEqual(exporter.url, url)
65+
self.assertEqual(exporter.port, port)
66+
4667
def test_constructor_default(self):
4768
"""Test the default values assigned by constructor."""
4869
service_name = "my-service-name"
49-
host_name = "localhost"
5070
port = 9411
51-
endpoint = "/api/v2/spans"
5271
exporter = ZipkinSpanExporter(service_name)
5372
ipv4 = None
5473
ipv6 = None
55-
protocol = "http"
5674
url = "http://localhost:9411/api/v2/spans"
5775

5876
self.assertEqual(exporter.service_name, service_name)
59-
self.assertEqual(exporter.host_name, host_name)
6077
self.assertEqual(exporter.port, port)
61-
self.assertEqual(exporter.endpoint, endpoint)
6278
self.assertEqual(exporter.ipv4, ipv4)
6379
self.assertEqual(exporter.ipv6, ipv6)
64-
self.assertEqual(exporter.protocol, protocol)
6580
self.assertEqual(exporter.url, url)
6681

6782
def test_constructor_explicit(self):
6883
"""Test the constructor passing all the options."""
6984
service_name = "my-opentelemetry-zipkin"
70-
host_name = "opentelemetry.io"
7185
port = 15875
72-
endpoint = "/myapi/traces?format=zipkin"
7386
ipv4 = "1.2.3.4"
7487
ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
75-
protocol = "https"
7688
url = "https://opentelemetry.io:15875/myapi/traces?format=zipkin"
7789
exporter = ZipkinSpanExporter(
78-
service_name=service_name,
79-
host_name=host_name,
80-
port=port,
81-
endpoint=endpoint,
82-
ipv4=ipv4,
83-
ipv6=ipv6,
84-
protocol=protocol,
90+
service_name=service_name, url=url, ipv4=ipv4, ipv6=ipv6,
8591
)
8692

8793
self.assertEqual(exporter.service_name, service_name)
88-
self.assertEqual(exporter.host_name, host_name)
8994
self.assertEqual(exporter.port, port)
90-
self.assertEqual(exporter.endpoint, endpoint)
9195
self.assertEqual(exporter.ipv4, ipv4)
9296
self.assertEqual(exporter.ipv6, ipv6)
93-
self.assertEqual(exporter.protocol, protocol)
9497
self.assertEqual(exporter.url, url)
9598

9699
# pylint: disable=too-many-locals

0 commit comments

Comments
 (0)