Skip to content

Commit e2a5b0b

Browse files
author
alrex
authored
OTLP exporter uses scheme from endpoint configuration (#1771)
1 parent 49c5c2f commit e2a5b0b

File tree

7 files changed

+64
-6
lines changed

7 files changed

+64
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
- `opentelemetry-opentracing-shim` Fix an issue in the shim where a Span was being wrapped
3333
in a NonRecordingSpan when it wasn't necessary.
3434
([#1776](https://github.com/open-telemetry/opentelemetry-python/pull/1776))
35+
- OTLP Exporter now uses the scheme in the endpoint to determine whether to establish
36+
a secure connection or not.
37+
([#1771](https://github.com/open-telemetry/opentelemetry-python/pull/1771))
3538

3639
## [1.0.0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.0.0) - 2021-03-26
3740
### Added

docs/examples/fork-process-model/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Gunicorn post_fork hook
3131
3232
trace.set_tracer_provider(TracerProvider(resource=resource))
3333
span_processor = BatchSpanProcessor(
34-
OTLPSpanExporter(endpoint="localhost:4317")
34+
OTLPSpanExporter(endpoint="http://localhost:4317")
3535
)
3636
trace.get_tracer_provider().add_span_processor(span_processor)
3737
@@ -58,7 +58,7 @@ uWSGI postfork decorator
5858
5959
trace.set_tracer_provider(TracerProvider(resource=resource))
6060
span_processor = BatchSpanProcessor(
61-
OTLPSpanExporter(endpoint="localhost:4317")
61+
OTLPSpanExporter(endpoint="http://localhost:4317")
6262
)
6363
trace.get_tracer_provider().add_span_processor(span_processor)
6464

docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ def post_fork(server, worker):
4747
# This uses insecure connection for the purpose of example. Please see the
4848
# OTLP Exporter documentation for other options.
4949
span_processor = BatchSpanProcessor(
50-
OTLPSpanExporter(endpoint="localhost:4317", insecure=True)
50+
OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)
5151
)
5252
trace.get_tracer_provider().add_span_processor(span_processor)

docs/examples/fork-process-model/flask-uwsgi/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def init_tracing():
3838
# This uses insecure connection for the purpose of example. Please see the
3939
# OTLP Exporter documentation for other options.
4040
span_processor = BatchSpanProcessor(
41-
OTLPSpanExporter(endpoint="localhost:4317", insecure=True)
41+
OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)
4242
)
4343
trace.get_tracer_provider().add_span_processor(span_processor)
4444

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
trace.set_tracer_provider(TracerProvider(resource=resource))
5858
tracer = trace.get_tracer(__name__)
5959
60-
otlp_exporter = OTLPSpanExporter(endpoint="localhost:4317", insecure=True)
60+
otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)
6161
6262
span_processor = BatchSpanProcessor(otlp_exporter)
6363

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from typing import Any, Callable, Dict, Generic, List, Optional
2323
from typing import Sequence as TypingSequence
2424
from typing import Text, TypeVar
25+
from urllib import parse
26+
from urllib.parse import urlparse
2527

2628
from backoff import expo
2729
from google.rpc.error_details_pb2 import RetryInfo
@@ -194,9 +196,19 @@ def __init__(
194196
super().__init__()
195197

196198
endpoint = endpoint or environ.get(
197-
OTEL_EXPORTER_OTLP_ENDPOINT, "localhost:4317"
199+
OTEL_EXPORTER_OTLP_ENDPOINT, "http://localhost:4317"
198200
)
199201

202+
parsed_url = urlparse(endpoint)
203+
204+
if insecure is None:
205+
if parsed_url.scheme == "https":
206+
insecure = False
207+
else:
208+
insecure = True
209+
210+
endpoint = parsed_url.netloc
211+
200212
self._headers = headers or environ.get(OTEL_EXPORTER_OTLP_HEADERS)
201213
if isinstance(self._headers, str):
202214
self._headers = tuple(

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,49 @@ def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure):
234234
exporter._headers, (("key3", "value3"), ("key4", "value4"))
235235
)
236236

237+
# pylint: disable=no-self-use
238+
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel")
239+
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.secure_channel")
240+
def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure):
241+
"""Just OTEL_EXPORTER_OTLP_COMPRESSION should work"""
242+
endpoints = [
243+
(
244+
"http://localhost:4317",
245+
None,
246+
mock_insecure,
247+
),
248+
(
249+
"localhost:4317",
250+
None,
251+
mock_insecure,
252+
),
253+
(
254+
"localhost:4317",
255+
False,
256+
mock_secure,
257+
),
258+
(
259+
"https://localhost:4317",
260+
None,
261+
mock_secure,
262+
),
263+
(
264+
"https://localhost:4317",
265+
True,
266+
mock_insecure,
267+
),
268+
]
269+
for endpoint, insecure, mock_method in endpoints:
270+
OTLPSpanExporter(endpoint=endpoint, insecure=insecure)
271+
self.assertEqual(
272+
1,
273+
mock_method.call_count,
274+
"expected {} to be called for {} {}".format(
275+
mock_method, endpoint, insecure
276+
),
277+
)
278+
mock_method.reset_mock()
279+
237280
# pylint: disable=no-self-use
238281
@patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel")
239282
@patch.dict("os.environ", {OTEL_EXPORTER_OTLP_COMPRESSION: "gzip"})

0 commit comments

Comments
 (0)