Skip to content

Commit 651f283

Browse files
authored
Merge branch 'main' into logs-mainline
2 parents cb21d39 + c3e1190 commit 651f283

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_attribute_keys.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
AWS_LAMBDA_FUNCTION_NAME: str = "aws.lambda.function.name"
3434
AWS_LAMBDA_RESOURCEMAPPING_ID: str = "aws.lambda.resource_mapping.id"
3535
AWS_LAMBDA_FUNCTION_ARN: str = "aws.lambda.function.arn"
36+
AWS_AI_AGENT_TYPE: str = "aws.ai.agent.type"

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from importlib_metadata import version
1111
from typing_extensions import override
1212

13-
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_LOCAL_SERVICE
13+
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_AI_AGENT_TYPE, AWS_LOCAL_SERVICE
1414
from amazon.opentelemetry.distro._aws_resource_attribute_configurator import get_service_attribute
1515
from amazon.opentelemetry.distro._utils import is_agent_observability_enabled, is_installed
1616
from amazon.opentelemetry.distro.always_record_sampler import AlwaysRecordSampler
@@ -181,7 +181,7 @@ def _initialize_components():
181181
AwsEksResourceDetector(),
182182
AwsEcsResourceDetector(),
183183
]
184-
if not _is_lambda_environment()
184+
if not (_is_lambda_environment() or is_agent_observability_enabled())
185185
else []
186186
)
187187

@@ -571,7 +571,15 @@ def _customize_resource(resource: Resource) -> Resource:
571571
if is_unknown:
572572
_logger.debug("No valid service name found")
573573

574-
return resource.merge(Resource.create({AWS_LOCAL_SERVICE: service_name}))
574+
custom_attributes = {AWS_LOCAL_SERVICE: service_name}
575+
576+
if is_agent_observability_enabled():
577+
# Add aws.ai.agent.type if it doesn't exist in the resource
578+
if resource and resource.attributes.get(AWS_AI_AGENT_TYPE) is None:
579+
# Set a default agent type for AI agent observability
580+
custom_attributes[AWS_AI_AGENT_TYPE] = "default"
581+
582+
return resource.merge(Resource.create(custom_attributes))
575583

576584

577585
def _is_application_signals_enabled():

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ def _configure(self, **kwargs):
7070

7171
os.environ.setdefault(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf")
7272

73-
super(AwsOpenTelemetryDistro, self)._configure()
74-
7573
os.environ.setdefault(OTEL_PROPAGATORS, "xray,tracecontext,b3,b3multi")
7674
os.environ.setdefault(OTEL_PYTHON_ID_GENERATOR, "xray")
7775
os.environ.setdefault(
@@ -117,5 +115,7 @@ def _configure(self, **kwargs):
117115
# Disable AWS Application Signals by default
118116
os.environ.setdefault(APPLICATION_SIGNALS_ENABLED_CONFIG, "false")
119117

118+
super(AwsOpenTelemetryDistro, self)._configure()
119+
120120
if kwargs.get("apply_patches", True):
121121
apply_instrumentation_patches()

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from requests import Session
1212

13+
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_AI_AGENT_TYPE, AWS_LOCAL_SERVICE
1314
from amazon.opentelemetry.distro.always_record_sampler import AlwaysRecordSampler
1415
from amazon.opentelemetry.distro.attribute_propagating_span_processor import AttributePropagatingSpanProcessor
1516
from amazon.opentelemetry.distro.aws_batch_unsampled_span_processor import BatchUnsampledSpanProcessor
@@ -27,6 +28,7 @@
2728
_custom_import_sampler,
2829
_customize_logs_exporter,
2930
_customize_metric_exporters,
31+
_customize_resource,
3032
_customize_sampler,
3133
_customize_span_exporter,
3234
_customize_span_processors,
@@ -67,6 +69,7 @@
6769
from opentelemetry.sdk.trace import Span, SpanProcessor, Tracer, TracerProvider
6870
from opentelemetry.sdk.trace.export import SpanExporter
6971
from opentelemetry.sdk.trace.sampling import DEFAULT_ON, Sampler
72+
from opentelemetry.semconv.resource import ResourceAttributes
7073
from opentelemetry.trace import get_tracer_provider
7174

7275

@@ -1085,6 +1088,53 @@ def test_customize_metric_exporters_with_emf(self):
10851088
self.assertEqual(len(metric_readers), 1)
10861089
self.assertIsInstance(metric_readers[0], PeriodicExportingMetricReader)
10871090

1091+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.is_agent_observability_enabled")
1092+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.get_service_attribute")
1093+
def test_customize_resource_without_agent_observability(self, mock_get_service_attribute, mock_is_agent_enabled):
1094+
"""Test _customize_resource when agent observability is disabled"""
1095+
mock_is_agent_enabled.return_value = False
1096+
mock_get_service_attribute.return_value = ("test-service", False)
1097+
1098+
resource = Resource.create({ResourceAttributes.SERVICE_NAME: "test-service"})
1099+
result = _customize_resource(resource)
1100+
1101+
# Should only have AWS_LOCAL_SERVICE added
1102+
self.assertEqual(result.attributes[AWS_LOCAL_SERVICE], "test-service")
1103+
self.assertNotIn(AWS_AI_AGENT_TYPE, result.attributes)
1104+
1105+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.is_agent_observability_enabled")
1106+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.get_service_attribute")
1107+
def test_customize_resource_with_agent_observability_default(
1108+
self, mock_get_service_attribute, mock_is_agent_enabled
1109+
):
1110+
"""Test _customize_resource when agent observability is enabled with default agent type"""
1111+
mock_is_agent_enabled.return_value = True
1112+
mock_get_service_attribute.return_value = ("test-service", False)
1113+
1114+
resource = Resource.create({ResourceAttributes.SERVICE_NAME: "test-service"})
1115+
result = _customize_resource(resource)
1116+
1117+
# Should have both AWS_LOCAL_SERVICE and AWS_AI_AGENT_TYPE with default value
1118+
self.assertEqual(result.attributes[AWS_LOCAL_SERVICE], "test-service")
1119+
self.assertEqual(result.attributes[AWS_AI_AGENT_TYPE], "default")
1120+
1121+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.is_agent_observability_enabled")
1122+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_configurator.get_service_attribute")
1123+
def test_customize_resource_with_existing_agent_type(self, mock_get_service_attribute, mock_is_agent_enabled):
1124+
"""Test _customize_resource when agent type already exists in resource"""
1125+
mock_is_agent_enabled.return_value = True
1126+
mock_get_service_attribute.return_value = ("test-service", False)
1127+
1128+
# Create resource with existing agent type
1129+
resource = Resource.create(
1130+
{ResourceAttributes.SERVICE_NAME: "test-service", AWS_AI_AGENT_TYPE: "existing-agent"}
1131+
)
1132+
result = _customize_resource(resource)
1133+
1134+
# Should preserve existing agent type and not override it
1135+
self.assertEqual(result.attributes[AWS_LOCAL_SERVICE], "test-service")
1136+
self.assertEqual(result.attributes[AWS_AI_AGENT_TYPE], "existing-agent")
1137+
10881138

10891139
def validate_distro_environ():
10901140
tc: TestCase = TestCase()

0 commit comments

Comments
 (0)