在Spring Boot 2中为测微器定制Cloudwatch MeterRegistry [英] Custom Cloudwatch MeterRegistry for Micrometer in Spring Boot 2

查看:87
本文介绍了在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