diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e0c8f6f2f5..d5d58c8293b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py index b320119aeeb..156afc247d9 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py @@ -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 @@ -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, @@ -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}" diff --git a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py index 7f91e05984f..e3cd2046267 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py @@ -20,6 +20,7 @@ DEFAULT_COMPRESSION, DEFAULT_ENDPOINT, DEFAULT_TIMEOUT, + DEFAULT_TRACES_EXPORT_PATH, OTLPSpanExporter, ) from opentelemetry.sdk.environment_variables import ( @@ -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) @@ -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, }, @@ -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, }, @@ -126,7 +129,6 @@ 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) @@ -134,6 +136,32 @@ def test_exporter_env(self): 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", {