From aa60faee09e691f81defecec74532fa14c191829 Mon Sep 17 00:00:00 2001 From: Martin Schumacher Date: Wed, 23 Apr 2025 12:07:56 +0000 Subject: [PATCH 1/2] =?UTF-8?q?test:=20Test=20f=C3=BCr=20Instrumentierungs?= =?UTF-8?q?fehler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/instrumentation.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index b67a72ca4e..9d0e7f17d2 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -157,11 +157,16 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { From 8f7e0064605050c16f5269605eb86e8559bf4bae Mon Sep 17 00:00:00 2001 From: Martin Schumacher Date: Wed, 23 Apr 2025 12:10:27 +0000 Subject: [PATCH 2/2] test: Tests --- ...ambda-handler.instrumentationError.test.ts | 99 +++++++++++++++++++ .../test/lambda-test/instrumentationError.js | 12 +++ 2 files changed, 111 insertions(+) create mode 100644 plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.instrumentationError.test.ts create mode 100644 plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/instrumentationError.js diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.instrumentationError.test.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.instrumentationError.test.ts new file mode 100644 index 0000000000..40c7c65199 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.instrumentationError.test.ts @@ -0,0 +1,99 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// We access through node_modules to allow it to be patched. +/* eslint-disable node/no-extraneous-require */ + +import * as path from 'path'; + +import { + AwsLambdaInstrumentation, + AwsLambdaInstrumentationConfig, +} from '../../src'; +import { + BatchSpanProcessor, + InMemorySpanExporter, +} from '@opentelemetry/sdk-trace-base'; +import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; +import { Context } from 'aws-lambda'; +import * as assert from 'assert'; + +const memoryExporter = new InMemorySpanExporter(); + +describe('lambda handler', () => { + let instrumentation: AwsLambdaInstrumentation; + + let oldEnv: NodeJS.ProcessEnv; + + const ctx = { + functionName: 'my_function', + invokedFunctionArn: 'my_arn', + awsRequestId: 'aws_request_id', + } as Context; + + const initializeHandler = ( + handler: string, + config: AwsLambdaInstrumentationConfig = {} + ) => { + process.env._HANDLER = handler; + + const provider = new NodeTracerProvider({ + spanProcessors: [new BatchSpanProcessor(memoryExporter)], + }); + provider.register(); + + instrumentation = new AwsLambdaInstrumentation(config); + instrumentation.setTracerProvider(provider); + + return provider; + }; + + const lambdaRequire = (module: string) => + require(path.resolve(__dirname, '..', module)); + + beforeEach(() => { + oldEnv = { ...process.env }; + process.env.LAMBDA_TASK_ROOT = path.resolve(__dirname, '..'); + }); + + afterEach(() => { + process.env = oldEnv; + instrumentation.disable(); + + memoryExporter.reset(); + }); + + describe('handler with module.exports syntax', () => { + it('should not fail', async () => { + initializeHandler('lambda-test/instrumentationError.handler'); + + const result = await new Promise((resolve, reject) => { + lambdaRequire('lambda-test/instrumentationError').handler( + 'arg', + ctx, + (err: Error, res: any) => { + if (err) { + reject(err); + } else { + resolve(res); + } + } + ); + }); + assert.strictEqual(result, 'ok'); + }); + }); +}); diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/instrumentationError.js b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/instrumentationError.js new file mode 100644 index 0000000000..a010102475 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/instrumentationError.js @@ -0,0 +1,12 @@ +const obj = {}; +Object.defineProperty(obj, 'handler', { + get: () => handler, + enumerable: true, + configurable: false, // ❗️Nicht konfigurierbar = Problem für Shimmer +}); + +module.exports = obj; + +async function handler(event) { + return 'ok'; +}