Custom Cloudwatch MeterRegistry for Micrometer in Spring Boot 2(在Spring Boot 2中为测微器定制Cloudwatch MeterRegistry)
问题描述
我们希望向Cloudwatch发送致动器指标。使用提供的微米CloudWatch MeterRegistry解决方案对我们的项目如何设置做出了许多假设,例如,您需要依赖云AWS,而云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方法,也不会记录任何指标。我错过了什么才能使其正常工作?
推荐答案
以下是一个示例项目。我自己没有使用CloudWatch,所以没有机会测试它与AWS的集成。如果有任何问题,请留言,我们可以尝试解决它们
https://github.com/michaelmcfadyen/spring-boot-cloudwatch
这篇关于在Spring Boot 2中为测微器定制Cloudwatch MeterRegistry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Spring Boot 2中为测微器定制Cloudwatch MeterRegistry
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
