From d180fd6f9220869fd66f9bd71102592f1151e015 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Tue, 15 Sep 2020 14:45:11 +0200 Subject: [PATCH 01/10] Introduce OTEL Environment Variables support --- .../opentelemetry/exporter/jaeger/__init__.py | 63 ++++++++----------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py index 354fe8b85c1..786f788685f 100644 --- a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py +++ b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py @@ -39,10 +39,7 @@ agent_host_name='localhost', agent_port=6831, # optional: configure also collector - # collector_host_name='localhost', - # collector_port=14268, - # collector_endpoint='/api/traces?format=jaeger.thrift', - # collector_protocol='http', + # collector_endpoint='http://localhost:14268/api/traces?format=jaeger.thrift', # username=xxxx, # optional # password=xxxx, # optional ) @@ -64,6 +61,7 @@ import base64 import logging +import os import socket from thrift.protocol import TBinaryProtocol, TCompactProtocol @@ -77,14 +75,21 @@ DEFAULT_AGENT_HOST_NAME = "localhost" DEFAULT_AGENT_PORT = 6831 -DEFAULT_COLLECTOR_ENDPOINT = "/api/traces?format=jaeger.thrift" -DEFAULT_COLLECTOR_PROTOCOL = "http" UDP_PACKET_MAX_LENGTH = 65000 logger = logging.getLogger(__name__) +OTEL_ENVS = { + 'agent_host': "OTEL_EXPORTER_JAEGER_AGENT_HOST", + 'agent_port': "OTEL_EXPORTER_JAEGER_AGENT_PORT", + 'collector_endpoint': "OTEL_EXPORTER_JAEGER_ENDPOINT", + 'username': "OTEL_EXPORTER_JAEGER_USER", + 'password': "OTEL_EXPORTER_JAEGER_PASSWORD" +} + + class JaegerSpanExporter(SpanExporter): """Jaeger span exporter for OpenTelemetry. @@ -93,11 +98,7 @@ class JaegerSpanExporter(SpanExporter): when query for spans. agent_host_name: The host name of the Jaeger-Agent. agent_port: The port of the Jaeger-Agent. - collector_host_name: The host name of the Jaeger-Collector HTTP/HTTPS - Thrift. - collector_port: The port of the Jaeger-Collector HTTP/HTTPS Thrift. collector_endpoint: The endpoint of the Jaeger-Collector HTTP/HTTPS Thrift. - collector_protocol: The transfer protocol for the Jaeger-Collector(HTTP or HTTPS). username: The user name of the Basic Auth if authentication is required. password: The password of the Basic Auth if authentication is @@ -109,23 +110,18 @@ def __init__( service_name, agent_host_name=DEFAULT_AGENT_HOST_NAME, agent_port=DEFAULT_AGENT_PORT, - collector_host_name=None, - collector_port=None, - collector_endpoint=DEFAULT_COLLECTOR_ENDPOINT, - collector_protocol=DEFAULT_COLLECTOR_PROTOCOL, + collector_endpoint=None, username=None, password=None, ): self.service_name = service_name - self.agent_host_name = agent_host_name - self.agent_port = agent_port + self.agent_host_name = os.environ.get(OTEL_ENVS["agent_host"]) or agent_host_name + self.agent_port = int(os.environ.get(OTEL_ENVS["agent_port"])) \ + if os.environ.get(OTEL_ENVS["agent_port"]) else agent_port self._agent_client = None - self.collector_host_name = collector_host_name - self.collector_port = collector_port - self.collector_endpoint = collector_endpoint - self.collector_protocol = collector_protocol - self.username = username - self.password = password + self.collector_endpoint = os.environ.get(OTEL_ENVS["collector_endpoint"]) or collector_endpoint + self.username = os.environ.get(OTEL_ENVS["username"]) or username + self.password = os.environ.get(OTEL_ENVS["password"]) or password self._collector = None @property @@ -141,21 +137,16 @@ def collector(self): if self._collector is not None: return self._collector - if self.collector_host_name is None or self.collector_port is None: + if self.collector_endpoint is None: return None - thrift_url = "{}://{}:{}{}".format( - self.collector_protocol, - self.collector_host_name, - self.collector_port, - self.collector_endpoint, - ) - auth = None if self.username is not None and self.password is not None: auth = (self.username, self.password) - self._collector = Collector(thrift_url=thrift_url, auth=auth) + self._collector = Collector( + thrift_url=self.collector_endpoint, auth=auth + ) return self._collector def export(self, spans): @@ -364,11 +355,11 @@ class AgentClientUDP: """ def __init__( - self, - host_name, - port, - max_packet_size=UDP_PACKET_MAX_LENGTH, - client=agent.Client, + self, + host_name, + port, + max_packet_size=UDP_PACKET_MAX_LENGTH, + client=agent.Client, ): self.address = (host_name, port) self.max_packet_size = max_packet_size From f7847ddcccd5f9097f4b319ad0e48dbf70d41e16 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Tue, 15 Sep 2020 14:45:59 +0200 Subject: [PATCH 02/10] Update and add OTEL Envs test --- .../tests/test_jaeger_exporter.py | 73 ++++++++++++++----- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py b/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py index c7870814226..40539e3a355 100644 --- a/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py +++ b/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import unittest from unittest import mock @@ -43,20 +44,14 @@ def setUp(self): def test_constructor_default(self): """Test the default values assigned by constructor.""" service_name = "my-service-name" - host_name = "localhost" - thrift_port = None + agent_host_name = "localhost" agent_port = 6831 - collector_endpoint = "/api/traces?format=jaeger.thrift" - collector_protocol = "http" exporter = jaeger_exporter.JaegerSpanExporter(service_name) self.assertEqual(exporter.service_name, service_name) - self.assertEqual(exporter.collector_host_name, None) - self.assertEqual(exporter.agent_host_name, host_name) + self.assertEqual(exporter.agent_host_name, agent_host_name) self.assertEqual(exporter.agent_port, agent_port) - self.assertEqual(exporter.collector_port, thrift_port) - self.assertEqual(exporter.collector_protocol, collector_protocol) - self.assertEqual(exporter.collector_endpoint, collector_endpoint) + self.assertEqual(exporter.collector_endpoint, None) self.assertEqual(exporter.username, None) self.assertEqual(exporter.password, None) self.assertTrue(exporter.collector is None) @@ -65,10 +60,7 @@ def test_constructor_default(self): def test_constructor_explicit(self): """Test the constructor passing all the options.""" service = "my-opentelemetry-jaeger" - collector_host_name = "opentelemetry.io" - collector_port = 15875 - collector_endpoint = "/myapi/traces?format=jaeger.thrift" - collector_protocol = "https" + collector_endpoint = "https://opentelemetry.io:15875" agent_port = 14268 agent_host_name = "opentelemetry.io" @@ -79,21 +71,16 @@ def test_constructor_explicit(self): exporter = jaeger_exporter.JaegerSpanExporter( service_name=service, - collector_host_name=collector_host_name, - collector_port=collector_port, - collector_endpoint=collector_endpoint, - collector_protocol="https", agent_host_name=agent_host_name, agent_port=agent_port, + collector_endpoint=collector_endpoint, username=username, password=password, ) + self.assertEqual(exporter.service_name, service) self.assertEqual(exporter.agent_host_name, agent_host_name) self.assertEqual(exporter.agent_port, agent_port) - self.assertEqual(exporter.collector_host_name, collector_host_name) - self.assertEqual(exporter.collector_port, collector_port) - self.assertEqual(exporter.collector_protocol, collector_protocol) self.assertTrue(exporter.collector is not None) self.assertEqual(exporter.collector.auth, auth) # property should not construct new object @@ -107,6 +94,52 @@ def test_constructor_explicit(self): self.assertNotEqual(exporter.collector, collector) self.assertTrue(exporter.collector.auth is None) + def test_constructor_by_environment_variables(self): + """Test the constructor using Environment Variables.""" + service = "my-opentelemetry-jaeger" + + agent_host_name = "opentelemetry.io" + agent_port = 6833 + + collector_endpoint = "https://opentelemetry.io:15875" + + username = "username" + password = "password" + auth = (username, password) + + otel_envs = jaeger_exporter.OTEL_ENVS + + os.environ[otel_envs["agent_host"]] = agent_host_name + os.environ[otel_envs["agent_port"]] = str(agent_port) + os.environ[otel_envs["collector_endpoint"]] = collector_endpoint + os.environ[otel_envs["username"]] = username + os.environ[otel_envs["password"]] = password + + exporter = jaeger_exporter.JaegerSpanExporter(service_name=service) + + self.assertEqual(exporter.service_name, service) + self.assertEqual(exporter.agent_host_name, agent_host_name) + self.assertEqual(exporter.agent_port, agent_port) + self.assertTrue(exporter.collector is not None) + self.assertEqual(exporter.collector_endpoint, collector_endpoint) + self.assertEqual(exporter.collector.auth, auth) + # property should not construct new object + collector = exporter.collector + self.assertEqual(exporter.collector, collector) + # property should construct new object + # pylint: disable=protected-access + exporter._collector = None + exporter.username = None + exporter.password = None + self.assertNotEqual(exporter.collector, collector) + self.assertTrue(exporter.collector.auth is None) + + del os.environ[otel_envs["agent_host"]] + del os.environ[otel_envs["agent_port"]] + del os.environ[otel_envs["collector_endpoint"]] + del os.environ[otel_envs["username"]] + del os.environ[otel_envs["password"]] + def test_nsec_to_usec_round(self): # pylint: disable=protected-access nsec_to_usec_round = jaeger_exporter._nsec_to_usec_round From 0913ee04ad1b4fbcd8a69ad1f96eeca127bef176 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Tue, 15 Sep 2020 16:26:35 +0200 Subject: [PATCH 03/10] Apply lint comments --- .../opentelemetry/exporter/jaeger/__init__.py | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py index 786f788685f..9790a01dacc 100644 --- a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py +++ b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py @@ -80,13 +80,12 @@ logger = logging.getLogger(__name__) - OTEL_ENVS = { - 'agent_host': "OTEL_EXPORTER_JAEGER_AGENT_HOST", - 'agent_port': "OTEL_EXPORTER_JAEGER_AGENT_PORT", - 'collector_endpoint': "OTEL_EXPORTER_JAEGER_ENDPOINT", - 'username': "OTEL_EXPORTER_JAEGER_USER", - 'password': "OTEL_EXPORTER_JAEGER_PASSWORD" + "agent_host": "OTEL_EXPORTER_JAEGER_AGENT_HOST", + "agent_port": "OTEL_EXPORTER_JAEGER_AGENT_PORT", + "collector_endpoint": "OTEL_EXPORTER_JAEGER_ENDPOINT", + "username": "OTEL_EXPORTER_JAEGER_USER", + "password": "OTEL_EXPORTER_JAEGER_PASSWORD", } @@ -115,11 +114,19 @@ def __init__( password=None, ): self.service_name = service_name - self.agent_host_name = os.environ.get(OTEL_ENVS["agent_host"]) or agent_host_name - self.agent_port = int(os.environ.get(OTEL_ENVS["agent_port"])) \ - if os.environ.get(OTEL_ENVS["agent_port"]) else agent_port + self.agent_host_name = ( + os.environ.get(OTEL_ENVS["agent_host"]) or agent_host_name + ) + self.agent_port = ( + int(os.environ.get(OTEL_ENVS["agent_port"])) + if os.environ.get(OTEL_ENVS["agent_port"]) + else agent_port + ) self._agent_client = None - self.collector_endpoint = os.environ.get(OTEL_ENVS["collector_endpoint"]) or collector_endpoint + self.collector_endpoint = ( + os.environ.get(OTEL_ENVS["collector_endpoint"]) + or collector_endpoint + ) self.username = os.environ.get(OTEL_ENVS["username"]) or username self.password = os.environ.get(OTEL_ENVS["password"]) or password self._collector = None @@ -355,11 +362,11 @@ class AgentClientUDP: """ def __init__( - self, - host_name, - port, - max_packet_size=UDP_PACKET_MAX_LENGTH, - client=agent.Client, + self, + host_name, + port, + max_packet_size=UDP_PACKET_MAX_LENGTH, + client=agent.Client, ): self.address = (host_name, port) self.max_packet_size = max_packet_size From 79cee1609012c10405827a682d2fd45c633fb413 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Fri, 18 Sep 2020 19:41:12 +0200 Subject: [PATCH 04/10] Reverse setup of username and password --- .../src/opentelemetry/exporter/jaeger/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py index 9790a01dacc..65e1e59816c 100644 --- a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py +++ b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py @@ -127,8 +127,8 @@ def __init__( os.environ.get(OTEL_ENVS["collector_endpoint"]) or collector_endpoint ) - self.username = os.environ.get(OTEL_ENVS["username"]) or username - self.password = os.environ.get(OTEL_ENVS["password"]) or password + self.username = username or os.environ.get(OTEL_ENVS["username"]) + self.password = password or os.environ.get(OTEL_ENVS["password"]) self._collector = None @property From 2ff3c9fc6af93ea04101d3cb9b384b5e9044f895 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Fri, 18 Sep 2020 19:51:12 +0200 Subject: [PATCH 05/10] Update CHANGELOG.md --- exporter/opentelemetry-exporter-jaeger/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exporter/opentelemetry-exporter-jaeger/CHANGELOG.md b/exporter/opentelemetry-exporter-jaeger/CHANGELOG.md index 1545e187fcf..b74ba6ea892 100644 --- a/exporter/opentelemetry-exporter-jaeger/CHANGELOG.md +++ b/exporter/opentelemetry-exporter-jaeger/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog ## Unreleased +- Add support for Jaeger Span Exporter configuration by environment variables and
+ change JaegerSpanExporter constructor parameters + ([#1114](https://github.com/open-telemetry/opentelemetry-python/pull/1114)) ## Version 0.13b0 From 3d8f3334db0778eb187e50a6417fbf0aea31eed4 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Fri, 18 Sep 2020 20:04:33 +0200 Subject: [PATCH 06/10] Add configuration information --- exporter/opentelemetry-exporter-jaeger/README.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/exporter/opentelemetry-exporter-jaeger/README.rst b/exporter/opentelemetry-exporter-jaeger/README.rst index 0069d130cdf..8a9b6b08197 100644 --- a/exporter/opentelemetry-exporter-jaeger/README.rst +++ b/exporter/opentelemetry-exporter-jaeger/README.rst @@ -19,6 +19,14 @@ Installation .. _Jaeger: https://www.jaegertracing.io/ .. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/ +Configuration +------------- + +OpenTelemetry Jaeger Exporter can be configured by setting `JaegerSpanExporter parameters +`_ or by setting +`environment variables `_ References ---------- From d466113c5596284063453d5cf560f84d08032bb0 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Tue, 22 Sep 2020 00:37:43 +0200 Subject: [PATCH 07/10] Introduce Configuration --- .../opentelemetry/exporter/jaeger/__init__.py | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py index 65e1e59816c..ba95da6771d 100644 --- a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py +++ b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py @@ -61,13 +61,12 @@ import base64 import logging -import os import socket from thrift.protocol import TBinaryProtocol, TCompactProtocol from thrift.transport import THttpClient, TTransport -import opentelemetry.trace as trace_api +from opentelemetry.configuration import Configuration from opentelemetry.exporter.jaeger.gen.agent import Agent as agent from opentelemetry.exporter.jaeger.gen.jaeger import Collector as jaeger from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult @@ -80,14 +79,6 @@ logger = logging.getLogger(__name__) -OTEL_ENVS = { - "agent_host": "OTEL_EXPORTER_JAEGER_AGENT_HOST", - "agent_port": "OTEL_EXPORTER_JAEGER_AGENT_PORT", - "collector_endpoint": "OTEL_EXPORTER_JAEGER_ENDPOINT", - "username": "OTEL_EXPORTER_JAEGER_USER", - "password": "OTEL_EXPORTER_JAEGER_PASSWORD", -} - class JaegerSpanExporter(SpanExporter): """Jaeger span exporter for OpenTelemetry. @@ -115,20 +106,17 @@ def __init__( ): self.service_name = service_name self.agent_host_name = ( - os.environ.get(OTEL_ENVS["agent_host"]) or agent_host_name + Configuration().EXPORTER_JAEGER_AGENT_HOST or agent_host_name ) self.agent_port = ( - int(os.environ.get(OTEL_ENVS["agent_port"])) - if os.environ.get(OTEL_ENVS["agent_port"]) - else agent_port + Configuration().EXPORTER_JAEGER_AGENT_PORT or agent_port ) self._agent_client = None self.collector_endpoint = ( - os.environ.get(OTEL_ENVS["collector_endpoint"]) - or collector_endpoint + Configuration().EXPORTER_JAEGER_ENDPOINT or collector_endpoint ) - self.username = username or os.environ.get(OTEL_ENVS["username"]) - self.password = password or os.environ.get(OTEL_ENVS["password"]) + self.username = username or Configuration().EXPORTER_JAEGER_USER + self.password = password or Configuration().EXPORTER_JAEGER_PASSWORD self._collector = None @property From 50a9312f8ae27eb31368aede6177d128125ffe69 Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Tue, 22 Sep 2020 00:38:15 +0200 Subject: [PATCH 08/10] Update tests --- .../tests/test_jaeger_exporter.py | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py b/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py index 40539e3a355..dff04e033b2 100644 --- a/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py +++ b/exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest from unittest import mock @@ -21,6 +20,7 @@ # pylint:disable=import-error import opentelemetry.exporter.jaeger as jaeger_exporter from opentelemetry import trace as trace_api +from opentelemetry.configuration import Configuration from opentelemetry.exporter.jaeger.gen.jaeger import ttypes as jaeger from opentelemetry.sdk import trace from opentelemetry.sdk.trace import Resource @@ -99,7 +99,7 @@ def test_constructor_by_environment_variables(self): service = "my-opentelemetry-jaeger" agent_host_name = "opentelemetry.io" - agent_port = 6833 + agent_port = "6831" collector_endpoint = "https://opentelemetry.io:15875" @@ -107,19 +107,24 @@ def test_constructor_by_environment_variables(self): password = "password" auth = (username, password) - otel_envs = jaeger_exporter.OTEL_ENVS + environ_patcher = mock.patch.dict( + "os.environ", + { + "OTEL_EXPORTER_JAEGER_AGENT_HOST": agent_host_name, + "OTEL_EXPORTER_JAEGER_AGENT_PORT": agent_port, + "OTEL_EXPORTER_JAEGER_ENDPOINT": collector_endpoint, + "OTEL_EXPORTER_JAEGER_USER": username, + "OTEL_EXPORTER_JAEGER_PASSWORD": password, + }, + ) - os.environ[otel_envs["agent_host"]] = agent_host_name - os.environ[otel_envs["agent_port"]] = str(agent_port) - os.environ[otel_envs["collector_endpoint"]] = collector_endpoint - os.environ[otel_envs["username"]] = username - os.environ[otel_envs["password"]] = password + environ_patcher.start() exporter = jaeger_exporter.JaegerSpanExporter(service_name=service) self.assertEqual(exporter.service_name, service) self.assertEqual(exporter.agent_host_name, agent_host_name) - self.assertEqual(exporter.agent_port, agent_port) + self.assertEqual(exporter.agent_port, int(agent_port)) self.assertTrue(exporter.collector is not None) self.assertEqual(exporter.collector_endpoint, collector_endpoint) self.assertEqual(exporter.collector.auth, auth) @@ -134,11 +139,9 @@ def test_constructor_by_environment_variables(self): self.assertNotEqual(exporter.collector, collector) self.assertTrue(exporter.collector.auth is None) - del os.environ[otel_envs["agent_host"]] - del os.environ[otel_envs["agent_port"]] - del os.environ[otel_envs["collector_endpoint"]] - del os.environ[otel_envs["username"]] - del os.environ[otel_envs["password"]] + environ_patcher.stop() + + Configuration._reset() def test_nsec_to_usec_round(self): # pylint: disable=protected-access From 952549c26a8b40d03eaf4e48dfd09379acfdc50b Mon Sep 17 00:00:00 2001 From: Mateusz 'mat' Rumian Date: Fri, 2 Oct 2020 23:53:32 +0200 Subject: [PATCH 09/10] Swap username and password with env vars --- .../src/opentelemetry/exporter/jaeger/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py index ba95da6771d..40d2835128a 100644 --- a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py +++ b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py @@ -115,8 +115,8 @@ def __init__( self.collector_endpoint = ( Configuration().EXPORTER_JAEGER_ENDPOINT or collector_endpoint ) - self.username = username or Configuration().EXPORTER_JAEGER_USER - self.password = password or Configuration().EXPORTER_JAEGER_PASSWORD + self.username = Configuration().EXPORTER_JAEGER_USER or username + self.password = Configuration().EXPORTER_JAEGER_PASSWORD or password self._collector = None @property From b3c0da6cf31de93e7afb1929d9085b9290efb39e Mon Sep 17 00:00:00 2001 From: rydzykje Date: Sun, 4 Oct 2020 17:04:17 +0200 Subject: [PATCH 10/10] Introduce parameter setter, constructor parameters over env vars --- .../opentelemetry/exporter/jaeger/__init__.py | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py index 40d2835128a..b64376fc212 100644 --- a/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py +++ b/exporter/opentelemetry-exporter-jaeger/src/opentelemetry/exporter/jaeger/__init__.py @@ -98,25 +98,39 @@ class JaegerSpanExporter(SpanExporter): def __init__( self, service_name, - agent_host_name=DEFAULT_AGENT_HOST_NAME, - agent_port=DEFAULT_AGENT_PORT, + agent_host_name=None, + agent_port=None, collector_endpoint=None, username=None, password=None, ): self.service_name = service_name - self.agent_host_name = ( - Configuration().EXPORTER_JAEGER_AGENT_HOST or agent_host_name + self.agent_host_name = _parameter_setter( + param=agent_host_name, + env_variable=Configuration().EXPORTER_JAEGER_AGENT_HOST, + default=DEFAULT_AGENT_HOST_NAME, ) - self.agent_port = ( - Configuration().EXPORTER_JAEGER_AGENT_PORT or agent_port + self.agent_port = _parameter_setter( + param=agent_port, + env_variable=Configuration().EXPORTER_JAEGER_AGENT_PORT, + default=DEFAULT_AGENT_PORT, ) self._agent_client = None - self.collector_endpoint = ( - Configuration().EXPORTER_JAEGER_ENDPOINT or collector_endpoint + self.collector_endpoint = _parameter_setter( + param=collector_endpoint, + env_variable=Configuration().EXPORTER_JAEGER_ENDPOINT, + default=None, + ) + self.username = _parameter_setter( + param=username, + env_variable=Configuration().EXPORTER_JAEGER_USER, + default=None, + ) + self.password = _parameter_setter( + param=password, + env_variable=Configuration().EXPORTER_JAEGER_PASSWORD, + default=None, ) - self.username = Configuration().EXPORTER_JAEGER_USER or username - self.password = Configuration().EXPORTER_JAEGER_PASSWORD or password self._collector = None @property @@ -163,6 +177,22 @@ def shutdown(self): pass +def _parameter_setter(param, env_variable, default): + """Returns value according to the provided data. + + Args: + param: Constructor parameter value + env_variable: Environment variable related to the parameter + default: Constructor parameter default value + """ + if param is None: + res = env_variable or default + else: + res = param + + return res + + def _nsec_to_usec_round(nsec): """Round nanoseconds to microseconds""" return (nsec + 500) // 10 ** 3