Skip to content

Fix OTLP HTTP Endpoint Usage #2493

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 13 commits into from
Mar 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@


DEFAULT_COMPRESSION = Compression.NoCompression
DEFAULT_ENDPOINT = "http://localhost:4318/v1/traces"
DEFAULT_ENDPOINT = "http://localhost:4318/"
DEFAULT_TRACES_EXPORT_PATH = "v1/traces"
DEFAULT_TIMEOUT = 10 # in seconds


Expand All @@ -65,7 +66,9 @@ def __init__(
):
self._endpoint = endpoint or environ.get(
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
environ.get(OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_ENDPOINT),
_append_trace_path(
environ.get(OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_ENDPOINT)
),
)
self._certificate_file = certificate_file or environ.get(
OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE,
Expand Down Expand Up @@ -172,3 +175,9 @@ def _compression_from_env() -> Compression:
.strip()
)
return Compression(compression)


def _append_trace_path(endpoint: str) -> str:
if endpoint.endswith("/"):
return endpoint + DEFAULT_TRACES_EXPORT_PATH
return endpoint + f"/{DEFAULT_TRACES_EXPORT_PATH}"
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
DEFAULT_COMPRESSION,
DEFAULT_ENDPOINT,
DEFAULT_TRACES_EXPORT_PATH,
DEFAULT_TIMEOUT,
OTLPSpanExporter,
)
Expand Down Expand Up @@ -47,7 +48,9 @@ def test_constructor_default(self):

exporter = OTLPSpanExporter()

self.assertEqual(exporter._endpoint, DEFAULT_ENDPOINT)
self.assertEqual(
exporter._endpoint, DEFAULT_ENDPOINT + DEFAULT_TRACES_EXPORT_PATH
)
self.assertEqual(exporter._certificate_file, True)
self.assertEqual(exporter._timeout, DEFAULT_TIMEOUT)
self.assertIs(exporter._compression, DEFAULT_COMPRESSION)
Expand Down Expand Up @@ -90,6 +93,7 @@ def test_exporter_traces_env_take_priority(self):
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://traces.endpoint.env",
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
},
Expand Down Expand Up @@ -117,7 +121,6 @@ def test_exporter_constructor_take_priority(self):
{
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
},
Expand All @@ -126,14 +129,39 @@ def test_exporter_env(self):

exporter = OTLPSpanExporter()

self.assertEqual(exporter._endpoint, OS_ENV_ENDPOINT)
self.assertEqual(exporter._certificate_file, OS_ENV_CERTIFICATE)
self.assertEqual(exporter._timeout, int(OS_ENV_TIMEOUT))
self.assertIs(exporter._compression, Compression.Gzip)
self.assertEqual(
exporter._headers, {"envheader1": "val1", "envheader2": "val2"}
)

@patch.dict(
"os.environ",
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT},
)
def test_exporter_env_endpoint_without_slash(self):

exporter = OTLPSpanExporter()

self.assertEqual(
exporter._endpoint,
OS_ENV_ENDPOINT + f"/{DEFAULT_TRACES_EXPORT_PATH}",
)

@patch.dict(
"os.environ",
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT + "/"},
)
def test_exporter_env_endpoint_with_slash(self):

exporter = OTLPSpanExporter()

self.assertEqual(
exporter._endpoint,
OS_ENV_ENDPOINT + f"/{DEFAULT_TRACES_EXPORT_PATH}",
)

@patch.dict(
"os.environ",
{
Expand Down