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

Support Environment Variables for JaegerSpanExporter configuration #1114

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
3 changes: 3 additions & 0 deletions exporter/opentelemetry-exporter-jaeger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## Unreleased
- Add support for Jaeger Span Exporter configuration by environment variables and<br/>
change JaegerSpanExporter constructor parameters
([#1114](https://github.com/open-telemetry/opentelemetry-python/pull/1114))

## Version 0.13b0

Expand Down
8 changes: 8 additions & 0 deletions exporter/opentelemetry-exporter-jaeger/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://github.com/open-telemetry/opentelemetry-python/blob/master/exporter/opentelemetry-exporter-jaeger
/src/opentelemetry/exporter/jaeger/__init__.py#L88>`_ or by setting
`environment variables <https://github.com/open-telemetry/opentelemetry-specification/blob/master/
specification/sdk-environment-variables.md#jaeger-exporter>`_

References
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -64,6 +61,7 @@

import base64
import logging
import os
import socket

from thrift.protocol import TBinaryProtocol, TCompactProtocol
Expand All @@ -77,13 +75,19 @@

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.
Expand All @@ -93,11 +97,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
Expand All @@ -109,23 +109,26 @@ 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the default supposed to be http://localhost:14250?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case it shouldn't. Previously collector_endpoint was related to /api/traces?format=jaeger.thrift and it was just a part of complete endpoint address. If we set collector_endpoint default then it will be used over agent which was default implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why the specs would have that is the default value then?

If we set collector_endpoint default then it will be used over agent which was default implementation

I'm also not sure what you mean by the above statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to say that in previous implementation collector by default was not used and collector_endpoint was just an end part of complete URL. In current implementation there's no need to specify host,port and endpoint separately because they will be provided by one environment variable called OTEL_EXPORTER_JAEGER_ENDPOINT coming from otel specification.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I am asking whether the collector_endpoint value should default to http://localhost:14250 as defined by the spec if the value is not explicitly passed into the exporter AND the environment variable is not configured.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to our discussion on the Gitter. We've decided to do not set the collector_endpoint default because it is causing not entirely clear path of the used protocol to export spans. Separate issue and PR should be created for this purpose.

Copy link
Contributor

@lzchen lzchen Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, looking at java's implementation, it looks like they only have the concept of the collector endpoint. This leads me to believe that we should still have the default value for collector_endpoint, and if there's anything that errors with the creation of the Collector, then we would use the AgentClientUDP instead. So we should still set the default value to http://localhost:14250.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codeboten @rydzykje
Thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, I don't know if there's any scenario where the .constructor property would return None if the default of collector_endpoint is always set. Based on the jaeger docs, it would appear that only the agent configuration is usually set by default, and a collector endpoint would have to be explicitly set:

If we want Jaeger client libraries to send trace data directly to collectors, we must provide them with a URL of the HTTP endpoint. It means that our applications require additional configuration containing this parameter, especially if we are running multiple Jaeger installations

I wonder if the spec should default to not having a collector endpoint. You're right @lzchen that other implementations only have the concept of an endpoint, go does the same thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be ok to go ahead with this implementation and raise an issue with the spec about this.

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
rydzykje marked this conversation as resolved.
Show resolved Hide resolved
)
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 = username or os.environ.get(OTEL_ENVS["username"])
self.password = password or os.environ.get(OTEL_ENVS["password"])
self._collector = None

@property
Expand All @@ -141,21 +144,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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down