Publish SDK metrics from the Amazon SDK for Java 2.x - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Publish SDK metrics from the Amazon SDK for Java 2.x

With the Amazon SDK for Java 2.x you can collect metrics about the service clients and requests in your application, analyze the output in Amazon CloudWatch Logs, and then act on it.

By default, metrics collection is disabled in the SDK. This topic helps you to enable and configure it.

Getting started with SDK metrics

To enable metrics collection in your application, choose the appropriate implementation of the MetricPublisher interface based on your use case and follow the detailed setup instructions:

For long-running applications:

For Amazon Lambda functions:

For troubleshooting and console output:

Quick implementation preview

Here's what enabling metrics looks like for each use case:

Long-running applications:

MetricPublisher metricsPub = CloudWatchMetricPublisher.create(); DynamoDbClient ddb = DynamoDbClient.builder() .overrideConfiguration(c -> c.addMetricPublisher(metricsPub)) .build();

Lambda functions:

EmfMetricLoggingPublisher emfPublisher = EmfMetricLoggingPublisher.builder() .namespace("MyApp") .build(); DynamoDbClient dynamoDb = DynamoDbClient.builder() .overrideConfiguration(c -> c.addMetricPublisher(emfPublisher)) .build();

Development and debugging:

MetricPublisher loggingPublisher = LoggingMetricPublisher.create(); S3Client s3 = S3Client.builder() .overrideConfiguration(c -> c.addMetricPublisher(loggingPublisher)) .build();

For complete implementation details, dependencies, and configuration options, see the setup guides above.

When are metrics available?

Metrics are generally available within 5-10 minutes after the SDK for Java emits them. For accurate and up-to-date metrics, check Cloudwatch at least 10 minutes after emitting the metrics from your Java applications.

What information is collected?

Metrics collection includes the following:

  • Number of API requests, including whether they succeed or fail

  • Information about the Amazon Web Services services you call in your API requests, including exceptions returned

  • The duration for various operations such as Marshalling, Signing, and HTTP requests

  • HTTP client metrics, such as the number of open connections, the number of pending requests, and the name of the HTTP client used

Note

The metrics available vary by HTTP client.

For a complete list, see Service client metrics.

How can I use this information?

You can use the metrics the SDK collects to monitor the service clients in your application. You can look at overall usage trends, identify anomalies, review service client exceptions returned, or to dig in to understand a particular issue. Using Amazon CloudWatch Logs, you can also create alarms to notify you as soon as your application reaches a condition that you define.

For more information, see Using Amazon CloudWatch Logs Metrics and Using Amazon CloudWatch Logs Alarms in the Amazon CloudWatch Logs User Guide.

Topics