Skip to content
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

Feature request: convenience reset_state method in Logger #4134

Closed
1 of 2 tasks
dreamorosi opened this issue Apr 15, 2024 · 7 comments · Fixed by #5956
Closed
1 of 2 tasks

Feature request: convenience reset_state method in Logger #4134

dreamorosi opened this issue Apr 15, 2024 · 7 comments · Fixed by #5956
Assignees
Labels
feature-request feature request help wanted Could use a second pair of eyes/hands logger

Comments

@dreamorosi
Copy link
Contributor

Use case

Note

This is a cross-language feature request that was originally brought up under aws-powertools/powertools-lambda-typescript#2337

As a customer who uses canonical logging - aka wide logging - I would like to be able to clear any key I might have appended to the Logger instance regardless of me using the default formatter or the decorator - both of which either expose a method along these lines or can be configured to automatically clear state.

Solution/User Experience

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger()

def lambda_handler(event: dict, context: LambdaContext) -> str:
    try:
        logger.append_keys(order_id=event.get('order_id', None)
    finally:
        logger.info('CANONICAL')
        logger.clear_state() 

Alternative solutions

N/A

Acknowledgment

@rubenfonseca
Copy link
Contributor

Awesome, thank you for opening this @dreamorosi ! We'll work on this tomorrow

@rubenfonseca rubenfonseca removed the triage Pending triage from maintainers label Apr 15, 2024
@rubenfonseca rubenfonseca moved this from Triage to Working on it in Powertools for AWS Lambda (Python) Apr 15, 2024
@rubenfonseca rubenfonseca self-assigned this Apr 15, 2024
@heitorlessa
Copy link
Contributor

Meanwhile we don't submit a PR for a top level method, this is possible today like this:

logger.registered_formatter.clear_state()

The top level method should delegate to formatter

@leandrodamascena
Copy link
Contributor

Hello @heitorlessa and @dreamorosi! I noticed in the issue aws-powertools/powertools-lambda-typescript#2337 that you two discussed how to resolve a "conceptual" bug with our current method names (persistent vs non-persistent) and their behavior. I see that some things are marked as deprecated to be removed in the next major release. As we're about to release Powertools for AWS Lambda (Python) v3, we have an opportunity to review this here.

My idea is:

1 - Create a new method and call it add_persistent_keys so that those keys will persist even the clear_state() method is invoked. This method will allow developers to explicitly define the keys that should remain until the Logger instance is destroyed.

2 - Regarding the current append_keys method, I don't think we need to change its functionality. However, we should improve the documentation and potentially add a workflow example to clarify that this method behaves like a "temporary keys" mechanism. The keys added through append_keys will be removed when clear_state() is called, as it is currently designed.

I might be misunderstanding something regarding the TypeScript issue, and please correct me if this is the case. If not, and you agree with my proposal, I think we can proceed with implementing this change for Powertools for AWS Lambda (Python) v3.

@leandrodamascena leandrodamascena moved this from Working on it to Ideas in Powertools for AWS Lambda (Python) May 29, 2024
@dreamorosi
Copy link
Contributor Author

Hey Leo, I'm unsure on how to move forward with that feature and even more unsure whether to recommend that you skip it entirely in Python or not.

The clear_state method is fine, but the persistent/temporary distinction is a bit messy due to how it was naively implemented in our repo.

With Heitor we already hashed out the new API and wording (my second to last comment in the PR), but now there are some questions on how to actually implement it as fixing the implementation requires a different merging strategy for the attributes. I plan on fixing this later next week, or the week after once I'm done with the Madrid Summit.

It's important to highlight here that this distinction between persistent and temporary attributes was added to TS as a deviation by the original implementer and not in response to customer demand, at least afaik.

While there are valid use cases for this type of feature, I don't know that we have evidence of customers actually needing/using it, so the decision to add it to Python is up to you.

@leandrodamascena
Copy link
Contributor

Hello @dreamorosi! Thank you for your always valuable insights! I'm going to leave this here on hold until you make progress on the TypeScript PR and see if we can adopt the same in Python. If possible, it's good because we can start to reduce the differences in language implementations, if not I'll move forward and implement only the clear_state method.

@heitorlessa heitorlessa changed the title Feature request: convenience clear_state method in Logger Feature request: convenience reset_state method in Logger Jun 9, 2024
@heitorlessa heitorlessa added the help wanted Could use a second pair of eyes/hands label Jun 9, 2024
@heitorlessa heitorlessa moved this from Ideas to Backlog in Powertools for AWS Lambda (Python) Jun 10, 2024
@anafalcao anafalcao self-assigned this Jan 24, 2025
@anafalcao anafalcao moved this from Backlog to Working on it in Powertools for AWS Lambda (Python) Jan 30, 2025
@anafalcao anafalcao linked a pull request Jan 30, 2025 that will close this issue
7 tasks
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (Python) Jan 31, 2025
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

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.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Jan 31, 2025
Copy link
Contributor

This is now released under 3.6.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Feb 11, 2025
@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request help wanted Could use a second pair of eyes/hands logger
Projects
Status: Shipped
Development

Successfully merging a pull request may close this issue.

5 participants