【发布时间】:2019-11-15 18:00:21
【问题描述】:
我们想将执行器指标发送到 Cloudwatch。使用提供的 micrometer cloudwatch MeterRegistry 解决方案对我们的项目如何设置做出了许多假设,例如,您需要依赖云 AWS,然后再做出更多假设。我们想编写一个更轻量级的实现,它只注入一个 CloudWatchAsyncClient 并且不对我们的项目做任何其他假设。
但是我不确定如何。是否有任何示例说明如何使自定义实现不得不依赖可用的指标注册表?
到目前为止,我已经对以下内容进行了一些实验:
public interface CloudWatchConfig extends StepRegistryConfig {
int MAX_BATCH_SIZE = 20;
@Override
default String prefix() {
return "cloudwatch";
}
default String namespace() {
String v = get(prefix() + ".namespace");
if (v == null)
throw new MissingRequiredConfigurationException("namespace must be set to report metrics to CloudWatch");
return v;
}
@Override
default int batchSize() {
String v = get(prefix() + ".batchSize");
if (v == null) {
return MAX_BATCH_SIZE;
}
int vInt = Integer.parseInt(v);
if (vInt > MAX_BATCH_SIZE)
throw new InvalidConfigurationException("batchSize must be <= " + MAX_BATCH_SIZE);
return vInt;
}
}
@Service
@Log
public class CloudWatchMeterRegistry extends StepMeterRegistry {
public CloudWatchMeterRegistry(CloudWatchConfig config, Clock clock) {
super(config, clock);
}
@Override
protected void publish() {
getMeters().stream().forEach(a -> {
log.warning(a.getId().toString());
});
}
@Override
protected TimeUnit getBaseTimeUnit() {
return TimeUnit.MILLISECONDS;
}
}
@Configuration
public class MetricsPublisherConfig {
@Bean
public CloudWatchConfig cloudWatchConfig() {
return new CloudWatchConfig() {
@Override
public String get(String key) {
switch (key) {
case "cloudwatch.step":
return props.getStep();
default:
return "testtest";
}
}
};
}
}
但是,当我运行 publish 方法时,永远不会调用它,也不会记录任何指标。让这个工作我缺少什么?
【问题讨论】:
标签: java spring-boot amazon-cloudwatch metrics micrometer