powertools-lambda-typescript
powertools-lambda-typescript copied to clipboard
Bug (tracer): decorator method names not captured
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):
Related issues, RFCs
#1084 could be used as a shim in the meantime to remedy this error.
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 withthis.formatDateForCloudFront
since it's a class method - in your example theformatDateForCloudFront
function is not prefixed withthis
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:
AWS X-Ray console view:
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?
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.
@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.
⚠️ 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.