diff --git a/CHANGELOG.md b/CHANGELOG.md index 74d64e59115..ebfb94a195e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `opentelemetry-opentracing-shim` Fix an issue in the shim where a Span was being wrapped in a NonRecordingSpan when it wasn't necessary. ([#1776](https://github.com/open-telemetry/opentelemetry-python/pull/1776)) +- OTLP Exporter now uses the scheme in the endpoint to determine whether to establish + a secure connection or not. + ([#1771](https://github.com/open-telemetry/opentelemetry-python/pull/1771)) ## [1.0.0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.0.0) - 2021-03-26 ### Added diff --git a/docs/examples/fork-process-model/README.rst b/docs/examples/fork-process-model/README.rst index 2bd0c9ecbcd..2f33bcf500a 100644 --- a/docs/examples/fork-process-model/README.rst +++ b/docs/examples/fork-process-model/README.rst @@ -31,7 +31,7 @@ Gunicorn post_fork hook trace.set_tracer_provider(TracerProvider(resource=resource)) span_processor = BatchSpanProcessor( - OTLPSpanExporter(endpoint="localhost:4317") + OTLPSpanExporter(endpoint="http://localhost:4317") ) trace.get_tracer_provider().add_span_processor(span_processor) @@ -58,7 +58,7 @@ uWSGI postfork decorator trace.set_tracer_provider(TracerProvider(resource=resource)) span_processor = BatchSpanProcessor( - OTLPSpanExporter(endpoint="localhost:4317") + OTLPSpanExporter(endpoint="http://localhost:4317") ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py index eaf67773929..902a0133e3b 100644 --- a/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py +++ b/docs/examples/fork-process-model/flask-gunicorn/gunicorn.config.py @@ -47,6 +47,6 @@ def post_fork(server, worker): # This uses insecure connection for the purpose of example. Please see the # OTLP Exporter documentation for other options. span_processor = BatchSpanProcessor( - OTLPSpanExporter(endpoint="localhost:4317", insecure=True) + OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True) ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/docs/examples/fork-process-model/flask-uwsgi/app.py b/docs/examples/fork-process-model/flask-uwsgi/app.py index adcf52691f9..fb56037c06e 100644 --- a/docs/examples/fork-process-model/flask-uwsgi/app.py +++ b/docs/examples/fork-process-model/flask-uwsgi/app.py @@ -38,7 +38,7 @@ def init_tracing(): # This uses insecure connection for the purpose of example. Please see the # OTLP Exporter documentation for other options. span_processor = BatchSpanProcessor( - OTLPSpanExporter(endpoint="localhost:4317", insecure=True) + OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True) ) trace.get_tracer_provider().add_span_processor(span_processor) diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/__init__.py index c4c96b76c0f..0a33b6325ad 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/__init__.py @@ -57,7 +57,7 @@ trace.set_tracer_provider(TracerProvider(resource=resource)) tracer = trace.get_tracer(__name__) - otlp_exporter = OTLPSpanExporter(endpoint="localhost:4317", insecure=True) + otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True) span_processor = BatchSpanProcessor(otlp_exporter) diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py index e3a42a24c53..03b17cf2e42 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py @@ -22,6 +22,8 @@ from typing import Any, Callable, Dict, Generic, List, Optional from typing import Sequence as TypingSequence from typing import Text, TypeVar +from urllib import parse +from urllib.parse import urlparse from backoff import expo from google.rpc.error_details_pb2 import RetryInfo @@ -194,9 +196,19 @@ def __init__( super().__init__() endpoint = endpoint or environ.get( - OTEL_EXPORTER_OTLP_ENDPOINT, "localhost:4317" + OTEL_EXPORTER_OTLP_ENDPOINT, "http://localhost:4317" ) + parsed_url = urlparse(endpoint) + + if insecure is None: + if parsed_url.scheme == "https": + insecure = False + else: + insecure = True + + endpoint = parsed_url.netloc + self._headers = headers or environ.get(OTEL_EXPORTER_OTLP_HEADERS) if isinstance(self._headers, str): self._headers = tuple( diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py index 94fe01767dc..5e0e9dd37b3 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py @@ -234,6 +234,49 @@ def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure): exporter._headers, (("key3", "value3"), ("key4", "value4")) ) + # pylint: disable=no-self-use + @patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel") + @patch("opentelemetry.exporter.otlp.proto.grpc.exporter.secure_channel") + def test_otlp_exporter_endpoint(self, mock_secure, mock_insecure): + """Just OTEL_EXPORTER_OTLP_COMPRESSION should work""" + endpoints = [ + ( + "http://localhost:4317", + None, + mock_insecure, + ), + ( + "localhost:4317", + None, + mock_insecure, + ), + ( + "localhost:4317", + False, + mock_secure, + ), + ( + "https://localhost:4317", + None, + mock_secure, + ), + ( + "https://localhost:4317", + True, + mock_insecure, + ), + ] + for endpoint, insecure, mock_method in endpoints: + OTLPSpanExporter(endpoint=endpoint, insecure=insecure) + self.assertEqual( + 1, + mock_method.call_count, + "expected {} to be called for {} {}".format( + mock_method, endpoint, insecure + ), + ) + mock_method.reset_mock() + # pylint: disable=no-self-use @patch("opentelemetry.exporter.otlp.proto.grpc.exporter.insecure_channel") @patch.dict("os.environ", {OTEL_EXPORTER_OTLP_COMPRESSION: "gzip"})