Skip to content

Commit 6564573

Browse files
committed
[AgentObservability] Update resource attributes handling
1 parent 56b1b4f commit 6564573

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
@@ -64,6 +64,7 @@ import { LIB_VERSION } from './version';
6464
import { isAgentObservabilityEnabled } from './utils';
6565
import { BaggageSpanProcessor } from '@opentelemetry/baggage-span-processor';
6666
import { logs } from '@opentelemetry/api-logs';
67+
import { AWS_ATTRIBUTE_KEYS } from './aws-attribute-keys';
6768

6869
const XRAY_OTLP_ENDPOINT_PATTERN = '^https://xray\\.([a-z0-9-]+)\\.amazonaws\\.com/v1/traces$';
6970

@@ -139,8 +140,8 @@ export class AwsOpentelemetryConfigurator {
139140
if (!resourceDetectorsFromEnv.includes('env')) {
140141
defaultDetectors.push(envDetectorSync);
141142
}
142-
} else if (isLambdaEnvironment()) {
143-
// If in Lambda environment, only keep env detector as default
143+
} else if (isLambdaEnvironment() || isAgentObservabilityEnabled()) {
144+
// Only keep env detector here
144145
defaultDetectors.push(envDetectorSync);
145146
} else {
146147
/*
@@ -164,7 +165,7 @@ export class AwsOpentelemetryConfigurator {
164165
detectors: defaultDetectors,
165166
};
166167

167-
autoResource = autoResource.merge(detectResourcesSync(internalConfig));
168+
autoResource = this.customizeResource(autoResource.merge(detectResourcesSync(internalConfig)));
168169
this.resource = autoResource;
169170

170171
this.instrumentations = instrumentations;
@@ -195,6 +196,17 @@ export class AwsOpentelemetryConfigurator {
195196
return autoResource;
196197
}
197198

199+
private customizeResource(resource: Resource) {
200+
if (isAgentObservabilityEnabled()) {
201+
// Add aws.service.type if it doesn't exist in the resource
202+
if (!resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]) {
203+
// Set a default agent type for AI agent observability
204+
resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE] = 'gen_ai_agent';
205+
}
206+
}
207+
return resource;
208+
}
209+
198210
public configure(): Partial<NodeSDKConfiguration> {
199211
// config.autoDetectResources is set to False, as the resources are detected and added to the
200212
// 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
@@ -44,6 +44,8 @@ import { AwsXraySamplingClient } from '../src/sampler/aws-xray-sampling-client';
4444
import { GetSamplingRulesResponse } from '../src/sampler/remote-sampler.types';
4545
import { OTLPAwsSpanExporter } from '../src/otlp-aws-span-exporter';
4646
import { BaggageSpanProcessor } from '@opentelemetry/baggage-span-processor';
47+
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
48+
import { AWS_ATTRIBUTE_KEYS } from '../src/aws-attribute-keys';
4749

4850
// Tests AwsOpenTelemetryConfigurator after running Environment Variable setup in register.ts
4951
describe('AwsOpenTelemetryConfiguratorTest', () => {
@@ -818,4 +820,38 @@ describe('AwsOpenTelemetryConfiguratorTest', () => {
818820
delete process.env.AGENT_OBSERVABILITY_ENABLED;
819821
}
820822
});
823+
824+
it('CustomizeResourceWithoutAgentObservability', () => {
825+
delete process.env.AGENT_OBSERVABILITY_ENABLED;
826+
827+
let resource = new Resource({ [ATTR_SERVICE_NAME]: 'test-service' });
828+
resource = awsOtelConfigurator['customizeResource'](resource);
829+
expect(resource.attributes[ATTR_SERVICE_NAME]).toEqual('test-service');
830+
expect(resource.attributes).not.toHaveProperty(AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE);
831+
});
832+
833+
it('CustomizeResourceWithAgentObservabilityDefault', () => {
834+
process.env.AGENT_OBSERVABILITY_ENABLED = 'true';
835+
836+
let resource = new Resource({ [ATTR_SERVICE_NAME]: 'test-service' });
837+
resource = awsOtelConfigurator['customizeResource'](resource);
838+
expect(resource.attributes[ATTR_SERVICE_NAME]).toEqual('test-service');
839+
expect(resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]).toEqual('gen_ai_agent');
840+
841+
delete process.env.AGENT_OBSERVABILITY_ENABLED;
842+
});
843+
844+
it('CustomizeResourceWithoutAgentObservability', () => {
845+
process.env.AGENT_OBSERVABILITY_ENABLED = 'true';
846+
847+
let resource = new Resource({
848+
[ATTR_SERVICE_NAME]: 'test-service',
849+
[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]: 'existing-agent',
850+
});
851+
resource = awsOtelConfigurator['customizeResource'](resource);
852+
expect(resource.attributes[ATTR_SERVICE_NAME]).toEqual('test-service');
853+
expect(resource.attributes[AWS_ATTRIBUTE_KEYS.AWS_SERVICE_TYPE]).toEqual('existing-agent');
854+
855+
delete process.env.AGENT_OBSERVABILITY_ENABLED;
856+
});
821857
});

0 commit comments

Comments
 (0)