-
Notifications
You must be signed in to change notification settings - Fork 154
feat(metrics): allow setting functionName via constructor parameter and environment variable #3696
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
feat(metrics): allow setting functionName via constructor parameter and environment variable #3696
Conversation
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
@dreamorosi it looks like one of the tests failed with a network error. Would you mind rerunning it? |
I'm starting the review now. |
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 ignore my previous review comment.
After looking at the implementation I think we should go back to the drawing board and avoid overcomplicating things.
The function name here refers to the Lambda function resource, not the decorated/wrapped function, so there's no reason for adding a property in every place like we are doing.
I know the discussion in the linked issue went into that direction, but I still stand by my my first proposal in this comment.
With the current implementation, we're introducing some unnecessary complexity, for example what happens if we do this?
const metrics = new Metrics({});
metrics.setFunctionName('foo');
export const handler = middy().use(logMetrics(metrics, { functionName: 'bar' )).handler(async () => {})
Right now bar
would silently take precedence. We could add a warning signaling that this is happening, but I think the entire issue would be bypassed by just doing this:
const metrics = new Metrics({
functionName: 'foo'
});
and potentially deprecating the setFunctionName()
method.
Apologies for the confusion and any rework caused, but I think this is the best path forward for customers.
I think the idea with the middy and decorator option is to have one I agree that we need strive for simplicity, and having ENV variable to control this parameter is enough, since it is 1:1 mapping between resource name and environment variable. This means we keep three additions, except the middy/decorator option?
We can still deprecate |
How does that work? This dimension is specifically for the With that said, if you really want to do this, you can use the const metrics = new Metrics({
functionName: process.env.FOO
});
// or
metrics.functionName(process.env.FOO); |
Hi @steven10172, just wanted to check in to see if you were still interested in completing the PR. If not, or no longer available (it's fine, things change!), let us know so we can take over the implementation. |
I intend to still work on this. I have no objections to your recommendations and example code. I was not intending to deprecate the other method, but with your buy-in I'm all for it. As I was working through my implementation I recognized there was a non ideal pattern for the non decorator/middy path, thanks for the suggestion of updating the coldstart method. |
…tartMetric to support accepting context
Please retry analysis of this Pull-Request directly on SonarQube Cloud |
Thanks for updating the PR, I gave it a quick look and I don't see any major issue. I'll pull the branch and make a final review tomorrow morning. |
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.
Thank you for working on this and addressing all our comments throughout the past couple weeks.
I have made a couple small commits to align the PR with the suggested branch, but the logic stays the same.
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.
Hey @steven10172 and @dreamorosi! Great job you guys did here! I just left 3 comments that at least are worth checking out, so I'm going to request changes.
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
Summary
This PR ensures that a manually set
functionName
is not overwritten by middy and class decorators. IffunctionName
is not already defined a default value ofcontext.functionName
will be used with middy and class decorators.Changes
hasFunctionName()
to metrics class to support checking if thefunctionName
is already definedfunctionName
to metric constructor to support setting thefunctionName
used for cold startsPOWERTOOLS_METRICS_FUNCTION_NAME
environment variable to support setting thefunctionName
via IaCsetFunctionName
with defaultcontext.functionName
in middy and decorator iffunctionName
is already definedThis PR supports setting a custom
functionName
to be used with cold starts, that is more stable ascontext.functionName
will change often due to the lambda being replaced when the infrastructure is updated.Issue number: #3642
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.