|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { Span, TraceFlags, Tracer } from '@opentelemetry/api'; |
| 5 | +import { OTLPMetricExporter as OTLPGrpcOTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc'; |
| 6 | +import { OTLPMetricExporter as OTLPHttpOTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'; |
| 7 | +import { Resource } from '@opentelemetry/resources'; |
| 8 | +import { PushMetricExporter } from '@opentelemetry/sdk-metrics'; |
| 9 | +import { ReadableSpan, SpanProcessor } from '@opentelemetry/sdk-trace-base'; |
| 10 | +import { |
| 11 | + AlwaysOnSampler, |
| 12 | + NodeTracerProvider, |
| 13 | + ParentBasedSampler, |
| 14 | + Sampler, |
| 15 | + SpanExporter, |
| 16 | +} from '@opentelemetry/sdk-trace-node'; |
| 17 | +import * as assert from 'assert'; |
| 18 | +import expect from 'expect'; |
| 19 | +import * as sinon from 'sinon'; |
| 20 | +import { AlwaysRecordSampler } from '../src/always-record-sampler'; |
| 21 | +import { AttributePropagatingSpanProcessor } from '../src/attribute-propagating-span-processor'; |
| 22 | +import { AwsMetricAttributesSpanExporter } from '../src/aws-metric-attributes-span-exporter'; |
| 23 | +import { |
| 24 | + ApplicationSignalsExporterProvider, |
| 25 | + AwsOpentelemetryConfigurator, |
| 26 | + AwsSpanProcessorProvider, |
| 27 | + customBuildSamplerFromEnv, |
| 28 | +} from '../src/aws-opentelemetry-configurator'; |
| 29 | +import { AwsSpanMetricsProcessor } from '../src/aws-span-metrics-processor'; |
| 30 | +import { setAwsDefaultEnvironmentVariables } from '../src/register'; |
| 31 | + |
| 32 | +// Tests AwsOpenTelemetryConfigurator after running Environment Variable setup in register.ts |
| 33 | +describe('AwsOpenTelemetryConfiguratorTest', () => { |
| 34 | + let awsOtelConfigurator: AwsOpentelemetryConfigurator; |
| 35 | + |
| 36 | + // setUpClass |
| 37 | + before(() => { |
| 38 | + // Run environment setup in register.ts, then validate expected env values. |
| 39 | + setAwsDefaultEnvironmentVariables(); |
| 40 | + validateConfiguratorEnviron(); |
| 41 | + |
| 42 | + // Overwrite exporter configs to keep tests clean, set sampler configs for tests |
| 43 | + process.env.OTEL_TRACES_EXPORTER = 'none'; |
| 44 | + process.env.OTEL_METRICS_EXPORTER = 'none'; |
| 45 | + process.env.OTEL_LOGS_EXPORTER = 'none'; |
| 46 | + process.env.OTEL_TRACES_SAMPLER = 'traceidratio'; |
| 47 | + process.env.OTEL_TRACES_SAMPLER_ARG = '0.01'; |
| 48 | + |
| 49 | + // Create configurator |
| 50 | + awsOtelConfigurator = new AwsOpentelemetryConfigurator(); |
| 51 | + }); |
| 52 | + |
| 53 | + // The probability of this passing once without correct IDs is low, 20 times is inconceivable. |
| 54 | + it('ProvideGenerateXrayIdsTest', () => { |
| 55 | + const tracerProvider: NodeTracerProvider = new NodeTracerProvider(awsOtelConfigurator.configure()); |
| 56 | + tracerProvider.addSpanProcessor( |
| 57 | + AttributePropagatingSpanProcessor.create((span: ReadableSpan) => '', 'spanNameKey', ['testKey1', 'testKey2']) |
| 58 | + ); |
| 59 | + for (let _: number = 0; _ < 20; _++) { |
| 60 | + const tracer: Tracer = tracerProvider.getTracer('test'); |
| 61 | + const startTimeSec: number = Math.floor(new Date().getTime() / 1000.0); |
| 62 | + const span: Span = tracer.startSpan('test'); |
| 63 | + const traceId: string = span.spanContext().traceId; |
| 64 | + const traceId4ByteHex: string = traceId.substring(0, 8); |
| 65 | + const traceId4ByteNumber: number = Number(`0x${traceId4ByteHex}`); |
| 66 | + expect(traceId4ByteNumber).toBeGreaterThanOrEqual(startTimeSec); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + // Sanity check that the trace ID ratio sampler works fine with the x-ray generator. |
| 71 | + it('TraceIdRatioSamplerTest', () => { |
| 72 | + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True'; |
| 73 | + const tracerProvider: NodeTracerProvider = new NodeTracerProvider(awsOtelConfigurator.configure()); |
| 74 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 75 | + |
| 76 | + tracerProvider.addSpanProcessor( |
| 77 | + AttributePropagatingSpanProcessor.create((span: ReadableSpan) => '', 'spanNameKey', ['testKey1', 'testKey2']) |
| 78 | + ); |
| 79 | + for (let _: number = 0; _ < 20; _++) { |
| 80 | + const numSpans: number = 100000; |
| 81 | + let numSampled: number = 0; |
| 82 | + const tracer: Tracer = tracerProvider.getTracer('test'); |
| 83 | + for (let __: number = 0; __ < numSpans; __++) { |
| 84 | + const span: Span = tracer.startSpan('test'); |
| 85 | + if (span.spanContext().traceFlags & TraceFlags.SAMPLED) { |
| 86 | + numSampled += 1; |
| 87 | + } |
| 88 | + span.end(); |
| 89 | + } |
| 90 | + // Configured for 1%, confirm there are at most 5% to account for randomness and reduce test flakiness. |
| 91 | + expect(0.05).toBeGreaterThan(numSampled / numSpans); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it('ImportDefaultSamplerWhenEnvVarIsNotSetTest', () => { |
| 96 | + delete process.env.OTEL_TRACES_SAMPLER; |
| 97 | + const defaultSampler: Sampler = customBuildSamplerFromEnv(Resource.empty()); |
| 98 | + |
| 99 | + expect(defaultSampler).not.toBeUndefined(); |
| 100 | + expect(defaultSampler.toString()).toEqual(new ParentBasedSampler({ root: new AlwaysOnSampler() }).toString()); |
| 101 | + }); |
| 102 | + |
| 103 | + it('IsApplicationSignalsEnabledTest', () => { |
| 104 | + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True'; |
| 105 | + expect(AwsOpentelemetryConfigurator.isApplicationSignalsEnabled()).toBeTruthy(); |
| 106 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 107 | + |
| 108 | + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'False'; |
| 109 | + expect(AwsOpentelemetryConfigurator.isApplicationSignalsEnabled()).toBeFalsy(); |
| 110 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 111 | + expect(AwsOpentelemetryConfigurator.isApplicationSignalsEnabled()).toBeFalsy(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('CustomizeSamplerTest', () => { |
| 115 | + const mockSampler: Sampler = sinon.createStubInstance(AlwaysOnSampler); |
| 116 | + let customizedSampler: Sampler = AwsOpentelemetryConfigurator.customizeSampler(mockSampler); |
| 117 | + expect(mockSampler).toEqual(customizedSampler); |
| 118 | + |
| 119 | + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True'; |
| 120 | + customizedSampler = AwsOpentelemetryConfigurator.customizeSampler(mockSampler); |
| 121 | + expect(mockSampler).not.toEqual(customizedSampler); |
| 122 | + expect(customizedSampler).toBeInstanceOf(AlwaysRecordSampler); |
| 123 | + expect(mockSampler).toEqual((customizedSampler as any).rootSampler); |
| 124 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 125 | + }); |
| 126 | + |
| 127 | + it('CustomizeExporterTest', () => { |
| 128 | + const mockExporter: SpanExporter = sinon.createStubInstance(AwsMetricAttributesSpanExporter); |
| 129 | + let customizedExporter: SpanExporter = AwsSpanProcessorProvider.customizeSpanExporter( |
| 130 | + mockExporter, |
| 131 | + Resource.empty() |
| 132 | + ); |
| 133 | + expect(mockExporter).toEqual(customizedExporter); |
| 134 | + |
| 135 | + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True'; |
| 136 | + customizedExporter = AwsSpanProcessorProvider.customizeSpanExporter(mockExporter, Resource.empty()); |
| 137 | + expect(mockExporter).not.toEqual(customizedExporter); |
| 138 | + expect(customizedExporter).toBeInstanceOf(AwsMetricAttributesSpanExporter); |
| 139 | + expect(mockExporter).toEqual((customizedExporter as any).delegate); |
| 140 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 141 | + }); |
| 142 | + |
| 143 | + it('CustomizeSpanProcessorsTest', () => { |
| 144 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 145 | + const spanProcessors: SpanProcessor[] = []; |
| 146 | + AwsOpentelemetryConfigurator.customizeSpanProcessors(spanProcessors, Resource.empty()); |
| 147 | + expect(spanProcessors.length).toEqual(0); |
| 148 | + |
| 149 | + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True'; |
| 150 | + AwsOpentelemetryConfigurator.customizeSpanProcessors(spanProcessors, Resource.empty()); |
| 151 | + expect(spanProcessors.length).toEqual(2); |
| 152 | + const firstProcessor: SpanProcessor = spanProcessors[0]; |
| 153 | + expect(firstProcessor).toBeInstanceOf(AttributePropagatingSpanProcessor); |
| 154 | + const secondProcessor: SpanProcessor = spanProcessors[1]; |
| 155 | + expect(secondProcessor).toBeInstanceOf(AwsSpanMetricsProcessor); |
| 156 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 157 | + |
| 158 | + try { |
| 159 | + process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'True'; |
| 160 | + process.env.OTEL_METRIC_EXPORT_INTERVAL = undefined; |
| 161 | + AwsOpentelemetryConfigurator.customizeSpanProcessors(spanProcessors, Resource.empty()); |
| 162 | + process.env.OTEL_METRIC_EXPORT_INTERVAL = '123abc'; |
| 163 | + AwsOpentelemetryConfigurator.customizeSpanProcessors(spanProcessors, Resource.empty()); |
| 164 | + process.env.OTEL_METRIC_EXPORT_INTERVAL = '!@#$%^&*()'; |
| 165 | + AwsOpentelemetryConfigurator.customizeSpanProcessors(spanProcessors, Resource.empty()); |
| 166 | + process.env.OTEL_METRIC_EXPORT_INTERVAL = '123'; |
| 167 | + AwsOpentelemetryConfigurator.customizeSpanProcessors(spanProcessors, Resource.empty()); |
| 168 | + } catch (e: any) { |
| 169 | + assert.fail(`AwsOpentelemetryConfigurator.customizeSpanProcessors() has incorrectly thrown error: ${e}`); |
| 170 | + } finally { |
| 171 | + delete process.env.OTEL_AWS_APPLICATION_SIGNALS_ENABLED; |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + it('ApplicationSignalsExporterProviderTest', () => { |
| 176 | + // Check default protocol - HTTP, as specified by aws-distro-opentelemetry-node-autoinstrumentation's register.ts. |
| 177 | + let exporter: PushMetricExporter = ApplicationSignalsExporterProvider.Instance.createExporter(); |
| 178 | + expect(exporter).toBeInstanceOf(OTLPHttpOTLPMetricExporter); |
| 179 | + expect('http://localhost:4316/v1/metrics').toEqual((exporter as any)._otlpExporter.url); |
| 180 | + |
| 181 | + // Overwrite protocol to gRPC. |
| 182 | + process.env.OTEL_EXPORTER_OTLP_PROTOCOL = 'grpc'; |
| 183 | + exporter = ApplicationSignalsExporterProvider.Instance.createExporter(); |
| 184 | + expect(exporter).toBeInstanceOf(OTLPGrpcOTLPMetricExporter); |
| 185 | + expect('localhost:4315').toEqual((exporter as any)._otlpExporter.url); |
| 186 | + |
| 187 | + // Overwrite protocol back to HTTP. |
| 188 | + process.env.OTEL_EXPORTER_OTLP_PROTOCOL = 'http/protobuf'; |
| 189 | + exporter = ApplicationSignalsExporterProvider.Instance.createExporter(); |
| 190 | + expect(exporter).toBeInstanceOf(OTLPHttpOTLPMetricExporter); |
| 191 | + expect('http://localhost:4316/v1/metrics').toEqual((exporter as any)._otlpExporter.url); |
| 192 | + }); |
| 193 | + |
| 194 | + function validateConfiguratorEnviron() { |
| 195 | + // Set by register.ts |
| 196 | + expect('http/protobuf').toEqual(process.env.OTEL_EXPORTER_OTLP_PROTOCOL); |
| 197 | + expect('xray,tracecontext,b3,b3multi').toEqual(process.env.OTEL_PROPAGATORS); |
| 198 | + |
| 199 | + // Not set |
| 200 | + expect(undefined).toEqual(process.env.OTEL_TRACES_SAMPLER); |
| 201 | + expect(undefined).toEqual(process.env.OTEL_TRACES_SAMPLER_ARG); |
| 202 | + expect(undefined).toEqual(process.env.OTEL_TRACES_EXPORTER); |
| 203 | + expect(undefined).toEqual(process.env.OTEL_METRICS_EXPORTER); |
| 204 | + } |
| 205 | +}); |
0 commit comments