Spring Boot 2 中用于 Micrometer 的自定义 Cloudwatch MeterRegistry
Custom Cloudwatch MeterRegistry for Micrometer in Spring Boot 2
我们想将执行器指标发送到 Cloudwatch。使用提供的千分尺 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 的集成。如果有任何问题请发表评论,我们会尽力解决
我正在尝试做类似的事情,并避免使用 Spring 云。到目前为止我找到的最简单的解决方案是:
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>
我们想将执行器指标发送到 Cloudwatch。使用提供的千分尺 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 的集成。如果有任何问题请发表评论,我们会尽力解决
我正在尝试做类似的事情,并避免使用 Spring 云。到目前为止我找到的最简单的解决方案是:
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>