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

feat: Add support for POWERTOOLS_LOGGER_LOG_EVENT #1510

Merged

Conversation

AlexeySoshin
Copy link
Contributor

@AlexeySoshin AlexeySoshin commented Nov 10, 2023

Issue #, if available: #1444

Description of changes:

Adds support for POWERTOOLS_LOGGER_LOG_EVENT environment variable
The event will be logged only of this variable is set to true and @Logger(logEvent=true)

Checklist

Breaking change checklist

RFC issue #:

  • Migration process documented
  • Implement warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@AlexeySoshin AlexeySoshin marked this pull request as draft November 10, 2023 13:28
| **functionMemorySize** | String | "128" | |
| **functionArn** | String | "arn:aws:lambda:eu-west-1:012345678910:function:example-powertools-HelloWorldFunction-1P1Z6B39FLU73" | |
| **xray_trace_id** | String | "1-5759e988-bd862e3fe1be46a994272793" | X-Ray Trace ID when Lambda function has enabled Tracing |
| **function_request_id** | String | "899856cb-83d1-40d7-8611-9e78f15f32f4"" | AWS Request ID from lambda context |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry to say that but can we avoid this reformatting? will be a mess with v2...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No big deal


## Capturing context Lambda info

You can enrich your structured logs with key Lambda context information via `logEvent` annotation parameter.
You can also explicitly log any incoming event using `logEvent` param. Refer [Override default object mapper](#override-default-object-mapper)
to customise what is logged.

Note that the `logEvent` will work only if `POWERTOOLS_LOGGER_SAMPLE_RATE` environment variable is set to `"true"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it the python behaviour ?

I think it is either one or the other to have the event logged, not both

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should follow python. It's either one or the other, objective is to simplify job, users choose either programatic way or with env var, we don't want to complexify and require both. More over, if we require both, it becomes a breaking change and should go in v2.

docs/index.md Outdated
| **POWERTOOLS_LOG_LEVEL** | Sets logging level | [Logging](./core/logging) |
| **POWERTOOLS_TRACER_CAPTURE_RESPONSE** | Enables/Disables tracing mode to capture method response | [Tracing](./core/tracing) |
| **POWERTOOLS_TRACER_CAPTURE_ERROR** | Enables/Disables tracing mode to capture method error | [Tracing](./core/tracing) |
| Environment variable | Description | Utility |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, can you avoid reformat, just add the new env var

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do a proper formatting in v2


public final class EnvironmentVariables {
public static class LOGGER {
public final static String LOG_EVENT = "POWERTOOLS_LOGGER_LOG_EVENT";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have other files containing environment variables, do not introduce a new one please

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe that's something we should clean up in v2 by the way.

@@ -74,6 +76,12 @@ public final class LambdaLoggingAspect {
}

LEVEL_AT_INITIALISATION = LOG.getLevel();

if (System.getenv(EnvironmentVariables.LOGGER.LOG_EVENT) != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, remain consistent with the other environment variables line 67/68, we'll create a proper file in v2...

@@ -74,6 +76,12 @@ public final class LambdaLoggingAspect {
}

LEVEL_AT_INITIALISATION = LOG.getLevel();

if (System.getenv(EnvironmentVariables.LOGGER.LOG_EVENT) != null) {
LOG_EVENT = Boolean.parseBoolean(System.getenv(EnvironmentVariables.LOGGER.LOG_EVENT));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

twice getenv, can you use a variable?

if (logging.logEvent()) {
// Make sure that the environment variable was enabled explicitly
// And that the handler was annotated with @Logging(logEvent = true)
if (LOG_EVENT && logging.logEvent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (LOG_EVENT && logging.logEvent()) {
if (LOG_EVENT || logging.logEvent()) {

Copy link
Contributor

@jeromevdl jeromevdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this PR, a few things to fix

@jeromevdl jeromevdl changed the title Feat: Add support for POWERTOOLS_LOGGER_LOG_EVENT feat: Add support for POWERTOOLS_LOGGER_LOG_EVENT Nov 10, 2023
@@ -198,27 +182,24 @@ void shouldLogEventForHandler() throws IOException, JSONException {
assertEquals(expectEvent, event, false);
}

/**
* If POWERTOOLS_LOGGER_LOG_EVENT was set to false, the handler should log anything, despite @Logging(logEvent=true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, see my other comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python is the ref, not typescript

@AlexeySoshin AlexeySoshin marked this pull request as ready for review November 13, 2023 09:54
@@ -221,6 +221,8 @@ You can enrich your structured logs with key Lambda context information via `log
You can also explicitly log any incoming event using `logEvent` param. Refer [Override default object mapper](#override-default-object-mapper)
to customise what is logged.

If you want to enable this event logging for all handlers, set `POWERTOOLS_LOGGER_SAMPLE_RATE` environment variable to `"true"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we simplify this whole paragraph with the same content as python: https://docs.powertools.aws.dev/lambda/python/latest/core/logger/#logging-incoming-event


String logEvent = System.getenv("POWERTOOLS_LOGGER_LOG_EVENT");
if (logEvent != null) {
LOG_EVENT = Boolean.parseBoolean(logEvent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we try catch if the parseBoolean fails and log a warn, to avoid an exception ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could with some effort, but I think this is before logger is initialized, though

Copy link
Contributor

@jeromevdl jeromevdl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small changes and we should be good

docs/core/logging.md Outdated Show resolved Hide resolved
…ent' into support_powertools_logger_log_event

# Conflicts:
#	docs/core/logging.md
Copy link

sonarcloud bot commented Nov 14, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@jeromevdl jeromevdl merged commit de547d0 into aws-powertools:main Nov 14, 2023
10 of 17 checks passed
scottgerring added a commit that referenced this pull request Dec 5, 2023
* Setting up Kotlin environment. Converting test to Kotlin.

* Deploying via SAM successfully.

* Added Kotlin example.

* Removing unused Gradle build file.

* Adding SAM template so can be used as an existing project and Java target compatibility

* Adding SAM template so can be used as an existing project

* Updating guidance to use SAM for build and deploy

* Restructuring separate Java and Kotlin examples.

* Updating core examples readme to represent new structure for Java and Kotlin examples.

* Refactoring application code for efficiency, updating build to cover tests too and is more idiomatic and readme to be more descriptive

* Updating to fix trailing \n

* Updating guidance to be more specific for examples

* Adopting new mechanism for specifying jvm target.

* accommodating new project structure

* Fixing link typo after refactoring

* Setting up Kotlin environment. Converting test to Kotlin.

* Deploying via SAM successfully.

* Added Kotlin example.

* Removing unused Gradle build file.

* Adding SAM template so can be used as an existing project and Java target compatibility

* Adding SAM template so can be used as an existing project

* Updating guidance to use SAM for build and deploy

* Restructuring separate Java and Kotlin examples.

* Updating core examples readme to represent new structure for Java and Kotlin examples.

* Refactoring application code for efficiency, updating build to cover tests too and is more idiomatic and readme to be more descriptive

* Updating to fix trailing \n

* Updating guidance to be more specific for examples

* Adopting new mechanism for specifying jvm target.

* accommodating new project structure

* Fixing link typo after refactoring

* Flattening structure back to original to make merging easier for v2

* Adding build for Kotlin Gradle

* Adding build for Kotlin Gradle - Restructuring Java examples to v1 approach

* Correcting paths

* Adding SNAPSHOT support and local capability for Maven. Testing using Java 1.8

* Reviewed and updated against PR comments.

* Un-commenting examples

* Adding validation step for IaC SAM

* Adding Terraform for Java projects IaC validator and linter

* Adding additional projects for SAM validation and matrix approach

* Refactoring stream function to process input logging example with a Lambda Function URL instead of APIGW.

* Demonstrating Java streaming response

* Refactoring stream function to process input logging example to return

* Update CONTRIBUTING.md

* fix: get trace id from system property when env var is not set (#1503)

* fix: check if XRAY Trace ID is present in System property

* chore: remove erroneous extra char in tests

* fix #1500 (#1506)

* feat: Add support for POWERTOOLS_LOGGER_LOG_EVENT (#1510)

* chore: Addition of Warn Message If Invalid Annotation Key While Tracing #1511 (#1512)

* feat: ALC (#1514)

* handle AWS_LAMBDA_LOG configuration

* ALC documentation + code review

* update doc

* chore:Prep release 1.18.0 (#1515)

* chore:prep release 1.18.0

* update version

* update version in kotlin example

* maven local repo in gradle example

* update changelog

---------

Co-authored-by: scottgerring <scottgerring@users.noreply.github.com>

* chore: update version to next snapshot: 1-19.0-SNAPSHOT (#1516)

* update version to next snapshot: 1-19.0-SNAPSHOT

* update version to next snapshot: 1-19.0-SNAPSHOT

* update version to next snapshot: 1-19.0-SNAPSHOT

* building only for LTS

* Add some more margin to the test pause (#1518)

* test: e2e tests with java 21 (#1517)

* e2e tests with java 21

* Run Java21 tests using the Java17 compiler

* Run all of the E2E tests in parallel, not just the first 3

* Try again

* .

* Let's try again

* Add some comment on Java21 to the repo

* Add caveat about lambda runtimes

* Clean up wording a little

---------

Co-authored-by: Scott Gerring <gerrings@amazon.com>

* update doc for ALC (#1520)

* chore: Testing java21 aspectj pre-release (#1519)

* e2e tests with java 21

* use aspectj 1.9.21-SNAPSHOT

* Fix log4j2.xml missing in logging test for java21

* rollback double runtime

* remove comment

* keep aspectj 1.9.7 in parent for java8 compatibility

* use M1 instead of snapshot

* update documentation for aspectj

* update documentation for aspectj

---------

Co-authored-by: Jerome Van Der Linden <jeromevdl@gmail.com>

* chore: Remove build cruft

* Adding context for using RequestStreamHandler

* removing pr_lint

* Update examples/powertools-examples-core/sam/src/main/java/helloworld/AppStream.java

Clarify usage of RequestStreamHandler.

Co-authored-by: Alexey Soshin <alexey.soshin@gmail.com>

---------

Co-authored-by: Jason Harris <harrzjas@amazon.com>
Co-authored-by: Scott Gerring <scottgerring@users.noreply.github.com>
Co-authored-by: Jason Harris <harrzjas@amazon.co.uk>
Co-authored-by: Jérôme Van Der Linden <117538+jeromevdl@users.noreply.github.com>
Co-authored-by: Michele Ricciardi <mriccia@amazon.com>
Co-authored-by: Alexey Soshin <alexey.soshin@gmail.com>
Co-authored-by: jdoherty <jdoherty07@gmail.com>
Co-authored-by: Scott Gerring <gerrings@amazon.com>
Co-authored-by: Jerome Van Der Linden <jeromevdl@gmail.com>
scottgerring added a commit that referenced this pull request Feb 22, 2024
* docs: HelloWorldStreamFunction in examples fails with sam (#1532)

* Setting up Kotlin environment. Converting test to Kotlin.

* Deploying via SAM successfully.

* Added Kotlin example.

* Removing unused Gradle build file.

* Adding SAM template so can be used as an existing project and Java target compatibility

* Adding SAM template so can be used as an existing project

* Updating guidance to use SAM for build and deploy

* Restructuring separate Java and Kotlin examples.

* Updating core examples readme to represent new structure for Java and Kotlin examples.

* Refactoring application code for efficiency, updating build to cover tests too and is more idiomatic and readme to be more descriptive

* Updating to fix trailing \n

* Updating guidance to be more specific for examples

* Adopting new mechanism for specifying jvm target.

* accommodating new project structure

* Fixing link typo after refactoring

* Setting up Kotlin environment. Converting test to Kotlin.

* Deploying via SAM successfully.

* Added Kotlin example.

* Removing unused Gradle build file.

* Adding SAM template so can be used as an existing project and Java target compatibility

* Adding SAM template so can be used as an existing project

* Updating guidance to use SAM for build and deploy

* Restructuring separate Java and Kotlin examples.

* Updating core examples readme to represent new structure for Java and Kotlin examples.

* Refactoring application code for efficiency, updating build to cover tests too and is more idiomatic and readme to be more descriptive

* Updating to fix trailing \n

* Updating guidance to be more specific for examples

* Adopting new mechanism for specifying jvm target.

* accommodating new project structure

* Fixing link typo after refactoring

* Flattening structure back to original to make merging easier for v2

* Adding build for Kotlin Gradle

* Adding build for Kotlin Gradle - Restructuring Java examples to v1 approach

* Correcting paths

* Adding SNAPSHOT support and local capability for Maven. Testing using Java 1.8

* Reviewed and updated against PR comments.

* Un-commenting examples

* Adding validation step for IaC SAM

* Adding Terraform for Java projects IaC validator and linter

* Adding additional projects for SAM validation and matrix approach

* Refactoring stream function to process input logging example with a Lambda Function URL instead of APIGW.

* Demonstrating Java streaming response

* Refactoring stream function to process input logging example to return

* Update CONTRIBUTING.md

* fix: get trace id from system property when env var is not set (#1503)

* fix: check if XRAY Trace ID is present in System property

* chore: remove erroneous extra char in tests

* fix #1500 (#1506)

* feat: Add support for POWERTOOLS_LOGGER_LOG_EVENT (#1510)

* chore: Addition of Warn Message If Invalid Annotation Key While Tracing #1511 (#1512)

* feat: ALC (#1514)

* handle AWS_LAMBDA_LOG configuration

* ALC documentation + code review

* update doc

* chore:Prep release 1.18.0 (#1515)

* chore:prep release 1.18.0

* update version

* update version in kotlin example

* maven local repo in gradle example

* update changelog

---------

Co-authored-by: scottgerring <scottgerring@users.noreply.github.com>

* chore: update version to next snapshot: 1-19.0-SNAPSHOT (#1516)

* update version to next snapshot: 1-19.0-SNAPSHOT

* update version to next snapshot: 1-19.0-SNAPSHOT

* update version to next snapshot: 1-19.0-SNAPSHOT

* building only for LTS

* Add some more margin to the test pause (#1518)

* test: e2e tests with java 21 (#1517)

* e2e tests with java 21

* Run Java21 tests using the Java17 compiler

* Run all of the E2E tests in parallel, not just the first 3

* Try again

* .

* Let's try again

* Add some comment on Java21 to the repo

* Add caveat about lambda runtimes

* Clean up wording a little

---------

Co-authored-by: Scott Gerring <gerrings@amazon.com>

* update doc for ALC (#1520)

* chore: Testing java21 aspectj pre-release (#1519)

* e2e tests with java 21

* use aspectj 1.9.21-SNAPSHOT

* Fix log4j2.xml missing in logging test for java21

* rollback double runtime

* remove comment

* keep aspectj 1.9.7 in parent for java8 compatibility

* use M1 instead of snapshot

* update documentation for aspectj

* update documentation for aspectj

---------

Co-authored-by: Jerome Van Der Linden <jeromevdl@gmail.com>

* chore: Remove build cruft

* Adding context for using RequestStreamHandler

* removing pr_lint

* Update examples/powertools-examples-core/sam/src/main/java/helloworld/AppStream.java

Clarify usage of RequestStreamHandler.

Co-authored-by: Alexey Soshin <alexey.soshin@gmail.com>

---------

Co-authored-by: Jason Harris <harrzjas@amazon.com>
Co-authored-by: Scott Gerring <scottgerring@users.noreply.github.com>
Co-authored-by: Jason Harris <harrzjas@amazon.co.uk>
Co-authored-by: Jérôme Van Der Linden <117538+jeromevdl@users.noreply.github.com>
Co-authored-by: Michele Ricciardi <mriccia@amazon.com>
Co-authored-by: Alexey Soshin <alexey.soshin@gmail.com>
Co-authored-by: jdoherty <jdoherty07@gmail.com>
Co-authored-by: Scott Gerring <gerrings@amazon.com>
Co-authored-by: Jerome Van Der Linden <jeromevdl@gmail.com>

* deps: bump aspectj to 1.9.21 for jdk21 (#1536)

* chore: SAM and Terraform IaC extracted from pr_build and simplified approach. (#1533)

* SAM and Terraform IaC extracted from pr_build and simplified approach.

* Update .github/workflows/pr_iac_lint.yml

Co-authored-by: Scott Gerring <scottgerring@users.noreply.github.com>

---------

Co-authored-by: Jason Harris <harrzjas@amazon.co.uk>
Co-authored-by: Scott Gerring <scottgerring@users.noreply.github.com>

* chore: Remove empty CDK test (#1542)

* Remove CDK test

* Build core utilities so that sam validator can find them

* Remove CDK test

* Ok just build it all

* Remove the plan ...

* Remove terraform plan from iac lint

---------

Co-authored-by: Jérôme Van Der Linden <117538+jeromevdl@users.noreply.github.com>

* dependabot on v2 branch (#1548)

* chore: remove unecessary creds acquisition (#1572)

* Merge from main

---------

Co-authored-by: Jason Harris <jiharris90@gmail.com>
Co-authored-by: Jason Harris <harrzjas@amazon.com>
Co-authored-by: Jason Harris <harrzjas@amazon.co.uk>
Co-authored-by: Jérôme Van Der Linden <117538+jeromevdl@users.noreply.github.com>
Co-authored-by: Michele Ricciardi <mriccia@amazon.com>
Co-authored-by: Alexey Soshin <alexey.soshin@gmail.com>
Co-authored-by: jdoherty <jdoherty07@gmail.com>
Co-authored-by: Jerome Van Der Linden <jeromevdl@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants