简体   繁体   English

Spring Boot 2 中 Micrometer 的自定义 Cloudwatch MeterRegistry

[英]Custom Cloudwatch MeterRegistry for Micrometer in Spring Boot 2

We would like to send actuator metrics to Cloudwatch.我们想将执行器指标发送到 Cloudwatch。 Using the provided micrometer cloudwatch MeterRegistry solutions makes to many assumptions about how our project is setup, for example you need to depend on cloud AWS which then makes even more assumptions.使用提供的 micrometer cloudwatch MeterRegistry 解决方案对我们的项目如何设置做出了许多假设,例如,您需要依赖云 AWS,然后它会做出更多假设。 We would like to write a more lightweight implementation which just get a CloudWatchAsyncClient injected and makes no other assumptions about our project.我们想编写一个更轻量级的实现,它只注入一个 CloudWatchAsyncClient 并且不对我们的项目做其他假设。

However im not sure how.但是我不确定如何。 Is there any example on how to make a custom implementation insted of having to depend on the available metrics registry?是否有任何示例说明如何使自定义实现不再依赖于可用的指标注册表?

So far I have done some experimenting with the following:到目前为止,我已经对以下内容进行了一些实验:

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";
                }

            }
        };
    }
}

However when I run the publish method is never called and no metrics are ever logged.但是,当我运行时, publish方法永远不会被调用,也不会记录任何指标。 What am I missing to get this working?我缺少什么才能使其正常工作?

Here's an example project. 这是一个示例项目。 I don't use cloudwatch myself so not had a chance to test it integrating with AWS. 我自己没有使用cloudwatch,因此没有机会测试它是否与AWS集成。 Leave a comment if there are any issues and we can try to resolve them 如果有任何问题,请发表评论,我们可以尝试解决它们

https://github.com/michaelmcfadyen/spring-boot-cloudwatch https://github.com/michaelmcfadyen/spring-boot-cloudwatch

I am trying to do something similar, and avoid using Spring Cloud.我正在尝试做类似的事情,并避免使用 Spring Cloud。 The simplest solution I have found so far is:到目前为止,我找到的最简单的解决方案是:

import io.micrometer.cloudwatch2.CloudWatchConfig;
import io.micrometer.cloudwatch2.CloudWatchMeterRegistry;
import io.micrometer.core.instrument.Clock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;

@Configuration
public class MetricsConfiguration {

    @Bean
    public CloudWatchMeterRegistry cloudWatchMeterRegistry(CloudWatchConfig config, Clock clock) {
        return new CloudWatchMeterRegistry(config, clock, CloudWatchAsyncClient.create());
    }

    @Component
    public static class MicrometerCloudWatchConfig
            extends StepRegistryPropertiesConfigAdapter<StepRegistryProperties>
            implements CloudWatchConfig {

        private final String namespace;
        private final boolean enabled;

        public MicrometerCloudWatchConfig(
                @Value("${CLOUDWATCH_NAMESPACE}") String namespace,
                @Value("${METRICS_ENABLED}") boolean enabled) {
            super(new StepRegistryProperties() {
            });
            this.namespace = namespace;
            this.enabled = enabled;
        }

        @Override
        public String namespace() {
            return namespace;
        }

        @Override
        public boolean enabled() {
            return enabled;
        }

        @Override
        public int batchSize() {
            return CloudWatchConfig.MAX_BATCH_SIZE;
        }
    }
}

Dependencies:依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-cloudwatch2</artifactId>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM