title: Custom Cloudwatch MeterRegistry for Micrometer in Spring Boot 2
body: <p>We would like to send actuator metrics to 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. We would like to write a more lightweight implementation which just get a CloudWatchAsyncClient injected and makes no other assumptions about our project. </p>
  
  <p>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? </p>
  
  <p>So far I have done some experimenting with the following:</p>
  
  <pre><code>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 &gt; MAX_BATCH_SIZE)
              throw new InvalidConfigurationException("batchSize must be &lt;= " + MAX_BATCH_SIZE);
  
          return vInt;
      }
  }
  </code></pre>
  
  <pre><code>@Service
  @Log
  public class CloudWatchMeterRegistry extends StepMeterRegistry {
  
      public CloudWatchMeterRegistry(CloudWatchConfig config, Clock clock) {
          super(config, clock);
      }
  
      @Override
      protected void publish() {
          getMeters().stream().forEach(a -&gt; {
              log.warning(a.getId().toString());
          });
      }
  
      @Override
      protected TimeUnit getBaseTimeUnit() {
          return TimeUnit.MILLISECONDS;
      }
  }
  </code></pre>
  
  <pre><code>@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";
                  }
  
              }
          };
      }
  }
  </code></pre>
  
  <p>However when I run the <code>publish</code> method is never called and no metrics are ever logged. What am I missing to get this working?</p>