-
Notifications
You must be signed in to change notification settings - Fork 621
fix(aws-lambda-instrumentation): errors while instrumenting cause instrumented lambda to fail #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...-instrumentation-aws-lambda/test/integrations/lambda-handler.instrumentationError.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'); | ||
| }); | ||
| }); | ||
| }); |
12 changes: 12 additions & 0 deletions
12
...ns/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/instrumentationError.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of letting an error get thrown and potentially relying on catching it, one idea is preemptively determining if
TypeError: Cannot redefine property: handlerwill occur, and in that case we just return[]and don't attempt to useInstrumentationNodeModuleDefinitionorInstrumentationNodeModuleFile.I haven't looked into yet, but is it possible to check if the
"lambda handler is compiled with esbuild and using an immutable ESM export export const handler = function() {} transpiled to CJS."?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, but trying to guess if the wrap-function would throw an error seems not a good approach to me. If the implementation of shimmer changes, we would need to update this as well. Besides, there could be other reasons for wrapping to fail and in my opinion this should never lead to an error in the instrumented code.