powertools-lambda-typescript icon indicating copy to clipboard operation
powertools-lambda-typescript copied to clipboard

Bug (Tracer): empty segment name when chaining decorators

Open misterjoshua opened this issue 3 years ago • 0 comments

Bug description

I wanted to capture all the retries of a method that uses a decorator to implement async retries. https://github.com/awslabs/aws-lambda-powertools-typescript/issues/1084#issuecomment-1251630666 However, when the tracer tries to determine the segment name, it doesn't find the name of the original method.

Expected Behavior

I expected the segment name to use the original method name.

Current Behavior

It uses the name of the other decorator's anonymous function instead of the original method's function. An anonymous function's name is empty, so the segment name is empty.

Possible Solution

Use String(_propertyKey) instead of originalMethod.name here: https://github.com/awslabs/aws-lambda-powertools-typescript/blob/06fbaae4db3825557aa84d40372a53422e42840d/packages/tracer/src/Tracer.ts#L437

Steps to Reproduce

const tracer = new Tracer();

function passThrough() {
  // A decorator that calls the original method.
  return (_target: unknown, _propertyKey: string, descriptor: PropertyDescriptor) => {
    const originalMethod = descriptor.value!;
    descriptor.value = function (...args: unknown[]) {
      return originalMethod.apply(this, [...args]);
    };
  };
}

class MyClass {
  @tracer.captureMethod()
  @passThrough()
  async doSomething(): Promise<string> {
    return 'foo';
  }
}

const myClass = new MyClass();
await myClass.doSomething();

// Segment name will be "### "

Environment

  • Powertools version used: 1.2.1
  • Packaging format (Layers, npm): npm
  • AWS Lambda function runtime: node 16
  • Debugging logs:

Related issues, RFCs

None

misterjoshua avatar Sep 19 '22 23:09 misterjoshua