繁体   English   中英

Spring Boot 2 中 Micrometer 的自定义 Cloudwatch MeterRegistry

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

我们想将执行器指标发送到 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方法永远不会被调用,也不会记录任何指标。 我缺少什么才能使其正常工作?

这是一个示例项目。 我自己没有使用cloudwatch,因此没有机会测试它是否与AWS集成。 如果有任何问题,请发表评论,我们可以尝试解决它们

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

我正在尝试做类似的事情,并避免使用 Spring Cloud。 到目前为止,我找到的最简单的解决方案是:

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

依赖项:

<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