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 documentationEmfMetricLoggingPublisher.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.