を使用して AWS Lambda 関数の SDK メトリクスを発行する AWS SDK for Java 2.x - AWS SDK for Java 2.x

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

を使用して AWS Lambda 関数の SDK メトリクスを発行する AWS SDK for Java 2.x

Lambda 関数は通常ミリ秒から数分実行されるため、 で発生するメトリクスの送信の遅延によりCloudWatchMetricPublisher、データが失われるリスクがあります。

EmfMetricLoggingPublisher は、CloudWatch Embedded Metric Format (EMF) で構造化ログエントリとしてメトリクスをすぐに記述することで、より適切なアプローチを提供します。 は、 AWS Lambda や Amazon Elastic Container Service などの Amazon CloudWatch Logs との統合が組み込まれている実行環境でEmfMetricLoggingPublisher動作します。

セットアップ

を使用してメトリクスを有効にして使用する前にEmfMetricLoggingPublisher、次の手順を実行します。

ステップ 1: 必要な依存関係を追加する

AWS SDK for Javaのバージョン 2.30.3 以降を使用するように、プロジェクトの依存関係を (例: pom.xml または build.gradle ファイルで) 設定します。

artifactId をバージョン番号 2.30.3 以降emf-metric-logging-publisherでプロジェクトの依存関係に含めます。

例:

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

ステップ 2: 必要なアクセス許可を設定する

メトリクスパブリッシャーが使用する IAM ID のlogs:PutLogEventsアクセス許可を有効にして、SDK for Java が EMF 形式のログを書き込むことを許可します。

ステップ 3: ログ記録を設定する

適切なメトリクス収集を確実に行うには、INFOレベル 以下のコンソール ( など) に出力するようにログ記録を設定しますDEBUGlog4j2.xml ファイルで、次の操作を行います。

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

log4j2.xml ファイルの設定方法の詳細については、このガイドのロギングトピックを参照してください。

の設定と使用 EmfMetricLoggingPublisher

次の Lambda 関数クラスは、まずEmfMetricLoggingPublisherインスタンスを作成して設定し、Amazon DynamoDB サービスクライアントで使用します。

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"; } }

DynamoDB クライアントは、 putItemメソッドを実行すると、メトリクスを EMF 形式で CloudWatch ログストリームに自動的に発行します。

たとえば、前述のようにログ記録が設定されている GameHandler Lambda 関数に次のイベントを送信するとします。

{ "id": "23456" }

関数がイベントを処理すると、次の例のような 2 つのログイベントが見つかります。2 番目のイベントの JSON オブジェクトには、DynamoDB への PutItem オペレーションの Java SDK メトリクスデータが含まれています。

CloudWatch は EMF 形式でログイベントを受信すると、構造化された JSON を自動的に解析してメトリクスデータを抽出します。その後、CloudWatch は、元のログエントリを CloudWatch Logs に保存しながら、対応するメトリクスを作成します。

2025-07-11 15:58:30 [main] INFO org.example.GameIdHandler:39 - Received map: {id=23456} 2025-07-11 15:58:34 [main] INFO software.amazon.awssdk.metrics.publishers.emf.EmfMetricLoggingPublisher:43 - { "_aws": { "Timestamp": 1752249513975, "LogGroupName": "/aws/lambda/GameId", "CloudWatchMetrics": [ { "Namespace": "namespace", "Dimensions": [ [ "OperationName", "ServiceId" ] ], "Metrics": [ { "Name": "AvailableConcurrency" }, { "Name": "PendingConcurrencyAcquires" }, { "Name": "ServiceCallDuration", "Unit": "Milliseconds" }, { "Name": "EndpointResolveDuration", "Unit": "Milliseconds" }, { "Name": "MaxConcurrency" }, { "Name": "BackoffDelayDuration", "Unit": "Milliseconds" }, { "Name": "MarshallingDuration", "Unit": "Milliseconds" }, { "Name": "LeasedConcurrency" }, { "Name": "SigningDuration", "Unit": "Milliseconds" }, { "Name": "ConcurrencyAcquireDuration", "Unit": "Milliseconds" }, { "Name": "ApiCallSuccessful" }, { "Name": "RetryCount" }, { "Name": "UnmarshallingDuration", "Unit": "Milliseconds" }, { "Name": "ApiCallDuration", "Unit": "Milliseconds" }, { "Name": "CredentialsFetchDuration", "Unit": "Milliseconds" } ] } ] }, "AvailableConcurrency": 0, "PendingConcurrencyAcquires": 0, "OperationName": "PutItem", "ServiceCallDuration": 1339, "EndpointResolveDuration": 81, "MaxConcurrency": 50, "BackoffDelayDuration": 0, "ServiceId": "DynamoDB", "MarshallingDuration": 181, "LeasedConcurrency": 1, "SigningDuration": 184, "ConcurrencyAcquireDuration": 83, "ApiCallSuccessful": 1, "RetryCount": 0, "UnmarshallingDuration": 85, "ApiCallDuration": 1880, "CredentialsFetchDuration": 138 }

API ドキュメントEmfMetricLoggingPublisher.Builderには、使用できる設定オプションが記載されています。

CloudWatchMetricPublisher に示すように、1 つのリクエストに対して EMF メトリクスログ記録を有効にすることもできます。

次のステップ: 長時間実行されるアプリケーションについては、CloudWatch ベースのメトリクス発行のために長時間実行されるアプリケーションから SDK メトリクスを発行する」を参照してください。