Skip to content

Commit

Permalink
OTLP exporter uses scheme from endpoint configuration (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrex authored Apr 16, 2021
1 parent 49c5c2f commit e2a5b0b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/fork-process-model/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion docs/examples/fork-process-model/flask-uwsgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit e2a5b0b

Please sign in to comment.