-
Notifications
You must be signed in to change notification settings - Fork 153
Bug: @tracer.captureMethod() is not bound to the decorated class #1028
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
Comments
Thank you for opening the issue, appreciate your time. I was able to reproduce the bug described using this code: import { Tracer } from "@aws-lambda-powertools/tracer";
import { LambdaInterface } from "@aws-lambda-powertools/commons";
const tracer = new Tracer();
export class Lambda implements LambdaInterface {
@tracer.captureMethod()
myMethod() {
//other method is undefined as "this" is no longer the Lambda class
this.otherMethod();
}
otherMethod() {
console.log("other method");
}
@tracer.captureLambdaHandler()
public async handler(_event: any, _context: any): Promise<void> {
this.myMethod();
}
}
export const handlerClass = new Lambda();
export const handler = handlerClass.handler; Given that you have already opened a PR to fix this I'm gonna assign the issue to you. |
|
@dreamorosi Hey. I tested import { Tracer } from '@aws-lambda-powertools/tracer';
test('bug', async () => {
const tracer = new Tracer();
class BugClass {
constructor(private readonly callback: () => string) {}
@tracer.captureMethod()
async getFoo(): Promise<string> {
return this.callback();
}
}
const myClass = new BugClass(() => 'somevalue');
await expect(myClass.getFoo()).resolves.toEqual('somevalue');
}); Here is a screenshot demonstrating the unexpected error: And here is a file from a repository that can reproduce the issue: You can test this for yourself by typing the following: git clone https://github.com/misterjoshua/lpt-tracer-bug.git
cd lpt-tracer-bug
yarn install
yarn test |
After looking into it a bit more it seems that the issue is not anymore related to how For instance, the example below works as expected: import { Tracer } from "@aws-lambda-powertools/tracer";
import { LambdaInterface } from "@aws-lambda-powertools/commons";
const tracer = new Tracer({ serviceName: "serverlessAirline" });
class BugClass {
constructor() {}
private ok(): void {
console.log("HELLO");
return;
}
@tracer.captureMethod()
async getFoo(): Promise<void> {
return this.ok();
}
}
class Lambda implements LambdaInterface {
@tracer.captureMethod()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public async dummyMethod(): Promise<string> {
return `otherMethod:${this.otherMethod()}`;
}
@tracer.captureLambdaHandler()
public async handler(_event: any, _context: any): Promise<string> {
const bugClass = new BugClass();
await bugClass.getFoo();
return await this.dummyMethod();
}
public otherMethod(): string {
return "otherMethod";
}
}
export const myFunction = new Lambda();
export const handler = myFunction.handler; To be honest I'm not sure if this is an expected behavior in TS decorators, I'd have to investigate more. But definitely I would consider this a separate issue from the first one that you reported - which was very valid and that was fixed. |
Closing this in favor of #1056 |
|
Bug description
The @tracer.captureMethod() decorator is bound to the decorator not the class its decorating.
Expected Behavior
this.otherMethod()
should call thethis.otherMethod()
in the same classCurrent Behavior
this.otherMethod()
is undefinedPossible Solution
see #1026
the
@tracer.captureLambdaHandler()
decorator already does thisSteps to Reproduce
@tracer.captureMethod()
to one or more of the functionEnvironment
Related issues, RFCs
#1026
The text was updated successfully, but these errors were encountered: