Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50093f2

Browse files
authored
Fix OTLP HTTP Endpoint Usage (open-telemetry#2493)
1 parent 6bb3ece commit 50093f2

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
([#2461](https://github.com/open-telemetry/opentelemetry-python/pull/2461))
2121
- fix exception handling in get_aggregated_resources
2222
([#2464](https://github.com/open-telemetry/opentelemetry-python/pull/2464))
23+
- Fix `OTEL_EXPORTER_OTLP_ENDPOINT` usage in OTLP HTTP trace exporter
24+
([#2493](https://github.com/open-telemetry/opentelemetry-python/pull/2493))
2325

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

exporter/opentelemetry-exporter-otlp-proto-http/src/opentelemetry/exporter/otlp/proto/http/trace_exporter/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747

4848

4949
DEFAULT_COMPRESSION = Compression.NoCompression
50-
DEFAULT_ENDPOINT = "http://localhost:4318/v1/traces"
50+
DEFAULT_ENDPOINT = "http://localhost:4318/"
51+
DEFAULT_TRACES_EXPORT_PATH = "v1/traces"
5152
DEFAULT_TIMEOUT = 10 # in seconds
5253

5354

@@ -65,7 +66,9 @@ def __init__(
6566
):
6667
self._endpoint = endpoint or environ.get(
6768
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
68-
environ.get(OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_ENDPOINT),
69+
_append_trace_path(
70+
environ.get(OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_ENDPOINT)
71+
),
6972
)
7073
self._certificate_file = certificate_file or environ.get(
7174
OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE,
@@ -172,3 +175,9 @@ def _compression_from_env() -> Compression:
172175
.strip()
173176
)
174177
return Compression(compression)
178+
179+
180+
def _append_trace_path(endpoint: str) -> str:
181+
if endpoint.endswith("/"):
182+
return endpoint + DEFAULT_TRACES_EXPORT_PATH
183+
return endpoint + f"/{DEFAULT_TRACES_EXPORT_PATH}"

exporter/opentelemetry-exporter-otlp-proto-http/tests/test_proto_span_exporter.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
DEFAULT_COMPRESSION,
2121
DEFAULT_ENDPOINT,
2222
DEFAULT_TIMEOUT,
23+
DEFAULT_TRACES_EXPORT_PATH,
2324
OTLPSpanExporter,
2425
)
2526
from opentelemetry.sdk.environment_variables import (
@@ -47,7 +48,9 @@ def test_constructor_default(self):
4748

4849
exporter = OTLPSpanExporter()
4950

50-
self.assertEqual(exporter._endpoint, DEFAULT_ENDPOINT)
51+
self.assertEqual(
52+
exporter._endpoint, DEFAULT_ENDPOINT + DEFAULT_TRACES_EXPORT_PATH
53+
)
5154
self.assertEqual(exporter._certificate_file, True)
5255
self.assertEqual(exporter._timeout, DEFAULT_TIMEOUT)
5356
self.assertIs(exporter._compression, DEFAULT_COMPRESSION)
@@ -90,6 +93,7 @@ def test_exporter_traces_env_take_priority(self):
9093
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
9194
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
9295
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
96+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://traces.endpoint.env",
9397
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
9498
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
9599
},
@@ -117,7 +121,6 @@ def test_exporter_constructor_take_priority(self):
117121
{
118122
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
119123
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
120-
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
121124
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
122125
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
123126
},
@@ -126,14 +129,39 @@ def test_exporter_env(self):
126129

127130
exporter = OTLPSpanExporter()
128131

129-
self.assertEqual(exporter._endpoint, OS_ENV_ENDPOINT)
130132
self.assertEqual(exporter._certificate_file, OS_ENV_CERTIFICATE)
131133
self.assertEqual(exporter._timeout, int(OS_ENV_TIMEOUT))
132134
self.assertIs(exporter._compression, Compression.Gzip)
133135
self.assertEqual(
134136
exporter._headers, {"envheader1": "val1", "envheader2": "val2"}
135137
)
136138

139+
@patch.dict(
140+
"os.environ",
141+
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT},
142+
)
143+
def test_exporter_env_endpoint_without_slash(self):
144+
145+
exporter = OTLPSpanExporter()
146+
147+
self.assertEqual(
148+
exporter._endpoint,
149+
OS_ENV_ENDPOINT + f"/{DEFAULT_TRACES_EXPORT_PATH}",
150+
)
151+
152+
@patch.dict(
153+
"os.environ",
154+
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT + "/"},
155+
)
156+
def test_exporter_env_endpoint_with_slash(self):
157+
158+
exporter = OTLPSpanExporter()
159+
160+
self.assertEqual(
161+
exporter._endpoint,
162+
OS_ENV_ENDPOINT + f"/{DEFAULT_TRACES_EXPORT_PATH}",
163+
)
164+
137165
@patch.dict(
138166
"os.environ",
139167
{

0 commit comments

Comments
 (0)