Skip to content

Commit a02218c

Browse files
authored
[AgentObservability] Update resource attributes handling (#207)
*Issue #, if available:* JS equivalent of: - aws-observability/aws-otel-python-instrumentation#414 - aws-observability/aws-otel-python-instrumentation#415 *Description of changes:* By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent c264e08 commit a02218c

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-attribute-keys.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { SEMATTRS_AWS_DYNAMODB_TABLE_NAMES } from '@opentelemetry/semantic-conventions';
55

66
// Utility class holding attribute keys with special meaning to AWS components
7-
export const AWS_ATTRIBUTE_KEYS: { [key: string]: string } = {
7+
export const AWS_ATTRIBUTE_KEYS = {
88
AWS_SPAN_KIND: 'aws.span.kind',
99
AWS_LOCAL_SERVICE: 'aws.local.service',
1010
AWS_LOCAL_OPERATION: 'aws.local.operation',
@@ -45,4 +45,5 @@ export const AWS_ATTRIBUTE_KEYS: { [key: string]: string } = {
4545
AWS_LAMBDA_FUNCTION_NAME: 'aws.lambda.function.name',
4646
AWS_LAMBDA_RESOURCE_MAPPING_ID: 'aws.lambda.resource_mapping.id',
4747
AWS_LAMBDA_FUNCTION_ARN: 'aws.lambda.function.arn',
48+
AWS_SERVICE_TYPE: 'aws.service.type',
4849
};

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-opentelemetry-configurator.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import { OTLPAwsLogExporter } from './exporter/otlp/aws/logs/otlp-aws-log-export
7878
import { isAgentObservabilityEnabled } from './utils';
7979
import { BaggageSpanProcessor } from '@opentelemetry/baggage-span-processor';
8080
import { logs } from '@opentelemetry/api-logs';
81+
import { AWS_ATTRIBUTE_KEYS } from './aws-attribute-keys';
8182

8283
const AWS_TRACES_OTLP_ENDPOINT_PATTERN = '^https://xray\\.([a-z0-9-]+)\\.amazonaws\\.com/v1/traces$';
8384
const AWS_LOGS_OTLP_ENDPOINT_PATTERN = '^https://logs\\.([a-z0-9-]+)\\.amazonaws\\.com/v1/logs$';
@@ -168,8 +169,8 @@ export class AwsOpentelemetryConfigurator {
168169
if (!resourceDetectorsFromEnv.includes('env')) {
169170
defaultDetectors.push(envDetectorSync);
170171
}
171-
} else if (isLambdaEnvironment()) {
172-
// If in Lambda environment, only keep env detector as default
172+
} else if (isLambdaEnvironment() || isAgentObservabilityEnabled()) {
173+
// Only keep env detector here
173174
defaultDetectors.push(envDetectorSync);
174175
} else {
175176
/*
@@ -193,7 +194,7 @@ export class AwsOpentelemetryConfigurator {
193194
detectors: defaultDetectors,
194195
};
195196

196-
autoResource = autoResource.merge(detectResourcesSync(internalConfig));
197+
autoResource = this.customizeResource(autoResource.merge(detectResourcesSync(internalConfig)));
197198
this.resource = autoResource;
198199

199200
this.instrumentations = instrumentations;
@@ -228,6 +229,17 @@ export class AwsOpentelemetryConfigurator {
228229
return autoResource;
229230
}
230231

232+
private customizeResource(resource: Resource) {
233+
if (isAgentObservabilityEnabled()) {
234+
// Add aws.service.type if it doesn't exist in the resource
235+
if (!resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]) {
236+
// Set a default agent type for AI agent observability
237+
resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE] = 'gen_ai_agent';
238+
}
239+
}
240+
return resource;
241+
}
242+
231243
public configure(): Partial<NodeSDKConfiguration> {
232244
// config.autoDetectResources is set to False, as the resources are detected and added to the
233245
// resource ahead of time. The resource is needed to be populated ahead of time instead of letting

aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ import { AwsXRayRemoteSampler } from '../src/sampler/aws-xray-remote-sampler';
5252
import { AwsXraySamplingClient } from '../src/sampler/aws-xray-sampling-client';
5353
import { GetSamplingRulesResponse } from '../src/sampler/remote-sampler.types';
5454
import { BaggageSpanProcessor } from '@opentelemetry/baggage-span-processor';
55+
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
56+
import { AWS_ATTRIBUTE_KEYS } from '../src/aws-attribute-keys';
5557
import {
5658
BatchLogRecordProcessor,
5759
ConsoleLogRecordExporter,
@@ -1183,4 +1185,38 @@ describe('AwsOpenTelemetryConfiguratorTest', () => {
11831185
delete process.env[key];
11841186
}
11851187
}
1188+
1189+
it('CustomizeResourceWithoutAgentObservability', () => {
1190+
delete process.env.AGENT_OBSERVABILITY_ENABLED;
1191+
1192+
let resource = new Resource({ [ATTR_SERVICE_NAME]: 'test-service' });
1193+
resource = awsOtelConfigurator['customizeResource'](resource);
1194+
expect(resource.attributes[ATTR_SERVICE_NAME]).toEqual('test-service');
1195+
expect(resource.attributes).not.toHaveProperty(AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE);
1196+
});
1197+
1198+
it('CustomizeResourceWithAgentObservabilityDefault', () => {
1199+
process.env.AGENT_OBSERVABILITY_ENABLED = 'true';
1200+
1201+
let resource = new Resource({ [ATTR_SERVICE_NAME]: 'test-service' });
1202+
resource = awsOtelConfigurator['customizeResource'](resource);
1203+
expect(resource.attributes[ATTR_SERVICE_NAME]).toEqual('test-service');
1204+
expect(resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]).toEqual('gen_ai_agent');
1205+
1206+
delete process.env.AGENT_OBSERVABILITY_ENABLED;
1207+
});
1208+
1209+
it('CustomizeResourceWithoutAgentObservability', () => {
1210+
process.env.AGENT_OBSERVABILITY_ENABLED = 'true';
1211+
1212+
let resource = new Resource({
1213+
[ATTR_SERVICE_NAME]: 'test-service',
1214+
[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]: 'existing-agent',
1215+
});
1216+
resource = awsOtelConfigurator['customizeResource'](resource);
1217+
expect(resource.attributes[ATTR_SERVICE_NAME]).toEqual('test-service');
1218+
expect(resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]).toEqual('existing-agent');
1219+
1220+
delete process.env.AGENT_OBSERVABILITY_ENABLED;
1221+
});
11861222
});

0 commit comments

Comments
 (0)