-
Notifications
You must be signed in to change notification settings - Fork 153
Feature request: add access method to coldStart
field in the Utility
class
#3668
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
I am going forward and set the |
The current design was built on the assumption that each utility instance (i.e. Logger) would access/visit that value only once per initialization. With that in mind, it made sense to make it this way. I know this request comes related to the work in #3278, which now requires us to access the cold start value twice, so it makes sense to refactor. Two non-invasive options would be:
I think both are not great and kind of defeat the purpose of having that Despite this initial push however, the core utilities have evolved to have their own independent logic over time and we barely ever touched that To move forward, I think we have two options, assuming we don't want to pursue any of the suboptimal ones above: Option 1 Remove this The two methods
Option 2 We keep the Specifically, we could implement new logic that actually keeps track of whether a certain invocation is a cold start or not based on the environment rather than on an internal state alone. Right now, if for some reason you have two independent Logger instances, or are using Powertools in a compute environment that supports multiple requests at once, the cold start logic is completely broken since it relies on the order of access for the property for each instance of the class. We could instead make the cold start scoped to a specific request and do something like this: coldStartMap = new Map<string, boolean>();
public isColdStart() {
const requestId = process.env._X_AMZN_REQUEST_ID;
if (!requestId) {
return true; // this is during init, so we it's THE cold start
}
if (this.coldStartMap.size === 0) {
this.coldStartMap.set(requestId, true); // this is the first request being seen
return true;
}
this.coldStartMap.set(requestId, false); // these are all the others
return false;
} This way we rely on the presence and value of This logic would have to exist outside of the Logger instance and its parents, so in practice we'd have to create some shared context. What do we think? Btw I am okay also with making a quick change and make it |
I like your idea to broaden the scope of Utility and make it more useful. I will change the coldStart field to protected to unblock the other issue, and will address this issue in a separate PR. |
On second thought, maybe we can just simplify it like this for now: export class Utility {
protected coldStart = true;
protected readonly defaultServiceName: string = 'service_undefined';
/**
* Get the cold start status of the current execution environment.
*
* @example
* ```typescript
* import { Utility } from '@aws-lambda-powertools/commons';
*
* const utility = new Utility();
* utility.isColdStart(); // true
* utility.isColdStart(); // false
* ```
*
* The method also flips the cold start status to `false` after the first invocation.
*/
public getColdStart(): boolean {
if (this.coldStart) {
this.coldStart = false;
return true;
}
return false;
}
/**
* Validate that the service name provided is valid.
* Used internally during initialization.
*
* @param serviceName Service name to validate
*/
protected isValidServiceName(serviceName?: string): boolean {
return typeof serviceName === 'string' && serviceName.trim().length > 0;
}
} and focus on something more impactful. I think this is something we might want to revisit in the future still, but I would not keep it on the backlog. |
This issue is now closed. Please be mindful that future comments 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. |
This is now released under v2.16.0 version! |
Use case
I'd like to have a method that will return the current value of the
coldStart
field of theUtility
class without changing the value. Right now bothisColdStart
andgetColdStart
changes the value when called. As mentioned in #3278 we can't access the flag without changing it.Solution/User Experience
I propose to change
isColdStart
to return thecoldStart
value without changing it which would also reflect its meaning.Alternative solutions
Acknowledgment
Future readers
Please react with 👍 and your use case to help us understand customer demand.
The text was updated successfully, but these errors were encountered: