Skip to content

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

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

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

Conversation

@Pratapchandradeo
Copy link
Copy Markdown

@Pratapchandradeo Pratapchandradeo commented Apr 15, 2026

Description

This PR adds support for AWS Lambda Tenant Isolation feature by automatically logging tenant_id from the Lambda context to all logging outputs.

What Changed

  • Add TENANT_ID field with null-safety checks
  • Create Log4j2 TENANT_ID_RESOLVER
  • Update JSON/ECS layout configurations
  • Add tenant_id serialization to Logback encoders
  • Update tests to verify tenant_id logging

Fixes #2348

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.
@sonarqubecloud
Copy link
Copy Markdown

@powertools-for-aws-oss-automation
Copy link
Copy Markdown

No acknowledgement section found. Please make sure you used the template to open a PR and didn't remove the acknowledgment section. Check the template at .github/PULL_REQUEST_TEMPLATE.md#acknowledgment

@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 :-)

hashMap.put(FUNCTION_MEMORY_SIZE.name, String.valueOf(context.getMemoryLimitInMB()));
hashMap.put(FUNCTION_REQUEST_ID.name, String.valueOf(context.getAwsRequestId()));
String tenantId = context.getTenantId();
if (tenantId != null && !tenantId.isEmpty()) {
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.

Can we add a unit test for tenantId being null and empty?

}

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.

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