Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Publish SDK metrics for AWS Lambda functions - AWS SDK for Java 2.x

Publish SDK metrics for AWS Lambda functions

Because Lambda functions typically execute for milliseconds to minutes, any delay in sending the metrics, which happens with the CloudWatchMetricPublisher, risks the loss of data.

EmfMetricLoggingPublisher provides a more suitable approach by immediately writing metrics as structured log entries in CloudWatch Embedded Metric Format (EMF). EmfMetricLoggingPublisher works in execution environments that have built-in integration with Amazon CloudWatch Logs such as AWS Lambda and Amazon Elastic Container Service.

Set-up

Before you can enable and use metrics by using EmfMetricLoggingPublisher, complete the following steps.

Step 1: Add required dependency

Configure your project dependencies (for example, in your pom.xml or build.gradle file) to use version 2.30.3 or later of the AWS SDK for Java.

Include the artifactId emf-metric-logging-publisher with the version number 2.30.3 or later in your project’s dependencies.

For example:

<project> <dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.30.11</version> <!-- Navigate the link to see the latest version. --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>emf-metric-logging-publisher</artifactId> </dependency> </dependencies> </project>

Step 2: Configure required permissions

Enable logs:PutLogEvents permissions for the IAM identity used by the metrics publisher to allow the SDK for Java to write EMF-formatted logs.

Step 3: Setup logging

To ensure proper metric collection, configure your logging to output to the console at the INFO level or lower (such as DEBUG). In your log4j2.xml file:

<Loggers> <Root level="WARN"> <AppenderRef ref="ConsoleAppender"/> </Root> <Logger name="software.amazon.awssdk.metrics.publishers.emf.EmfMetricLoggingPublisher" level="INFO" /> </Loggers>

See the logging topic in this guide for more information on how to set up a log4j2.xml file.

Configure and use EmfMetricLoggingPublisher

The following Lambda function class first creates and configures an EmfMetricLoggingPublisher instance and then uses it with a Amazon DynamoDB service client:

public class GameIdHandler implements RequestHandler<Map<String, String>, String> { private final EmfMetricLoggingPublisher emfPublisher; private final DynamoDbClient dynamoDb; public GameIdHandler() { // Build the publisher. this.emfPublisher = EmfMetricLoggingPublisher.builder() .namespace("namespace") .dimensions(CoreMetric.SERVICE_ID, CoreMetric.OPERATION_NAME) .build(); // Add the publisher to the client. this.dynamoDb = DynamoDbClient.builder() .overrideConfiguration(c -> c.addMetricPublisher(emfPublisher)) .region(Region.of(System.getenv("AWS_REGION"))) .build(); } @Override public String handleRequest(Map<String, String> event, Context context) { Map<String, AttributeValue> gameItem = new HashMap<>(); gameItem.put("gameId", AttributeValue.builder().s(event.get("id")).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName("games") .item(gameItem) .build(); dynamoDb.putItem(putItemRequest); return "Request handled"; } }

When the DynamoDB client executes the putItem method, it automatically publishes metrics to a CloudWatch log stream in EMF format.

The API documentation for EmfMetricLoggingPublisher.Builder shows the configuration options that you can use.

You can also enable EMF metric logging for a single request as shown for the CloudWatchMetricPublisher.

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.