title: Are counters thread safe in Micrometer
body: <p>I am trying to publish number of events my App is processing. This is the code I have at the receiving end:</p>
  
  <pre><code>public void process(List&lt;String&gt; batch) {
      logger.info ("Processing batch of size " + batch.size())
      metrics.incrementCounter(MetricsType.CONSUMER_TOTAL_PROCESSED, batch.size)
  }
  </code></pre>
  
  <p>The class Metrics is:</p>
  
  <pre><code>public class Metrics {
      private static final Map&lt;MetricsType, Counter&gt; COUNTER_MAP = new ConcurrentHashMap&lt;&gt;();
  
      public Metrics(
          @Autowired MeterRegistry meterRegistry
      ) {
          COUNTER_MAP.put(
              MetricsType.CONSUMER_TOTAL_PROCESSED,
              Counter.builder("CONSUMER_TOTAL_PROCESSED").register(meterRegistry)
          );
  
          COUNTER_MAP.put(
              MetricsType.CONSUMER_DUPLICATE_PROCESSED,
              Counter.builder("CONSUMER_DUPLICATE_PROCESSED").register(meterRegistry)
          );
      }
  
      public void increment(MetricsType metricsType, int size) {
          COUNTER_MAP.get(metricsType).increment(size);
      }
  }
  </code></pre>
  
  <p>The enum <code>MetricsType</code> contains all type of counters.</p>
  
  <p>The <code>process</code> method is be invoked by 16 threads at any time. The issue I am facing is the logger which prints the count and the total count reported in grafana are way off.</p>
  
  <p>Do I have to synchonize everytime I am incrementing the counter?</p>
  
  <hr>
  
  <p>Edit - What I mean by the counts are off is, if there are two logs with size 200, then grafana should report total counter 400. I am validating this by taking a time range of 2 hours, I extract all the sizes from logs and add them.</p>
  
  <hr>
  
  <p>If you stumble upon this and see a difference between two sources please check what is your maximum number of data points in Grafana, that was the actual issue why I thought counters may not be threadsafe. </p>