Repository navigation
Description
Is this related to an existing feature request or issue?
- Feature request: Simplified logger formatter when in AWS SAM Local powertools-lambda-python#1527
- Feature request: pretty printing logs in local development powertools-lambda-typescript#751
Which AWS Lambda Powertools utility does this relate to?
Logger, Event Handler, and potentially others
Summary
This RFC outlines the introduction of a new environment variable POWERTOOLS_DEV to ease local prototyping, debugging, and why not per utility env var.
Use case
Powertools optimizes certain outputs for costs (CloudWatch Logs) and to facilitate native integrations (CloudWatch Embedded Metric Format, Batch processing, etc.). It also disables certain behaviours such as tracing when running outside Lambda, so adding Powertools doesn't get in the way.
As our customer base increases, it diversifies its usage and thus lies the opportunity to adapt and improve. For example, customers are running their Lambda function locally in various forms: AWS Chalice, AWS SAM CLI, AWS Amplify CLI, Serverless framework, etc.
This increases our complexity as we try to keep track of potential sources of local emulation and their many environment variables. Notwithstanding customers who may also run Lambda Powertools in non-Lambda environments such as Glue jobs, Fargate, whether we provide support or not.
Proposal
Introduce POWERTOOLS_DEV boolean environment variable for the following effect:
| Utility | Effect |
|---|---|
| Logger | Increase JSON indentation to 4. This will ease local debugging when running functions locally under emulators or direct calls while not affecting unit tests |
| Event Handler | Enable full traceback errors in the response, indented request/responses, and CORS in dev mode (*). This will deprecate POWERTOOLS_EVENT_HANDLER_DEBUG that achieves the same effect in Event Handler Debug Mode |
| Tracer | Disables tracing operations. Future-proof non-Lambda environments. As of now, we'll continue with auto-disable feature until we decide when (if) we should move beyond Lambda as a compute environment |
This change future-proof additional convenience for customers and lower cognitive load on needing multiple env vars to the same end. We will create a new section in the documentation to keep track of behaviours this env var will enable.
Warning increased cost in log ingestion
We anticipate customers wanting to use POWERTOOLS_DEV in non-production AWS accounts. This might also occur unwillingly. Regardless of the intention, we should log a warning message to signal this feature is in use. This should help prevent an increase in log ingestion cost in production.
Out of scope
Change Tracer auto-disable behaviour in non-Lambda environments. This will cause a breaking change in customers unit tests, which is a great convenient Powertools feature.
We can revisit it when (if) we officially support compute beyond Lambda.
Potential challenges
An open question we have is: What if a customer intentionally deploys with POWERTOOLS_DEV=true? See Warning increased cost in log ingestion
Should we allow it or safely ignore it (e.g., POWERTOOLS_DEV && not TASK_ROOT)? Tracer only
Dependencies and Integrations
None
Alternative solutions
Introduce an environment variable per utility when the need arises, for example POWERTOOLS_LOGGER_INDENTED_LOGS, POWERTOOLS_EVENT_HANDLER_INDENTED_LOGS.
Acknowledgment
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Java, TypeScript, and .NET
Activity
benbridts commentedon Sep 28, 2022
Will this be expected to not be used when running (in a sandbox/dev) account on Lambda?
I might want to convenience of enabling all dev-functionality at once (logging the full event might also be one?), without it impacting how I see things in CloudWatch.
POWERTOOLS_LOGGER_INDENTED_LOGSs a good example of things that I want different locally or when using CloudWatchheitorlessa commentedon Sep 28, 2022
Exactly, we won't impact the final runtime. I'll make that clearer in the RFC body.
That's exactly the main argument to use
POWERTOOLS_DEVlocally instead of what was suggested in the original issue to change from JSON to non-JSON --POWERTOOLS_DEVwill only increase indentation so you can see it better locally but not change at Lambda runtime otherwise it'd increase costs.heitorlessa commentedon Sep 28, 2022
Updated the body of the RFC, and also posed a question if a customer accidentally deploys a function with
POWERTOOLS_DEV=trueinstead of only using it locally -- lemme know if it's clearer.Changes
benbridts commentedon Sep 28, 2022
I think
POWERTOOLS_LOCALwould be a better name in that case.I was thinking about it the other way. If this enables (in the future) things like more verbose logging, or logging of more things (inputs / outputs / ...), it might be nice to have it available in a Lambda Function too (if that function is in a developer's account and not getting a lot of traffic).
EDIT: or to put it more clearly: I assumed
POWERTOOLS_DEVcould also be used to toggle DEBUG-like functionality. I understand now that this is not the caseheitorlessa commentedon Sep 28, 2022
POWERTOOLS_DEVcould also be used for debugging if you intentionally deploy a function with it. We actually had issues with that in the past, where people would forget it and have a much higher bill in CloudWatch Logs, plus sensitive info being logged (or CORS in dev mode).I do see where you're coming from tho - I made the wording crispier and brought in as a what if question before we settle on this.
In hindsight, I guess we can't protect customers 100% from forgetting that parameter, and putting a guard would prevent you from utilizing in non-prod env. I initially thought it'd be more useful in local executions, but I can see cases where you'd want that deployed when multiple functions are involved.
On the naming itself, the reason we didn't propose
LOCALwas to not give the impression Powertools would provide a local execution mode of sorts (ala SAM CLI).benbridts commentedon Sep 28, 2022
If this will always switch indentation, I think it's fine to prevent me from using it (but maybe it should print an INFO one-liner to the logs). Setting POWERTOOLS_DEV to True and toggling indentation to be off, is the same effort as toggling individual debug settings.
Naming is always difficult :).
_DEVgave me the impression that it was similar toDEBUGin tools like Django.heitorlessa commentedon Sep 28, 2022
Yeah, hmmm. Future wise, I can't see how we'd detect you're running on Fargate/Glue/Docker and auto-toggle that off at runtime.
What if we simply log a warning (
warningsin Python) that you enabledPOWERTOOLS_DEVso to instruct this might not be a good idea in prod?That could solve our side on trying too hard to auto-toggle off this feature in scenarios we can't always predict, and keeping customers informed about the outcome when enabled.
heitorlessa commentedon Sep 28, 2022
@dreamorosi @saragerion @rubenfonseca @leandrodamascena what do you think of
POWERTOOLS_LOCALname?In hindsight, whatever name we settle, we could enable another one
POWERTOOLS_DEBUG=trueto automatically set logging asDEBUGfor our internals - For Python, we ask customers to manually enable raw debugging similar to boto.heitorlessa commentedon Sep 28, 2022
From @jfuss
My only concern with an enum is that it opens the room for typos given this is an env var. We could technically have a default in the Enum (
MODE.DEV)benbridts commentedon Sep 28, 2022
Would you log it everywhere, or only in lambda?
In the second case, I think the indentation would show it's not a great idea anyway.
In the first case, there is a balance between annoying and helpful, but warnings about security and/or cost seem to be on the helpful side (maybe it should log outside the handler, so that environments that keep the function warm don't log on every invocation)
heitorlessa commentedon Sep 28, 2022
The former. Like we do in Metrics today - we will log a warning (anywhere) if you try to flush metrics without a metric per se - it might be intentional, but we won't raise an Exception unless you tell us to (
raise_on_empty_metrics)benbridts commentedon Sep 28, 2022
👍🏻
If used as intended, that failure should be spotted fast ("I enabled better logging, and I'm not getting better logging"), adding a warning "DECV is not a valid value for POWERTOOLS_MODE" will also point in the right direction
However, I think as long as the intent is clear, a boolean is fine too. A ℹ️ in the docs saying that it will not be very useful when running with CloudWatch might be nice though.
heitorlessa commentedon Sep 28, 2022
Hypothetically, say we have
POWERTOOLS_MODEwith three modes:DEV,PROD,DEBUG, wherePRODis the default. Consider docs also outlines intent clearly.If you use
XPTOas a value, we log a warning with valid values, and fallback toPRODfor safety - does that sound clear enough?2 remaining items
dreamorosi commentedon Sep 29, 2022
Thanks for creating the RFC.
In my opinion the goal of this environment variable should be bringing better developer experience. This should also be reflected in the docs / messaging around it, but also in the name:
POWERTOOLS_DEBUGsounds overly generic and as already stated by others, it might cause confusion around where to use it / what it doesPOWERTOOLS_LOCALis a good approximation. It conveys that it should not be used on a live environment and I wouldn't worry too much about confusing it with SAM Local. People who know what SAM Local is will most probably realise that this is just an environment variable.POWERTOOLS_LOCAL_DEV<- however, if we want to hammer the concept that this is a local & development only flag, then we could spell it out fully. This would merge both name initially proposed (POWERTOOLS_DEV) and the one that was added later.My preference (in order):
POWERTOOLS_LOCAL_DEVPOWERTOOLS_LOCALFor the purpose of this RFC, I would also leave out the discussion about
POWERTOOLS_DEBUG. It's an interesting discussion but it also has a wider scope (many utilities potentially involved) and at the moment it's not relevant to all runtimes (i.e. Powertools for TypeScript doesn't log anything that is not an error outside of logger).About the enum/
PowertoolsModethat started out from this message, I think I'm missing some context as I'm not sure where this is coming from or why it's preferred to the simpler env var.At this stage I'd be inclined to keep things simple and stick to
POWERTOOLS_LOCAL_DEV(or similar) as an environment variable that modifies some of the behaviors in Powertools. Adding additional complexity in both the logicAbout the open question(s):
At least at this stage, when it comes to logging, CloudWatch already indents JSON structured logs when you expand an entry in a Log group. So it's safe to say that the setting should not be used on AWS Lambda and a warning could be enough.
When it comes to extra data, while I see some value in emitting a warning, I'm also not sure how the environment variable used in local dev or unit tests could end up in the env vars configuration on Lambda. Both manual deployment (via Console) or IaC require a conscious effort to set env variables.
I'm really on the fence on this topic. Adding a warning seems sensible and relatively low effort so I'm not against it.
rubenfonseca commentedon Sep 29, 2022
Thank you for this great discussoin.
While I understand the motivation for the change, I'm worried about
POWERTOOLS_MODEas an Enum: it lowers the barrier too much for creating all kinds of strange "modes" in the futue.I think there is room for having both environment variables:
POWERTOOLS_LOCAL/POWERTOOLS_LOCAL_DEVandPOWERTOOLS_DEBUGwhich have two separate meanings:LOCAL: I want to enable features that will make it easier for me to develop locally, like easier to read logs, etc.DEBUG: I want to enable additional logging because I'm debugging some functionality. This is true whenever I run locally or in the cloud.So unless I'm missing something, I would love to separate the two variables that control separate behaviour.
Yes, I can have
DEBUGon andLOCALfalse, because I'm debugging something on the cloud. Yes, I can haveLOCALon andDEBUGoff because I just want to read the logs output more clearly. And of course I can enable both to have the best of both worlds.heitorlessa commentedon Sep 29, 2022
Thank you @rubenfonseca @dreamorosi - that's super helpful to hear your input.
After re-reading the entire RFC, I'm inclined to move forward with
POWERTOOLS_DEVandPOWERTOOLS_DEBUGboolean flags. Having both helps make the intent more explicit. If a customer accidentally leaves them turned when deployed, a warning should suffice as an actionable advice on what to do.If either @rubenfonseca @dreamorosi @leandrodamascena @saragerion @msailes disagrees, please shout out until tomorrow. I'll create a separate PR in the Python repo to document when to use which and their effects as per RFC.
Thank you also to @benbridts @jfuss
saragerion commentedon Sep 29, 2022
@heitorlessa can we recap shortly in bullet points the difference between the 2 and the impact on each utility?
heitorlessa commentedon Sep 29, 2022
Absolutely. I'll update the RFC body tomorrow too:
POWERTOOLS_DEV
Provides additional information when prototyping an application.
Effect on utilities:
*). This will deprecatePOWERTOOLS_EVENT_HANDLER_DEBUGthat achieves the same effect in Event Handler Debug Mode.POWERTOOLS_DEBUG
Configures Lambda Powertools internal logging for debug logging for all operations.
Current Python implementation following AWS Python SDK standard: https://awslabs.github.io/aws-lambda-powertools-python/latest/#debug-mode
heitorlessa commentedon Oct 3, 2022
RFC body updated to reflect the direction we agreed. @pmarko1711 was kind enough to contribute the first implementation for
Loggerin Powertools for Python: aws-powertools/powertools-lambda-python#1548Before I close as successful, does anyone know of any ready-to-use automation to convert a RFC issue into a PR as markdown?
Storing these in this repo will ease discovery of cross-languages specifics.
dreamorosi commentedon Oct 19, 2022
@heitorlessa, I saw that Python has already merged the changes related to
POWERTOOLS_DEV& Logger, however I'm not clear from the RFC body on whether we should also do these changes to Tracer.Should
POWERTOOLS_DEV=${truthy_value)disable the Tracer? I'd say yes as I consider this an explicit disable, but wanted to make sure it's in line with the RFC before moving forward.heitorlessa commentedon Oct 19, 2022
dreamorosi commentedon Oct 19, 2022
I'm not sure I'm following. We both agree that Tracer existing behavior shouldn't change. This includes explicitly disabling it via env variable
POWERTOOLS_TRACE_DISABLEDor other direct settings. It also includes the current heuristics to disable automatically when outside of Lambda.My comment was referred to: should
POWERTOOLS_DEVbe an additional way to disable tracing? As in, on top of the existing behavior, when a customer setsPOWERTOOLS_DEV=1Tracer is disabled.From the doc section you linked the answer seems to be yes. But then, maybe I'm missing how it would break existing customers since it's a new setting that doesn't change/modifies other existing ones.
I'm sorry if this discussion is a bit redundant but I'm not 100% clear and I prefer to clarify
chrisandrewcl commentedon Nov 7, 2022
Isn't this config akin to tying behavior to NODE_ENV (which I think most people agree is not a good idea)? E.g.: if I want pretty-print on my logs I will have to accept other behaviors that I may not want (it's three or four now, but if not careful it tends to grow over time).
I am not against the convenience of having an easier configuration, but please don't take away granular control.
Also, there may be a third alternative: built-in config presets (e.g.:
CONFIG_PRESET=dev), which also gives space for community presets.leandrodamascena commentedon Aug 2, 2023
Hey @heitorlessa and @dreamorosi! Do we still need to keep this issue open or can we close it as complete?
dreamorosi commentedon Aug 3, 2023
Hey Leo, not sure about Python but in TS we only implemented this for Logger, the proposal also mentioned Event Handler (which we don't have yet) and Tracer.
Depending on the status, and whether we still want to move forward with the proposal you can close it or leave it, I think.
heitorlessa commentedon Jul 26, 2024
launched it