Skip to content

Bug: idempotent decorator in combination does not maintain class scope #2692

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
HaaLeo opened this issue Jun 26, 2024 · 6 comments · Fixed by #2693
Closed

Bug: idempotent decorator in combination does not maintain class scope #2692

HaaLeo opened this issue Jun 26, 2024 · 6 comments · Fixed by #2693
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped idempotency This item relates to the Idempotency Utility

Comments

@HaaLeo
Copy link
Contributor

HaaLeo commented Jun 26, 2024

Expected Behavior

This is probably a follow up to #1481
When the @idempotent decorator is used with other powertools decorators such as the @tracer.captureLambdaHandler() decorator the lambda function executes without an error with all the features of the decorators.

Current Behavior

This is probably a follow up to #1481
When the @idempotent decorator is used with other powertools decorators such as the @tracer.captureLambdaHandler() decorator the lambda function breaks

Code snippet

import type { EventBridgeEvent, EventBridgeHandler } from 'aws-lambda';
import { IdempotencyConfig, idempotent } from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import { Tracer } from '@aws-lambda-powertools/tracer';

const tracer = new Tracer({ serviceName: 'foobarservice' });
const persistenceStore = new DynamoDBPersistenceLayer({
    tableName: 'foo-table',
    keyAttr: 'partitionKey',
    sortKeyAttr: 'sk',
    inProgressExpiryAttr: 'inProgressExpiration',
    staticPkValue: 'Idempotency#pk'
});
const config = new IdempotencyConfig({ expiresAfterSeconds: 60, eventKeyJmesPath: 'detail.deduplicationId' });

export class RequestStatusChangedListener {
    private readonly logger = new MyCustomLogger();

    @tracer.captureLambdaHandler()
    @idempotent({ persistenceStore, config })
    public async handler(event: EventBridgeEvent<string, unknown>): Promise<string> {
        this.logger.debug(`Received event="${JSON.stringify(event)}".`);

        return 'success';
    }
}

const listener = new RequestStatusChangedListener();
export const handler: EventBridgeHandler<string, unknown, string> = listener.handler.bind(listener);

Steps to Reproduce

  1. Deploy the code snippet
  2. Add some dummy custom logger
  3. Invoke the lambda function

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

{
    "errorType": "TypeError",
    "errorMessage": "Cannot read properties of undefined (reading 'debug')",
    "stack": [
        "TypeError: Cannot read properties of undefined (reading 'debug')",
        "    at qg.handler (file:///var/task/index.mjs:125:1450)",
        "    at qg.getFunctionResult (file:///var/task/index.mjs:111:22338)",
        "    at qg.handle (file:///var/task/index.mjs:111:22683)",
        "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
        "    at async file:///var/task/index.mjs:111:39254"
    ]
}
@HaaLeo HaaLeo added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Jun 26, 2024
@HaaLeo
Copy link
Contributor Author

HaaLeo commented Jun 26, 2024

Update: Switching the order of the decorators did not change the behavior.

@dreamorosi
Copy link
Contributor

Hi @HaaLeo - thanks for opening this issue.

I'm looking into this and will report back here.

@dreamorosi dreamorosi self-assigned this Jun 26, 2024
@dreamorosi dreamorosi added idempotency This item relates to the Idempotency Utility discussing The issue needs to be discussed, elaborated, or refined labels Jun 26, 2024
@dreamorosi
Copy link
Contributor

I am able to reproduce the issue, and at first glance it does seem to be related to the issue you linked.

After testing however, it doesn't appear to be related to the interaction with other decorators. If I remove the Tracer decorator altogether, the handler is still not able to access the logger property.

Will continue to look into it.

@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed triage This item has not been triaged by a maintainer, please wait discussing The issue needs to be discussed, elaborated, or refined labels Jun 26, 2024
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) Jun 26, 2024
@dreamorosi dreamorosi changed the title Bug: idempotent decorator in combination with tracer breaks lambda handler Bug: idempotent decorator in combination does not maintain class scope Jun 26, 2024
@dreamorosi
Copy link
Contributor

Hi @HaaLeo - you were right on the root cause of the issue and we were not preserving the scope of the decorated method.

I have just put up a PR to address the bug. I have tested a build that includes the fix with the code snippet you shared and it seems to work.

@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Jun 26, 2024
@dreamorosi dreamorosi moved this from Working on it to Pending review in Powertools for AWS Lambda (TypeScript) Jun 26, 2024
@github-project-automation github-project-automation bot moved this from Pending review to Coming soon in Powertools for AWS Lambda (TypeScript) Jun 26, 2024
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jun 26, 2024
Copy link
Contributor

This is now released under v2.3.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jun 27, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped idempotency This item relates to the Idempotency Utility
Projects
Development

Successfully merging a pull request may close this issue.

2 participants