【问题标题】:Custom Cloudwatch MeterRegistry for Micrometer in Spring Boot 2在 Spring Boot 2 中为 Micrometer 自定义 Cloudwatch MeterRegistry
【发布时间】:2019-11-15 18:00:21
【问题描述】:

我们想将执行器指标发送到 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 方法时,永远不会调用它,也不会记录任何指标。让这个工作我缺少什么?

【问题讨论】:

    标签: java spring-boot amazon-cloudwatch metrics micrometer


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我正在尝试做类似的事情,并避免使用 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>
      

      【讨论】:

        猜你喜欢
        • 2020-02-19
        • 1970-01-01
        • 2019-09-13
        • 1970-01-01
        • 2020-03-21
        • 2018-11-03
        • 2020-07-14
        • 1970-01-01
        • 2020-04-19
        相关资源
        最近更新 更多