Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
AWS_XRAY_DAEMON_ADDRESS_CONFIG = "AWS_XRAY_DAEMON_ADDRESS"
OTEL_AWS_PYTHON_DEFER_TO_WORKERS_ENABLED_CONFIG = "OTEL_AWS_PYTHON_DEFER_TO_WORKERS_ENABLED"
SYSTEM_METRICS_INSTRUMENTATION_SCOPE_NAME = "opentelemetry.instrumentation.system_metrics"
OTEL_EXPORTER_OTLP_ENDPOINT = "OTEL_EXPORTER_OTLP_ENDPOINT"
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
OTEL_EXPORTER_OTLP_LOGS_HEADERS = "OTEL_EXPORTER_OTLP_LOGS_HEADERS"
Expand Down Expand Up @@ -406,7 +407,8 @@ def _customize_span_exporter(span_exporter: SpanExporter, resource: Resource) ->
traces_endpoint = os.environ.get(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT)
if _is_lambda_environment():
# Override OTLP http default endpoint to UDP
if isinstance(span_exporter, OTLPSpanExporter) and traces_endpoint is None:
# Check for custom endpoint using both signal-specific and generic env vars per OTel spec
if isinstance(span_exporter, OTLPSpanExporter) and not _has_custom_otlp_traces_endpoint():
traces_endpoint = os.environ.get(AWS_XRAY_DAEMON_ADDRESS_CONFIG, "127.0.0.1:2000")
span_exporter = OTLPUdpSpanExporter(endpoint=traces_endpoint)

Expand Down Expand Up @@ -645,6 +647,18 @@ def _is_lambda_environment():
return AWS_LAMBDA_FUNCTION_NAME_CONFIG in os.environ


def _has_custom_otlp_traces_endpoint():
"""Check if a custom OTLP traces endpoint is configured.

Per OpenTelemetry OTLP Exporter specification, check for signal-specific
endpoint first, then fall back to generic endpoint.
"""
return (
os.environ.get(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) is not None
or os.environ.get(OTEL_EXPORTER_OTLP_ENDPOINT) is not None
)


def _is_aws_otlp_endpoint(otlp_endpoint: Optional[str], service: str) -> bool:
"""Is the given endpoint an AWS OTLP endpoint?"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import (
LAMBDA_SPAN_EXPORT_BATCH_SIZE,
OTEL_AWS_ENHANCED_CODE_ATTRIBUTES,
OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_HEADERS,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
Expand All @@ -40,6 +41,7 @@
_export_unsampled_span_for_agent_observability,
_export_unsampled_span_for_lambda,
_fetch_logs_header,
_has_custom_otlp_traces_endpoint,
_init_logging,
_is_application_signals_enabled,
_is_application_signals_runtime_enabled,
Expand Down Expand Up @@ -388,6 +390,37 @@ def test_customize_span_exporter_with_agent_observability(self):
os.environ.pop("AGENT_OBSERVABILITY_ENABLED", None)
os.environ.pop(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, None)

def test_customize_span_exporter_lambda_with_generic_otlp_endpoint(self):
"""Test that setting OTEL_EXPORTER_OTLP_ENDPOINT in Lambda uses OTLP exporter, not UDP.

Per OpenTelemetry OTLP Exporter specification, the generic endpoint should be
recognized as a custom endpoint and prevent fallback to UDP export.
Fixes: https://github.com/aws-observability/aws-otel-js-instrumentation/issues/297
"""
# Set Lambda environment and generic OTLP endpoint (not signal-specific)
os.environ["AWS_LAMBDA_FUNCTION_NAME"] = "myLambdaFunc"
os.environ[OTEL_EXPORTER_OTLP_ENDPOINT] = "http://my-collector:4318"

try:
mock_exporter = MagicMock(spec=OTLPSpanExporter)
customized_exporter = _customize_span_exporter(mock_exporter, Resource.get_empty())

# Should NOT be converted to UDP - the generic endpoint should be respected
self.assertNotIsInstance(customized_exporter, OTLPUdpSpanExporter)
# Should remain as the original exporter (not wrapped since App Signals not enabled)
self.assertEqual(mock_exporter, customized_exporter)

# Also verify the helper function works correctly
self.assertTrue(_has_custom_otlp_traces_endpoint())
finally:
os.environ.pop("AWS_LAMBDA_FUNCTION_NAME", None)
os.environ.pop(OTEL_EXPORTER_OTLP_ENDPOINT, None)

# Verify helper returns False when no endpoints are set
os.environ.pop(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, None)
os.environ.pop(OTEL_EXPORTER_OTLP_ENDPOINT, None)
self.assertFalse(_has_custom_otlp_traces_endpoint())

def test_customize_span_processors_with_agent_observability(self):
mock_tracer_provider: TracerProvider = MagicMock()

Expand Down