Skip to content
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

Fix(metrics): decorated class methods cannot access this #1059

Merged

Conversation

dreamorosi
Copy link
Contributor

Description of your changes

As described in #1057 the decorator implementation was affected by an issue that prevented the decorated method/class to access other class members as the value of this was passed incorrectly.

This PR aims at fixing the issue, creating an unit test case, making minimal changes to the function in the e2e test to verify the change, and update docstrings/docs.

A long form explanation of the issue can be found at #1055, when merged this PR will close #1057.

How to verify this change

See checks below the PR, also see the results of the e2e test run.

Related issues, RFCs

Issue number: #1057

PR status

Is this ready for review?: YES
Is it a breaking change?: NO

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding changes to the examples
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published in downstream module
  • The PR title follows the conventional commit semantics

Breaking change checklist

N/A


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@dreamorosi dreamorosi added bug Something isn't working metrics This item relates to the Metrics Utility fix labels Aug 14, 2022
@dreamorosi dreamorosi self-assigned this Aug 14, 2022
@dreamorosi dreamorosi linked an issue Aug 14, 2022 that may be closed by this pull request
@dreamorosi dreamorosi changed the title Fix(metrics): decorated class methods cannot access this Fix(metrics): decorated class methods cannot access this Aug 14, 2022
@dreamorosi
Copy link
Contributor Author

Note to reviewers: don't merge this until #1054 is merged.

Copy link
Contributor

@ijemmy ijemmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the comment. Want to double check if that is intentional.

Also, similar to #1055, we should also update the examples/cdk/src/*.Decorator.ts files. They still don't use .bind() method. This could be done in another PR. But I prefer to have it now or we will forget about this.

@@ -1,6 +1,6 @@
import { Metrics, MetricUnits } from '../../src';
import { Context } from 'aws-lambda';
import { LambdaInterface } from '../../examples/utils/lambda/LambdaInterface';
import { LambdaInterface } from '@aws-lambda-powertools/commons';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this use the published version instead of local version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, given that we are inside the NPM workspace & it uses the only lock file that is present at the root, then no, it should get the local one (see screenshot below):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

ijemmy
ijemmy previously approved these changes Aug 16, 2022
// Decorate your handler class method
@metrics.logMetrics({ defaultDimensions: DEFAULT_DIMENSIONS })
public async handler(_event: any, _context: any): Promise<void> {
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
}
}

export const handlerClass = new Lambda();
Copy link
Contributor

@saragerion saragerion Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question(nit): do we we need to export the the Lambda class and the constant handlerClass in these examples?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we don't as long as the customer specifies the correct exported one in the function config (handler) in this case.

As you saw from the diff in the other files we were already doing so.


@metrics.logMetrics()
public async handler(_event: any, _context: any): Promise<void> {
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
}
}

export const handlerClass = new Lambda();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -629,6 +639,8 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `singleMetr
}
}

export const myFunction = new Lambda();
export const handler = myFunction.handler;
export const handlerClass = new Lambda();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@saragerion saragerion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot Andrea!

@dreamorosi dreamorosi merged commit 8d0c2a1 into main Aug 18, 2022
@dreamorosi dreamorosi deleted the 1057-bug-metrics-decorated-class-methods-cannot-access-this branch August 18, 2022 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working metrics This item relates to the Metrics Utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: decorated class methods cannot access this in Metrics
3 participants