Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
c34f3b9
add logs pipeline
liustve Jun 18, 2025
010e7df
add logs pipeline
liustve Jun 20, 2025
24f4308
Merge remote-tracking branch 'origin/mainline' into logs-mainline
liustve Jun 20, 2025
b75fe99
linting fix
liustve Jun 20, 2025
d588605
linting fix
liustve Jun 20, 2025
c78aca5
linting fix
liustve Jun 20, 2025
12eca32
linting fix
liustve Jun 20, 2025
83ec370
linting fix
liustve Jun 20, 2025
79bbf46
linting fix
liustve Jun 20, 2025
b6e1b97
remove gen ai handling logic
liustve Jun 23, 2025
17d0f90
fixed linting
liustve Jun 23, 2025
3d12858
refactor _init_logging to 1.33.1 version
liustve Jun 24, 2025
7f90bc7
refactored batch log record processor
liustve Jun 24, 2025
fdddb7a
Merge remote-tracking branch 'upstream/main' into logs-mainline
liustve Jun 24, 2025
4b7bb0e
linting
liustve Jun 24, 2025
8c64adb
lint fix
liustve Jun 24, 2025
01e3fd8
update configuration and tests
liustve Jun 24, 2025
2f0268c
lint fix
liustve Jun 24, 2025
7dbcb7e
linting fix
liustve Jun 24, 2025
886b009
Merge remote-tracking branch 'upstream/main' into logs-mainline
liustve Jun 28, 2025
48258c3
linting fix
liustve Jun 28, 2025
57bc772
add cycle detection
liustve Jun 28, 2025
7da6c75
add cycle detection unit tests
liustve Jun 28, 2025
ddea404
linting fix
liustve Jun 28, 2025
87c08bc
linting fix
liustve Jun 28, 2025
e68c4fd
linting fix
liustve Jun 28, 2025
93e2836
add cycle detection
liustve Jun 28, 2025
e1ff7b2
log processor race condition fix
liustve Jun 28, 2025
cb21d39
add comment about termination of loop
liustve Jun 30, 2025
5260e90
conolidate botocore sessions into utils
liustve Jun 30, 2025
0b43920
add explicit setting for aws region from environment variables
liustve Jun 30, 2025
651f283
Merge branch 'main' into logs-mainline
liustve Jul 1, 2025
ff2fb5d
refactored otlp aws log exporter, add comments aws batch log processor
liustve Jul 1, 2025
153679a
Merge branch 'main' into logs-mainline
liustve Jul 1, 2025
bce91dc
linting fix
liustve Jul 1, 2025
8479ac9
Merge branch 'logs-mainline' of https://github.com/liustve/aws-otel-p…
liustve Jul 1, 2025
76e4b47
remove shut down check before sleep
liustve Jul 1, 2025
77c700c
Merge remote-tracking branch 'upstream/consolidate-aws-setup' into ge…
liustve Jul 1, 2025
f0ebea2
linting fix
liustve Jul 1, 2025
6dd6a67
add better estimation for non-ascii characters
liustve Jul 2, 2025
502eb01
linting + formatting fix
liustve Jul 2, 2025
b30ad4f
fix unit test
liustve Jul 2, 2025
8b7e671
linting fix
liustve Jul 2, 2025
dc98cf8
add interruptible shutdown
liustve Jul 2, 2025
3450a11
fix sleep unit tests + renaming aws batch log processor
liustve Jul 2, 2025
7a83e92
linting fix
liustve Jul 2, 2025
a38d43d
fix test
liustve Jul 2, 2025
726a9a8
linting fix
liustve Jul 2, 2025
fc77123
linting fix
liustve Jul 2, 2025
f571ffb
linting fix
liustve Jul 2, 2025
f7fcaaa
Merge remote-tracking branch 'liustve/logs-mainline' into genesis-logs
liustve Jul 2, 2025
c7aeac7
Merge remote-tracking branch 'upstream/main' into genesis-logs
liustve Jul 2, 2025
d7575b8
fixing consolidation changes
liustve Jul 3, 2025
17d4cb9
add comments
liustve Jul 3, 2025
e8a1ec9
linting fix
liustve Jul 3, 2025
9f84183
update tests
liustve Jul 3, 2025
fb7f6c8
linting fix
liustve Jul 3, 2025
22a1bd1
addressed PR comments
liustve Jul 3, 2025
6b50a03
typo fix
liustve Jul 3, 2025
39ce39a
point logs to LOGS_SERVICE
liustve Jul 3, 2025
d1aebbb
removed unncessary comment
liustve Jul 3, 2025
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 @@ -42,7 +42,10 @@ def is_agent_observability_enabled() -> bool:


