95
95
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
96
96
OTEL_EXPORTER_OTLP_LOGS_HEADERS = "OTEL_EXPORTER_OTLP_LOGS_HEADERS"
97
97
98
+ XRAY_SERVICE = "xray"
99
+ LOGS_SERIVCE = "logs"
98
100
AWS_TRACES_OTLP_ENDPOINT_PATTERN = r"https://xray\.([a-z0-9-]+)\.amazonaws\.com/v1/traces$"
99
101
AWS_LOGS_OTLP_ENDPOINT_PATTERN = r"https://logs\.([a-z0-9-]+)\.amazonaws\.com/v1/logs$"
100
102
@@ -297,11 +299,10 @@ def _export_unsampled_span_for_agent_observability(trace_provider: TracerProvide
297
299
return
298
300
299
301
traces_endpoint = os .environ .get (OTEL_EXPORTER_OTLP_TRACES_ENDPOINT )
300
- if traces_endpoint and _is_aws_otlp_endpoint (traces_endpoint ):
301
- endpoint = traces_endpoint . lower ( )
302
- region = endpoint . split ( "." )[ 1 ]
302
+ if traces_endpoint and _is_aws_otlp_endpoint (traces_endpoint , XRAY_SERVICE ):
303
+ endpoint , region = _extract_endpoint_and_region_from_otlp_endpoint ( traces_endpoint )
304
+ span_exporter = _create_aws_otlp_exporter ( endpoint = endpoint , service = XRAY_SERVICE , region = region )
303
305
304
- span_exporter = _create_aws_otlp_exporter (endpoint = endpoint , service = "xray" , region = region )
305
306
trace_provider .add_span_processor (BatchUnsampledSpanProcessor (span_exporter = span_exporter ))
306
307
307
308
@@ -351,7 +352,7 @@ def _custom_import_sampler(sampler_name: str, resource: Resource) -> Sampler:
351
352
if sampler_name is None :
352
353
sampler_name = "parentbased_always_on"
353
354
354
- if sampler_name == "xray" :
355
+ if sampler_name == XRAY_SERVICE :
355
356
# Example env var value
356
357
# OTEL_TRACES_SAMPLER_ARG=endpoint=http://localhost:2000,polling_interval=360
357
358
sampler_argument_env : str = os .getenv (OTEL_TRACES_SAMPLER_ARG , None )
@@ -397,17 +398,16 @@ def _customize_span_exporter(span_exporter: SpanExporter, resource: Resource) ->
397
398
traces_endpoint = os .environ .get (AWS_XRAY_DAEMON_ADDRESS_CONFIG , "127.0.0.1:2000" )
398
399
span_exporter = OTLPUdpSpanExporter (endpoint = traces_endpoint )
399
400
400
- if traces_endpoint and _is_aws_otlp_endpoint (traces_endpoint , "xray" ):
401
+ if traces_endpoint and _is_aws_otlp_endpoint (traces_endpoint , XRAY_SERVICE ):
401
402
_logger .info ("Detected using AWS OTLP Traces Endpoint." )
402
403
403
404
if isinstance (span_exporter , OTLPSpanExporter ):
404
- endpoint = traces_endpoint .lower ()
405
- region = endpoint .split ("." )[1 ]
406
- return _create_aws_otlp_exporter (endpoint = traces_endpoint , service = "xray" , region = region )
405
+ endpoint , region = _extract_endpoint_and_region_from_otlp_endpoint (traces_endpoint )
406
+ return _create_aws_otlp_exporter (endpoint = endpoint , service = XRAY_SERVICE , region = region )
407
407
408
408
_logger .warning (
409
409
"Improper configuration see: please export/set "
410
- "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf and OTEL_TRACES_EXPORTER=otlp"
410
+ "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf ƒnd OTEL_TRACES_EXPORTER=otlp"
411
411
)
412
412
413
413
if not _is_application_signals_enabled ():
@@ -439,12 +439,11 @@ def _customize_logs_exporter(log_exporter: LogExporter) -> LogExporter:
439
439
_logger .info ("Detected using AWS OTLP Logs Endpoint." )
440
440
441
441
if isinstance (log_exporter , OTLPLogExporter ) and _validate_and_fetch_logs_header ().is_valid :
442
- endpoint = logs_endpoint .lower ()
443
- region = endpoint .split ("." )[1 ]
442
+ endpoint , region = _extract_endpoint_and_region_from_otlp_endpoint (logs_endpoint )
444
443
# Setting default compression mode to Gzip as this is the behavior in upstream's
445
444
# collector otlp http exporter:
446
445
# https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlphttpexporter
447
- return _create_aws_otlp_exporter (endpoint = logs_endpoint , service = "logs" , region = region )
446
+ return _create_aws_otlp_exporter (endpoint = endpoint , service = "logs" , region = region )
448
447
449
448
_logger .warning (
450
449
"Improper configuration see: please export/set "
@@ -513,7 +512,7 @@ def _customize_metric_exporters(
513
512
metric_readers .append (scope_based_periodic_exporting_metric_reader )
514
513
515
514
if is_emf_enabled :
516
- emf_exporter = create_emf_exporter ()
515
+ emf_exporter = _create_emf_exporter ()
517
516
if emf_exporter :
518
517
metric_readers .append (PeriodicExportingMetricReader (emf_exporter ))
519
518
@@ -603,17 +602,24 @@ def _is_lambda_environment():
603
602
return AWS_LAMBDA_FUNCTION_NAME_CONFIG in os .environ
604
603
605
604
606
- def _is_aws_otlp_endpoint (otlp_endpoint : Optional [str ] = None , service : str = "xray" ) -> bool :
605
+ def _is_aws_otlp_endpoint (otlp_endpoint : Optional [str ], service : str ) -> bool :
607
606
"""Is the given endpoint an AWS OTLP endpoint?"""
608
607
609
608
if not otlp_endpoint :
610
609
return False
611
610
612
- pattern = AWS_TRACES_OTLP_ENDPOINT_PATTERN if service == "xray" else AWS_LOGS_OTLP_ENDPOINT_PATTERN
611
+ pattern = AWS_TRACES_OTLP_ENDPOINT_PATTERN if service == XRAY_SERVICE else AWS_LOGS_OTLP_ENDPOINT_PATTERN
613
612
614
613
return bool (re .match (pattern , otlp_endpoint .lower ()))
615
614
616
615
616
+ def _extract_endpoint_and_region_from_otlp_endpoint (endpoint : str ):
617
+ endpoint = endpoint .lower ()
618
+ region = endpoint .split ("." )[1 ]
619
+
620
+ return endpoint , region
621
+
622
+
617
623
def _validate_and_fetch_logs_header () -> OtlpLogHeaderSetting :
618
624
"""Checks if x-aws-log-group and x-aws-log-stream are present in the headers in order to send logs to
619
625
AWS OTLP Logs endpoint."""
@@ -630,7 +636,6 @@ def _validate_and_fetch_logs_header() -> OtlpLogHeaderSetting:
630
636
log_group = None
631
637
log_stream = None
632
638
namespace = None
633
- filtered_log_headers_count = 0
634
639
635
640
for pair in logs_headers .split ("," ):
636
641
if "=" in pair :
@@ -639,14 +644,12 @@ def _validate_and_fetch_logs_header() -> OtlpLogHeaderSetting:
639
644
value = split [1 ]
640
645
if key == AWS_OTLP_LOGS_GROUP_HEADER and value :
641
646
log_group = value
642
- filtered_log_headers_count += 1
643
647
elif key == AWS_OTLP_LOGS_STREAM_HEADER and value :
644
648
log_stream = value
645
- filtered_log_headers_count += 1
646
649
elif key == AWS_EMF_METRICS_NAMESPACE and value :
647
650
namespace = value
648
651
649
- is_valid = filtered_log_headers_count == 2 and log_group is not None and log_stream is not None
652
+ is_valid = log_group is not None and log_stream is not None
650
653
651
654
if not is_valid :
652
655
_logger .warning (
@@ -768,7 +771,7 @@ def _check_emf_exporter_enabled() -> bool:
768
771
return True
769
772
770
773
771
- def create_emf_exporter ():
774
+ def _create_emf_exporter ():
772
775
"""Create and configure the CloudWatch EMF exporter."""
773
776
try :
774
777
session = get_aws_session ()
@@ -805,14 +808,14 @@ def _create_aws_otlp_exporter(endpoint: str, service: str, region: str):
805
808
session = get_aws_session ()
806
809
# Check if botocore is available before importing the AWS exporter
807
810
if not session :
808
- _logger .warning ("SigV4 Auth requires botocore to be enabled" )
811
+ _logger .warning ("Sigv4 Auth requires botocore to be enabled" )
809
812
return None
810
813
811
814
# pylint: disable=import-outside-toplevel
812
815
from amazon .opentelemetry .distro .exporter .otlp .aws .logs .otlp_aws_logs_exporter import OTLPAwsLogExporter
813
816
from amazon .opentelemetry .distro .exporter .otlp .aws .traces .otlp_aws_span_exporter import OTLPAwsSpanExporter
814
817
815
- if service == "xray" :
818
+ if service == XRAY_SERVICE :
816
819
if is_agent_observability_enabled ():
817
820
# Span exporter needs an instance of logger provider in ai agent
818
821
# observability case because we need to split input/output prompts
@@ -825,7 +828,7 @@ def _create_aws_otlp_exporter(endpoint: str, service: str, region: str):
825
828
826
829
return OTLPAwsSpanExporter (session = session , endpoint = endpoint , aws_region = region )
827
830
828
- if service == "logs" :
831
+ if service == LOGS_SERIVCE :
829
832
return OTLPAwsLogExporter (session = session , aws_region = region )
830
833
831
834
return None
0 commit comments