我正在致力于将 micrometer cloudwatch 库集成到 aws lambda 函数中。 我正在创建如下仪表注册表 -
@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();
使用此代码运行 lambda 后,我可以看到 counterName 出现在 cloudwatch 中。然而相同的计数始终为 0。
我验证了 lambda 角色具有 PutMetricData 权限。
这种情况正在发生,因为 lambda 运行了 15 秒,并且 Cloudwatch 配置设置为每 1 分钟发布一次指标。所以 lambda 在指标发布之前就退出了。
将该值设置为较小的值后,指标开始出现。