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

Bug (tracer): decorator method names not captured

Open jacksonwelsh opened this issue 1 year ago • 3 comments

Bug description

In the XRay console, method names are not reported by the tracer. The name shown for the subsegment is just ###,

I was trying to set up subsegments for each method in my class.

Expected Behavior

The name of the subsegment should be ### $methodName

Current Behavior

The name of the subsegment is ###

Possible Solution

Steps to Reproduce

class Lambda implements LambdaInterface {
  @tracer.captureMethod()
  public async formatDateForCloudFront(date: Date): Promise<string> {
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  }

  @tracer.captureLambdaHandler()
  public async handler(
    event: APIGatewayProxyEvent,
    _context: any
  ): Promise<void> {
    const date = await formatDateForCloudFront(new Date());
  }
}

Environment

  • Powertools version used: v1.2.1
  • Packaging format (Layers, npm): yarn. Built ts, zipped, uploaded to S3.
  • AWS Lambda function runtime: Node.js 16.x
  • Debugging logs: No relevant logs, but here's the XRay trace (lambda function name obfuscated):

jacksonwelsh - Firefox Developer Edition 2022-09-15 at 19 33 19@2x

Related issues, RFCs

#1084 could be used as a shim in the meantime to remedy this error.

jacksonwelsh avatar Sep 16 '22 00:09 jacksonwelsh

Hi @jacksonwelsh thank you for reporting this. I have tried to reproduce the issue but was not able to.

In order to reproduce the issue I have create a simple Lambda function with the latest version of Powertools Tracer (1.2.1) and used the code below:

import { Tracer } from "@aws-lambda-powertools/tracer";
import { LambdaInterface } from "@aws-lambda-powertools/commons";
import { Context, APIGatewayProxyEvent } from "aws-lambda";

const tracer = new Tracer();

class Lambda implements LambdaInterface {
  @tracer.captureMethod()
  public async formatDateForCloudFront(date: Date): Promise<string> {
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  }

  @tracer.captureLambdaHandler()
  public async handler(
    event: APIGatewayProxyEvent,
    _context: Context
  ): Promise<void> {
    await this.formatDateForCloudFront(new Date());
  }
}

const myFunction = new Lambda();
export const handler = myFunction.handler.bind(myFunction);

The code is very similar to the one you shared in your original comment, with these differences:

  • Inside handler I'm calling the other method with this.formatDateForCloudFront since it's a class method - in your example the formatDateForCloudFront function is not prefixed with this but I assume this was a typo.
  • I'm setting the Context type to the _context param - this should not have any bearing on the example
  • I'm including the two last lines that show how I exported handler so that it can be called by Lambda.

You can find the example above here: https://github.com/dreamorosi/1089-repro

As you can see from the examples below in this sample the name of the method is correctly used as the name of the subsegment.

Below a view of the same trace id 1-63284631-6a69302178279a5553e16df6 with both console experiences. In both cases the name of the method formatDateForCloudFront shows up:

Service Lens (CloudWatch) view: Screenshot 2022-09-19 at 13 41 39

AWS X-Ray console view: image

Given the above, could you please try these things:

  • If possible, try to deploy the sample I have provided and check if you observe the same behavior as your original post
  • If my sample works as intended, could you please provide more details on how you're transpiling & bundling your function?

@misterjoshua: sorry for the notification but I have noticed you gave a thumbs up to this issue, are you also experiencing this same behavior?

dreamorosi avatar Sep 19 '22 11:09 dreamorosi

Thanks for taking a look! I figured out the issue- I was targeting es5 and not es2016, so typescript was converting down to non-decorated functions. Might be worth adding a note about this to the documentation. Switching my tsconfig to target es2016 fixed everything.

jacksonwelsh avatar Sep 19 '22 19:09 jacksonwelsh

@misterjoshua: sorry for the notification but I have noticed you gave a thumbs up to this issue, are you also experiencing this same behavior?

@dreamorosi I had a similar problem as @jacksonwelsh, but after changing my minification settings, the problem was resolved.

misterjoshua avatar Sep 19 '22 21:09 misterjoshua

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues 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[bot] avatar Oct 02 '22 12:10 github-actions[bot]