-
Notifications
You must be signed in to change notification settings - Fork 145
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
Fix(metrics): decorated class methods cannot access this
#1059
Conversation
this
Note to reviewers: don't merge this until #1054 is merged. |
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.
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'; |
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.
Won't this use the published version instead of local version?
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.
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):
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.
docs/core/metrics.md
Outdated
// 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(); |
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.
Question(nit): do we we need to export the the Lambda class and the constant handlerClass in these examples?
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.
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.
docs/core/metrics.md
Outdated
|
||
@metrics.logMetrics() | ||
public async handler(_event: any, _context: any): Promise<void> { | ||
metrics.addMetric('successfulBooking', MetricUnits.Count, 1); | ||
} | ||
} | ||
|
||
export const handlerClass = new Lambda(); |
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.
Same as above
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.
docs/core/metrics.md
Outdated
@@ -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(); |
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.
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.
Thanks a lot Andrea!
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
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.