def get_aws_session():
"""Returns a botocore session only if botocore is installed, otherwise None.
"""
Returns a botocore session only if botocore is installed, otherwise None.
If AWS Region is defined in `AWS_REGION` or `AWS_DEFAULT_REGION` environment variables,
then the region is set in the botocore session before returning.

We do this to prevent runtime errors for ADOT customers that do not need
any features that require botocore.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
OTEL_EXPORTER_OTLP_LOGS_HEADERS = "OTEL_EXPORTER_OTLP_LOGS_HEADERS"

XRAY_SERVICE = "xray"
LOGS_SERIVCE = "logs"
AWS_TRACES_OTLP_ENDPOINT_PATTERN = r"https://xray\.([a-z0-9-]+)\.amazonaws\.com/v1/traces$"
AWS_LOGS_OTLP_ENDPOINT_PATTERN = r"https://logs\.([a-z0-9-]+)\.amazonaws\.com/v1/logs$"

Expand Down Expand Up @@ -297,11 +299,10 @@ def _export_unsampled_span_for_agent_observability(trace_provider: TracerProvide
return

traces_endpoint = os.environ.get(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT)
if traces_endpoint and _is_aws_otlp_endpoint(traces_endpoint):
endpoint = traces_endpoint.lower()
region = endpoint.split(".")[1]
if traces_endpoint and _is_aws_otlp_endpoint(traces_endpoint, XRAY_SERVICE):
endpoint, region = _extract_endpoint_and_region_from_otlp_endpoint(traces_endpoint)
span_exporter = _create_aws_otlp_exporter(endpoint=endpoint, service=XRAY_SERVICE, region=region)

span_exporter = _create_aws_otlp_exporter(endpoint=endpoint, service="xray", region=region)
trace_provider.add_span_processor(BatchUnsampledSpanProcessor(span_exporter=span_exporter))


Expand Down Expand Up @@ -351,7 +352,7 @@ def _custom_import_sampler(sampler_name: str, resource: Resource) -> Sampler:
if sampler_name is None:
sampler_name = "parentbased_always_on"

if sampler_name == "xray":
if sampler_name == XRAY_SERVICE:
# Example env var value
# OTEL_TRACES_SAMPLER_ARG=endpoint=http://localhost:2000,polling_interval=360
sampler_argument_env: str = os.getenv(OTEL_TRACES_SAMPLER_ARG, None)
Expand Down Expand Up @@ -397,17 +398,16 @@ def _customize_span_exporter(span_exporter: SpanExporter, resource: Resource) ->
traces_endpoint = os.environ.get(AWS_XRAY_DAEMON_ADDRESS_CONFIG, "127.0.0.1:2000")
span_exporter = OTLPUdpSpanExporter(endpoint=traces_endpoint)

if traces_endpoint and _is_aws_otlp_endpoint(traces_endpoint, "xray"):
if traces_endpoint and _is_aws_otlp_endpoint(traces_endpoint, XRAY_SERVICE):
_logger.info("Detected using AWS OTLP Traces Endpoint.")

if isinstance(span_exporter, OTLPSpanExporter):
endpoint = traces_endpoint.lower()
region = endpoint.split(".")[1]
return _create_aws_otlp_exporter(endpoint=traces_endpoint, service="xray", region=region)
endpoint, region = _extract_endpoint_and_region_from_otlp_endpoint(traces_endpoint)
return _create_aws_otlp_exporter(endpoint=endpoint, service=XRAY_SERVICE, region=region)

_logger.warning(
"Improper configuration see: please export/set "
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf and OTEL_TRACES_EXPORTER=otlp"
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf ƒnd OTEL_TRACES_EXPORTER=otlp"
)

if not _is_application_signals_enabled():
Expand Down Expand Up @@ -439,12 +439,11 @@ def _customize_logs_exporter(log_exporter: LogExporter) -> LogExporter:
_logger.info("Detected using AWS OTLP Logs Endpoint.")

if isinstance(log_exporter, OTLPLogExporter) and _validate_and_fetch_logs_header().is_valid:
endpoint = logs_endpoint.lower()
region = endpoint.split(".")[1]
endpoint, region = _extract_endpoint_and_region_from_otlp_endpoint(logs_endpoint)
# Setting default compression mode to Gzip as this is the behavior in upstream's
# collector otlp http exporter:
# https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlphttpexporter
return _create_aws_otlp_exporter(endpoint=logs_endpoint, service="logs", region=region)
return _create_aws_otlp_exporter(endpoint=endpoint, service="logs", region=region)

_logger.warning(
"Improper configuration see: please export/set "
Expand Down Expand Up @@ -513,7 +512,7 @@ def _customize_metric_exporters(
metric_readers.append(scope_based_periodic_exporting_metric_reader)

if is_emf_enabled:
emf_exporter = create_emf_exporter()
emf_exporter = _create_emf_exporter()
if emf_exporter:
metric_readers.append(PeriodicExportingMetricReader(emf_exporter))

Expand Down Expand Up @@ -603,17 +602,24 @@ def _is_lambda_environment():
return AWS_LAMBDA_FUNCTION_NAME_CONFIG in os.environ


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

if not otlp_endpoint:
return False

pattern = AWS_TRACES_OTLP_ENDPOINT_PATTERN if service == "xray" else AWS_LOGS_OTLP_ENDPOINT_PATTERN
pattern = AWS_TRACES_OTLP_ENDPOINT_PATTERN if service == XRAY_SERVICE else AWS_LOGS_OTLP_ENDPOINT_PATTERN

return bool(re.match(pattern, otlp_endpoint.lower()))


def _extract_endpoint_and_region_from_otlp_endpoint(endpoint: str):
endpoint = endpoint.lower()
region = endpoint.split(".")[1]

return endpoint, region


def _validate_and_fetch_logs_header() -> OtlpLogHeaderSetting:
"""Checks if x-aws-log-group and x-aws-log-stream are present in the headers in order to send logs to
AWS OTLP Logs endpoint."""
Expand All @@ -630,7 +636,6 @@ def _validate_and_fetch_logs_header() -> OtlpLogHeaderSetting:
log_group = None
log_stream = None
namespace = None
filtered_log_headers_count = 0

for pair in logs_headers.split(","):
if "=" in pair:
Expand All @@ -639,14 +644,12 @@ def _validate_and_fetch_logs_header() -> OtlpLogHeaderSetting:
value = split[1]
if key == AWS_OTLP_LOGS_GROUP_HEADER and value:
log_group = value
filtered_log_headers_count += 1
elif key == AWS_OTLP_LOGS_STREAM_HEADER and value:
log_stream = value
filtered_log_headers_count += 1
elif key == AWS_EMF_METRICS_NAMESPACE and value:
namespace = value

is_valid = filtered_log_headers_count == 2 and log_group is not None and log_stream is not None
is_valid = log_group is not None and log_stream is not None

if not is_valid:
_logger.warning(
Expand Down Expand Up @@ -768,7 +771,7 @@ def _check_emf_exporter_enabled() -> bool:
return True


def create_emf_exporter():
def _create_emf_exporter():
"""Create and configure the CloudWatch EMF exporter."""
try:
session = get_aws_session()
Expand Down Expand Up @@ -805,14 +808,14 @@ def _create_aws_otlp_exporter(endpoint: str, service: str, region: str):
session = get_aws_session()
# Check if botocore is available before importing the AWS exporter
if not session:
_logger.warning("SigV4 Auth requires botocore to be enabled")
_logger.warning("Sigv4 Auth requires botocore to be enabled")
return None

# pylint: disable=import-outside-toplevel
from amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter import OTLPAwsLogExporter
from amazon.opentelemetry.distro.exporter.otlp.aws.traces.otlp_aws_span_exporter import OTLPAwsSpanExporter

if service == "xray":
if service == XRAY_SERVICE:
if is_agent_observability_enabled():
# Span exporter needs an instance of logger provider in ai agent
# observability case because we need to split input/output prompts
Expand All @@ -825,7 +828,7 @@ def _create_aws_otlp_exporter(endpoint: str, service: str, region: str):

return OTLPAwsSpanExporter(session=session, endpoint=endpoint, aws_region=region)

if service == "logs":
if service == LOGS_SERIVCE:
return OTLPAwsLogExporter(session=session, aws_region=region)

return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
OtlpLogHeaderSetting,
_check_emf_exporter_enabled,
_create_aws_otlp_exporter,
_create_emf_exporter,
_custom_import_sampler,
_customize_log_record_processor,
_customize_logs_exporter,
Expand All @@ -42,7 +43,6 @@
_is_defer_to_workers_enabled,
_is_wsgi_master_process,
_validate_and_fetch_logs_header,
create_emf_exporter,
)
from amazon.opentelemetry.distro.aws_opentelemetry_distro import AwsOpenTelemetryDistro
from amazon.opentelemetry.distro.aws_span_metrics_processor import AwsSpanMetricsProcessor
Expand Down Expand Up @@ -1056,7 +1056,7 @@ def test_customize_log_record_processor_with_agent_observability(self, _):
def test_create_emf_exporter(self, mock_get_session, mock_validate):
# Test when botocore is not installed
mock_get_session.return_value = None
result = create_emf_exporter()
result = _create_emf_exporter()
self.assertIsNone(result)

# Reset mock for subsequent tests
Expand All @@ -1074,12 +1074,12 @@ def test_create_emf_exporter(self, mock_get_session, mock_validate):

# Test when headers are invalid
mock_validate.return_value = OtlpLogHeaderSetting(None, None, None, False)
result = create_emf_exporter()
result = _create_emf_exporter()
self.assertIsNone(result)

# Test when namespace is missing (should still create exporter with default namespace)
mock_validate.return_value = OtlpLogHeaderSetting("test-group", "test-stream", None, True)
result = create_emf_exporter()
result = _create_emf_exporter()
self.assertIsNotNone(result)
self.assertEqual(result, mock_exporter_instance)
# Verify that the EMF exporter was called with correct parameters
Expand All @@ -1092,7 +1092,7 @@ def test_create_emf_exporter(self, mock_get_session, mock_validate):

# Test with valid configuration
mock_validate.return_value = OtlpLogHeaderSetting("test-group", "test-stream", "test-namespace", True)
result = create_emf_exporter()
result = _create_emf_exporter()
self.assertIsNotNone(result)
self.assertEqual(result, mock_exporter_instance)
# Verify that the EMF exporter was called with correct parameters
Expand All @@ -1105,7 +1105,7 @@ def test_create_emf_exporter(self, mock_get_session, mock_validate):

# Test exception handling
mock_validate.side_effect = Exception("Test exception")
result = create_emf_exporter()
result = _create_emf_exporter()
self.assertIsNone(result)

@patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.get_logger_provider")
Expand Down Expand Up @@ -1183,7 +1183,9 @@ def test_customize_metric_exporters_with_emf(self):
self.assertEqual(len(metric_readers), 0)

# Test with EMF enabled but create_emf_exporter returns None
with patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.create_emf_exporter", return_value=None):
with patch(
"amazon.opentelemetry.distro.aws_opentelemetry_configurator._create_emf_exporter", return_value=None
):
_customize_metric_exporters(metric_readers, views, is_emf_enabled=True)
self.assertEqual(len(metric_readers), 0)

Expand All @@ -1194,7 +1196,7 @@ def test_customize_metric_exporters_with_emf(self):
mock_emf_exporter._preferred_aggregation = {}

with patch(
"amazon.opentelemetry.distro.aws_opentelemetry_configurator.create_emf_exporter",
"amazon.opentelemetry.distro.aws_opentelemetry_configurator._create_emf_exporter",
return_value=mock_emf_exporter,
):
_customize_metric_exporters(metric_readers, views, is_emf_enabled=True)
Expand Down
Loading