Skip to content

Commit

Permalink
Set otlp as default span exporter for lambda with AppSignals before o…
Browse files Browse the repository at this point in the history
…verriding it to UDP
  • Loading branch information
mxiamxia committed Sep 4, 2024
1 parent 2d10c16 commit 6da11af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,21 @@ export class AwsSpanProcessorProvider {

static configureOtlp(): SpanExporter {
// eslint-disable-next-line @typescript-eslint/typedef
const protocol = this.getOtlpProtocol();
let protocol = this.getOtlpProtocol();

if (AwsOpentelemetryConfigurator.isApplicationSignalsEnabled() && isLambdaEnvironment()) {
protocol = 'udp';
}
switch (protocol) {
case 'grpc':
return new OTLPGrpcTraceExporter();
case 'http/json':
return new OTLPHttpTraceExporter();
case 'http/protobuf':
return new OTLPProtoTraceExporter();
case 'udp':
diag.debug('Detected AWS Lambda environment and enabling UDPSpanExporter');
return new OTLPUdpSpanExporter(process.env[AWS_XRAY_DAEMON_ADDRESS_CONFIG]);
default:
diag.warn(`Unsupported OTLP traces protocol: ${protocol}. Using http/protobuf.`);
return new OTLPProtoTraceExporter();
Expand Down Expand Up @@ -511,20 +517,7 @@ export class AwsSpanProcessorProvider {

public static customizeSpanExporter(spanExporter: SpanExporter, resource: Resource): SpanExporter {
if (AwsOpentelemetryConfigurator.isApplicationSignalsEnabled()) {
if (isLambdaEnvironment()) {
diag.debug('Detected AWS Lambda environment and enabling UDPSpanExporter');
// When running in Lambda, export Application Signals metrics over UDP
let applicationSignalsEndpoint: string | undefined = process.env[AWS_XRAY_DAEMON_ADDRESS_CONFIG];
if (applicationSignalsEndpoint === undefined) {
applicationSignalsEndpoint = '127.0.0.1:2000';
}
return AwsMetricAttributesSpanExporterBuilder.create(
new OTLPUdpSpanExporter(applicationSignalsEndpoint),
resource
).build();
} else {
return AwsMetricAttributesSpanExporterBuilder.create(spanExporter, resource).build();
}
return AwsMetricAttributesSpanExporterBuilder.create(spanExporter, resource).build();
}
return spanExporter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,13 @@ describe('AwsOpenTelemetryConfiguratorTest', () => {
it('tests Span Exporter on Lambda with ApplicationSignals enabled', () => {
process.env.AWS_LAMBDA_FUNCTION_NAME = 'TestFunction';
process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True';
process.env.AWS_XRAY_DAEMON_ADDRESS = 'www.test.com:2222';
const mockExporter: SpanExporter = sinon.createStubInstance(AwsMetricAttributesSpanExporter);
const mockExporter: SpanExporter = sinon.createStubInstance(OTLPUdpSpanExporter);
const customizedExporter: SpanExporter = AwsSpanProcessorProvider.customizeSpanExporter(
mockExporter,
Resource.empty()
);
// should return UDP exporter for Lambda with AppSignals enabled
expect((customizedExporter as any).delegate).toBeInstanceOf(OTLPUdpSpanExporter);
expect((customizedExporter as any).delegate['_endpoint']).toBe('www.test.com:2222');
delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED;
delete process.env.AWS_LAMBDA_FUNCTION_NAME;
});
Expand All @@ -358,6 +356,20 @@ describe('AwsOpenTelemetryConfiguratorTest', () => {
delete process.env.AWS_LAMBDA_FUNCTION_NAME;
});

it('tests configureOTLP on Lambda with ApplicationSignals enabled', () => {
process.env.AWS_LAMBDA_FUNCTION_NAME = 'TestFunction';
process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True';
process.env.OTEL_TRACES_EXPORTER = 'otlp';
process.env.AWS_XRAY_DAEMON_ADDRESS = 'www.test.com:2222';
const spanExporter: SpanExporter = AwsSpanProcessorProvider.configureOtlp();
expect(spanExporter).toBeInstanceOf(OTLPUdpSpanExporter);
expect((spanExporter as OTLPUdpSpanExporter)['_endpoint']).toBe('www.test.com:2222');
delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED;
delete process.env.AWS_LAMBDA_FUNCTION_NAME;
delete process.env.OTEL_TRACES_EXPORTER;
delete process.env.AWS_XRAY_DAEMON_ADDRESS;
});

function validateConfiguratorEnviron() {
// Set by register.ts
expect('http/protobuf').toEqual(process.env.OTEL_EXPORTER_OTLP_PROTOCOL);
Expand Down

0 comments on commit 6da11af

Please sign in to comment.