I am working on integrating micrometer cloudwatch library in aws lambda function. I am creating meter registry as below -
@Provides
@Singleton
public CloudWatchAsyncClient cloudWatchAsyncClient() {
return CloudWatchAsyncClient.builder().region(Region.of("us-east-1")).build();
}
@Provides
@Singleton
public MeterRegistry getMeterRegistry(CloudWatchAsyncClient cloudWatchAsyncClient) {
CloudWatchConfig cloudWatchConfig = setupCloudWatchConfig();
return new CloudWatchMeterRegistry(cloudWatchConfig, Clock.SYSTEM, cloudWatchAsyncClient);
}
private CloudWatchConfig setupCloudWatchConfig() {
return new CloudWatchConfig() {
private final Map<String, String> configuration =
Map.of(
"cloudwatch.namespace",
"service-name",
"cloudwatch.step",
Duration.ofMinutes(1).toString());
@Override
public String get(String key) {
return configuration.get(key);
}
};
}
meterRegistry.counter("counterName").increment();
After running lambda with this code, I am able to see counterName appearing in cloudwatch. However count for the same is always 0.
I verified that lambda role has PutMetricData permission.
This was happening, as lambda was running for 15 seconds and Cloudwatch config was set to publish metrics every 1 minute. So lambda was exiting before metrics were getting published.
After setting that value to a lesser value, metrics started appearing.