Skip to content

Commit

Permalink
Fix OTLP HTTP Endpoint Usage (#2493)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth authored Mar 2, 2022
1 parent 6bb3ece commit 50093f2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2461](https://github.com/open-telemetry/opentelemetry-python/pull/2461))
- fix exception handling in get_aggregated_resources
([#2464](https://github.com/open-telemetry/opentelemetry-python/pull/2464))
- Fix `OTEL_EXPORTER_OTLP_ENDPOINT` usage in OTLP HTTP trace exporter
([#2493](https://github.com/open-telemetry/opentelemetry-python/pull/2493))

## [1.9.1-0.28b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.9.1-0.28b1) - 2022-01-29

Expand Down
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 @@ -20,6 +20,7 @@
DEFAULT_COMPRESSION,
DEFAULT_ENDPOINT,
DEFAULT_TIMEOUT,
DEFAULT_TRACES_EXPORT_PATH,
OTLPSpanExporter,
)
from opentelemetry.sdk.environment_variables import (
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

0 comments on commit 50093f2

Please sign in to comment.