Skip to content

Adding support for setting OTLP exporter protocol by env vars #2893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2870](https://github.com/open-telemetry/opentelemetry-python/pull/2870))
- Fix: Remove `LogEmitter.flush()` to align with OTel Log spec
([#2863](https://github.com/open-telemetry/opentelemetry-python/pull/2863))
- Add support for setting OTLP export protocol with env vars, as defined in the
[specifications](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specify-protocol)
([#2893](https://github.com/open-telemetry/opentelemetry-python/pull/2893))

## [1.12.0-0.33b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0) - 2022-08-08

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
from typing import Dict, Optional, Sequence, Tuple, Type

from pkg_resources import iter_entry_points
from typing_extensions import Literal

from opentelemetry.environment_variables import (
OTEL_LOGS_EXPORTER,
OTEL_METRICS_EXPORTER,
OTEL_PYTHON_ID_GENERATOR,
OTEL_TRACES_EXPORTER,
)
from opentelemetry.environment_variables import OTEL_PYTHON_ID_GENERATOR
from opentelemetry.metrics import set_meter_provider
from opentelemetry.sdk._logs import (
LogEmitterProvider,
Expand All @@ -40,6 +36,7 @@
from opentelemetry.sdk._logs.export import BatchLogProcessor, LogExporter
from opentelemetry.sdk.environment_variables import (
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED,
OTEL_EXPORTER_OTLP_PROTOCOL,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
Expand All @@ -55,26 +52,88 @@

_EXPORTER_OTLP = "otlp"
_EXPORTER_OTLP_PROTO_GRPC = "otlp_proto_grpc"
_EXPORTER_OTLP_PROTO_HTTP = "otlp_proto_http"

_RANDOM_ID_GENERATOR = "random"
_DEFAULT_ID_GENERATOR = _RANDOM_ID_GENERATOR

_logger = logging.getLogger(__name__)


def _get_id_generator() -> str:
return environ.get(OTEL_PYTHON_ID_GENERATOR, _DEFAULT_ID_GENERATOR)


def _get_exporter_names(names: str) -> Sequence[str]:
exporters = set()
def _get_otlp_exporter_entry_point(
otlp_protocol: Optional[str],
) -> Literal[_EXPORTER_OTLP_PROTO_GRPC, _EXPORTER_OTLP_PROTO_HTTP]:
if otlp_protocol == "grpc":
return _EXPORTER_OTLP_PROTO_GRPC
if otlp_protocol == "http/protobuf":
return _EXPORTER_OTLP_PROTO_HTTP
raise RuntimeError(
f"Unsupported OTLP protocol '{otlp_protocol}' is configured"
)


def _get_exporter_entry_point(
exporter_name: str, telemetry_type: Literal["traces", "metrics", "logs"]
):
if exporter_name not in (
_EXPORTER_OTLP,
_EXPORTER_OTLP_PROTO_GRPC,
_EXPORTER_OTLP_PROTO_HTTP,
):
return exporter_name

exporter_entry_point = None

# Checking env vars for OTLP protocol (grpc/http).
# If grpc/http already specified by exporter_name, will only log a warning
# in case of a conflict.
otlp_protocol_var = environ.get(
f"OTEL_EXPORTER_OTLP_{telemetry_type.upper()}_PROTOCOL"
)
if otlp_protocol_var:
exporter_entry_point = _get_otlp_exporter_entry_point(
otlp_protocol_var
)
else:
# Check the general env var
otlp_protocol_var = environ.get(OTEL_EXPORTER_OTLP_PROTOCOL)
if otlp_protocol_var:
exporter_entry_point = _get_otlp_exporter_entry_point(
otlp_protocol_var
)

if exporter_name == _EXPORTER_OTLP:
return (
exporter_entry_point
if exporter_entry_point
else _EXPORTER_OTLP_PROTO_GRPC
)

if exporter_entry_point and exporter_name != exporter_entry_point:
_logger.warning(
"Conflicting values for %s OTLP exporter protocol, using '%s'",
telemetry_type.lower(),
exporter_name,
)

return exporter_name

if names and names.lower().strip() != "none":
exporters.update({_exporter.strip() for _exporter in names.split(",")})

if _EXPORTER_OTLP in exporters:
exporters.remove(_EXPORTER_OTLP)
exporters.add(_EXPORTER_OTLP_PROTO_GRPC)
def _get_exporter_names(
telemetry_type: Literal["traces", "metrics", "logs"]
) -> Sequence[str]:
names = environ.get(f"OTEL_{telemetry_type.upper()}_EXPORTER")
if not names or names.lower().strip() == "none":
return []

return list(exporters)
return [
_get_exporter_entry_point(_exporter.strip(), telemetry_type)
for _exporter in names.split(",")
]


def _init_tracing(
Expand Down Expand Up @@ -232,9 +291,9 @@ def _import_id_generator(id_generator_name: str) -> IdGenerator:

def _initialize_components(auto_instrumentation_version):
trace_exporters, metric_exporters, log_exporters = _import_exporters(
_get_exporter_names(environ.get(OTEL_TRACES_EXPORTER)),
_get_exporter_names(environ.get(OTEL_METRICS_EXPORTER)),
_get_exporter_names(environ.get(OTEL_LOGS_EXPORTER)),
_get_exporter_names("traces"),
_get_exporter_names("metrics"),
_get_exporter_names("logs"),
)
id_generator_name = _get_id_generator()
id_generator = _import_id_generator(id_generator_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@
OTLP exporter.
"""

OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
"""
.. envvar:: OTEL_EXPORTER_OTLP_TRACES_PROTOCOL

The :envvar:`OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` represents the the transport protocol for spans.
"""

OTEL_EXPORTER_OTLP_METRICS_PROTOCOL = "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL"
"""
.. envvar:: OTEL_EXPORTER_OTLP_METRICS_PROTOCOL

The :envvar:`OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` represents the the transport protocol for metrics.
"""

OTEL_EXPORTER_OTLP_LOGS_PROTOCOL = "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL"
"""
.. envvar:: OTEL_EXPORTER_OTLP_LOGS_PROTOCOL

The :envvar:`OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` represents the the transport protocol for logs.
"""

OTEL_EXPORTER_OTLP_CERTIFICATE = "OTEL_EXPORTER_OTLP_CERTIFICATE"
"""
.. envvar:: OTEL_EXPORTER_OTLP_CERTIFICATE
Expand Down Expand Up @@ -314,13 +335,6 @@
A scheme of https indicates a secure connection and takes precedence over this configuration setting.
"""

OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
"""
.. envvar:: OTEL_EXPORTER_OTLP_TRACES_PROTOCOL

The :envvar:`OTEL_EXPORTER_OTLP_TRACES_PROTOCOL` represents the the transport protocol for spans.
"""

OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"
"""
.. envvar:: OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE
Expand Down
70 changes: 61 additions & 9 deletions opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from opentelemetry.sdk._configuration import (
_EXPORTER_OTLP,
_EXPORTER_OTLP_PROTO_GRPC,
_EXPORTER_OTLP_PROTO_HTTP,
_get_exporter_names,
_get_id_generator,
_import_exporters,
Expand Down Expand Up @@ -413,25 +414,76 @@ def test_metrics_init_exporter(self):


class TestExporterNames(TestCase):
def test_otlp_exporter_overwrite(self):
for exporter in [_EXPORTER_OTLP, _EXPORTER_OTLP_PROTO_GRPC]:
self.assertEqual(
_get_exporter_names(exporter), [_EXPORTER_OTLP_PROTO_GRPC]
)
@patch.dict(
environ,
{
"OTEL_TRACES_EXPORTER": _EXPORTER_OTLP,
"OTEL_METRICS_EXPORTER": _EXPORTER_OTLP_PROTO_GRPC,
"OTEL_LOGS_EXPORTER": _EXPORTER_OTLP_PROTO_HTTP,
},
)
def test_otlp_exporter(self):
self.assertEqual(
_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_GRPC]
)
self.assertEqual(
_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]
)
self.assertEqual(
_get_exporter_names("logs"), [_EXPORTER_OTLP_PROTO_HTTP]
)

@patch.dict(
environ,
{
"OTEL_TRACES_EXPORTER": _EXPORTER_OTLP,
"OTEL_METRICS_EXPORTER": _EXPORTER_OTLP,
"OTEL_EXPORTER_OTLP_PROTOCOL": "http/protobuf",
"OTEL_EXPORTER_OTLP_METRICS_PROTOCOL": "grpc",
},
)
def test_otlp_custom_exporter(self):
self.assertEqual(
_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]
)
self.assertEqual(
_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]
)

@patch.dict(
environ,
{
"OTEL_TRACES_EXPORTER": _EXPORTER_OTLP_PROTO_HTTP,
"OTEL_METRICS_EXPORTER": _EXPORTER_OTLP_PROTO_GRPC,
"OTEL_EXPORTER_OTLP_PROTOCOL": "grpc",
"OTEL_EXPORTER_OTLP_METRICS_PROTOCOL": "http/protobuf",
},
)
def test_otlp_exporter_conflict(self):
# Verify that OTEL_*_EXPORTER is used
self.assertEqual(
_get_exporter_names("traces"), [_EXPORTER_OTLP_PROTO_HTTP]
)
self.assertEqual(
_get_exporter_names("metrics"), [_EXPORTER_OTLP_PROTO_GRPC]
)

@patch.dict(environ, {"OTEL_TRACES_EXPORTER": "jaeger,zipkin"})
def test_multiple_exporters(self):
self.assertEqual(
sorted(_get_exporter_names("jaeger,zipkin")), ["jaeger", "zipkin"]
sorted(_get_exporter_names("traces")), ["jaeger", "zipkin"]
)

@patch.dict(environ, {"OTEL_TRACES_EXPORTER": "none"})
def test_none_exporters(self):
self.assertEqual(sorted(_get_exporter_names("none")), [])
self.assertEqual(sorted(_get_exporter_names("traces")), [])

def test_no_exporters(self):
self.assertEqual(sorted(_get_exporter_names(None)), [])
self.assertEqual(sorted(_get_exporter_names("traces")), [])

@patch.dict(environ, {"OTEL_TRACES_EXPORTER": ""})
def test_empty_exporters(self):
self.assertEqual(sorted(_get_exporter_names("")), [])
self.assertEqual(sorted(_get_exporter_names("traces")), [])


class TestImportExporters(TestCase):
Expand Down