From 3b2eb7cb78c1af81c8688e4f01e13f15a39f6b34 Mon Sep 17 00:00:00 2001 From: jjllee Date: Fri, 31 Jan 2025 10:44:24 -0800 Subject: [PATCH] fix issue where lambda cannot use udpexporter --- .../src/aws-opentelemetry-configurator.ts | 46 +++++++------------ .../aws-opentelemetry-configurator.test.ts | 26 ++++++++++- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-opentelemetry-configurator.ts b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-opentelemetry-configurator.ts index f0dc8763..6a6a797b 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-opentelemetry-configurator.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-opentelemetry-configurator.ts @@ -188,35 +188,23 @@ export class AwsOpentelemetryConfigurator { } public configure(): Partial { - let config: Partial; - if (AwsOpentelemetryConfigurator.isApplicationSignalsEnabled()) { - // config.autoDetectResources is set to False, as the resources are detected and added to the - // resource ahead of time. The resource is needed to be populated ahead of time instead of letting - // the OTel Node SDK do the population work because the constructed resource was required to build - // the sampler (if using XRay sampler) and the AwsMetricAttributesSpanExporter and AwsSpanMetricsProcessor - config = { - instrumentations: this.instrumentations, - resource: this.resource, - idGenerator: this.idGenerator, - sampler: this.sampler, - // Error message 'Exporter "otlp" requested through environment variable is unavailable.' - // will appear from BasicTracerProvider that is used in the OTel JS SDK, even though the - // span processors are specified - // https://github.com/open-telemetry/opentelemetry-js/issues/3449 - spanProcessors: this.spanProcessors, - autoDetectResources: false, - textMapPropagator: this.propagator, - }; - } else { - // Default experience config - config = { - instrumentations: this.instrumentations, - resource: this.resource, - sampler: this.sampler, - idGenerator: this.idGenerator, - autoDetectResources: false, - }; - } + // config.autoDetectResources is set to False, as the resources are detected and added to the + // resource ahead of time. The resource is needed to be populated ahead of time instead of letting + // the OTel Node SDK do the population work because the constructed resource was required to build + // the sampler (if using XRay sampler) and the AwsMetricAttributesSpanExporter and AwsSpanMetricsProcessor + const config: Partial = { + instrumentations: this.instrumentations, + resource: this.resource, + idGenerator: this.idGenerator, + sampler: this.sampler, + // Error message 'Exporter "otlp" requested through environment variable is unavailable.' + // will appear from BasicTracerProvider that is used in the OTel JS SDK, even though the + // span processors are specified + // https://github.com/open-telemetry/opentelemetry-js/issues/3449 + spanProcessors: this.spanProcessors, + autoDetectResources: false, + textMapPropagator: this.propagator, + }; return config; } diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts index 197bbe8a..39d82933 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts @@ -9,7 +9,13 @@ import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/expor import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; import { Resource } from '@opentelemetry/resources'; import { PushMetricExporter } from '@opentelemetry/sdk-metrics'; -import { AlwaysOffSampler, ReadableSpan, SpanProcessor, TraceIdRatioBasedSampler } from '@opentelemetry/sdk-trace-base'; +import { + AlwaysOffSampler, + BatchSpanProcessor, + ReadableSpan, + SpanProcessor, + TraceIdRatioBasedSampler, +} from '@opentelemetry/sdk-trace-base'; import { AlwaysOnSampler, NodeTracerProvider, @@ -457,6 +463,24 @@ describe('AwsOpenTelemetryConfiguratorTest', () => { delete process.env.AWS_XRAY_DAEMON_ADDRESS; }); + it('Tests that OTLP exporter from the configurator is UDPExporter when Application Signals is disabled on Lambda', () => { + process.env.AWS_LAMBDA_FUNCTION_NAME = 'TestFunction'; + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'False'; + process.env.OTEL_TRACES_EXPORTER = 'otlp'; + process.env.AWS_XRAY_DAEMON_ADDRESS = 'www.test.com:2222'; + + const config = new AwsOpentelemetryConfigurator([]).configure(); + expect((config.spanProcessors as any)[0]).toBeInstanceOf(BatchSpanProcessor); + expect((config.spanProcessors as any)[0]._exporter).toBeInstanceOf(OTLPUdpSpanExporter); + expect((config.spanProcessors as any)[0]._exporter._endpoint).toBe('www.test.com:2222'); + expect(config.spanProcessors?.length).toEqual(1); + + delete process.env.AWS_LAMBDA_FUNCTION_NAME; + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; + delete process.env.OTEL_TRACES_EXPORTER; + delete process.env.AWS_XRAY_DAEMON_ADDRESS; + }); + it('Test CustomizeSpanProcessors for Lambda', () => { process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True'; process.env.AWS_LAMBDA_FUNCTION_NAME = 'TestFunction';