Skip to content

feat: add automatic TenantId logging to Logger default properties#2471

Open
Pratapchandradeo wants to merge 4 commits intoaws-powertools:mainfrom
Pratapchandradeo:feat/add-tenant-id-logging
Open

feat: add automatic TenantId logging to Logger default properties#2471
Pratapchandradeo wants to merge 4 commits intoaws-powertools:mainfrom
Pratapchandradeo:feat/add-tenant-id-logging

Conversation

@Pratapchandradeo
Copy link
Copy Markdown

@Pratapchandradeo Pratapchandradeo commented Apr 15, 2026

Description

Implements automatic tenant_id logging for AWS Lambda Tenant Isolation feature with null-safe serialization in Logback JSON encoder. Ensures tenant_id is only logged when it contains a valid value.

Changes

✅ Add null-safety check to LambdaJsonEncoder.serializePowertools()
✅ Add TENANT_ID_RESOLVER in Log4j2 PowertoolsResolver
✅ Add TENANT_ID enum to PowertoolsLoggedFields
✅ Update LambdaJsonLayout.json and LambdaEcsLayout.json
✅ Add tenant_id serialization to LambdaEcsEncoder
✅ Add unit tests for null and empty tenant_id scenarios
✅ Fix test assertion and OS compatibility issues

Implementation Details

  • Skip tenant_id serialization when null or empty
  • Follows pattern used for sampling_rate
  • Supports both Log4j2 and Logback backends
  • No user code changes required

Testing

Tests run: 18, Failures: 0, Errors: 0, BUILD SUCCESS

Test coverage:

  • Valid tenant_id is logged
  • Null tenant_id is NOT logged
  • Empty tenant_id is NOT logged
  • Respects includePowertoolsInfo flag

Related Issues

Closes #2348

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

  • I confirm that I can use, modify, copy, and redistribute this contribution under the terms of your choice

Acknowledgment

- Add TENANT_ID to PowertoolsLoggedFields enum with null-safety checks
- Create TENANT_ID_RESOLVER in Log4j2 PowertoolsResolver with isResolvable check
- Register TENANT_ID resolver in eventResolverMap for Log4j2 support
- Add TENANT_ID_ATTR_NAME constant to LambdaEcsEncoder (ECS format: tenant.id)
- Add conditional null-safe serialization of tenant_id in Logback ECS encoder
- Update LambdaJsonLayout.json to include tenant_id field for Log4j2 JSON
- Update LambdaEcsLayout.json to include tenant.id field for Log4j2 ECS
- Update LambdaJsonEncoder JavaDoc to include tenant_id in listed fields
- Add getTenantId() override in TestLambdaContext test stub
- Update test assertions in LambdaJsonEncoderTest to verify tenant_id in output

Fixes: aws-powertools#2348
References: aws-powertools#2358

This implementation follows AWS Lambda Tenant Isolation feature requirements
and applies null-safety patterns consistent with correlation_id handling.
@phipag
Copy link
Copy Markdown
Contributor

phipag commented Apr 15, 2026

Hey @Pratapchandradeo thanks for opening this PR. Please check the PR template before we can proceed with the review: https://github.com/aws-powertools/powertools-lambda-java/blob/main/.github/PULL_REQUEST_TEMPLATE.md.

Copy link
Copy Markdown
Contributor

@phipag phipag left a comment

Choose a reason for hiding this comment

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

Also some first comments on the code :-)

}

String tenantId = mdcPropertyMap.get(TENANT_ID.getName());
if (tenantId != null) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should check for both null and empty string similar to the PowertoolsLoggedFields.

public boolean isResolvable(LogEvent logEvent) {
final String tenantId =
logEvent.getContextData().getValue(PowertoolsLoggedFields.TENANT_ID.getName());
return null != tenantId;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should check for both null and empty string similar to the PowertoolsLoggedFields.

@phipag
Copy link
Copy Markdown
Contributor

phipag commented Apr 16, 2026

@Pratapchandradeo thanks for the update. Please check my comment about the PR description format. The acknowledgement section is incorrect.

@sonarqubecloud
Copy link
Copy Markdown

@Pratapchandradeo Pratapchandradeo requested a review from phipag April 16, 2026 11:03
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.

Feature request: Add TenantId to Logger default properties

2 participants