java.lang.Object
org.elasticsearch.common.metrics.Counters
All Implemented Interfaces:
Writeable

public final class Counters extends Object implements Writeable
Simple usage stat counters based on longs. Internally this is a map mapping from String which is the counter's label to a CounterMetric which is the value. This class also provides a helper method that converts the counters to a nested map, using the "." in the label as a splitter. This allows the stats producer to not worry about how the map is actually nested. IMPORTANT: if the consumer of the metrics will make use of the nested map, it is the responsibility of the producer to provide labels that will not have conflicts, which means that there no counter will have a label which is a substring of the label of another counter. For example, the counters `foo: 1` and `foo.bar: 3` cannot co-exist in a nested map.
  • Constructor Details

  • Method Details

    • inc

      public void inc(String name)
      Increment the counter by one
      Parameters:
      name - Name of the counter
    • inc

      public void inc(String name, long count)
      Increment the counter by configured number
      Parameters:
      name - The name of the counter
      count - Incremental value
    • get

      public long get(String name)
    • size

      public long size()
    • hasCounters

      public boolean hasCounters()
    • toMutableNestedMap

      public Map<String,Object> toMutableNestedMap()
      Convert the counters to a nested map, using the "." as a splitter to create a nested map. For example, the counters `foo.bar`: 2, `foo.baz`: 1, `foobar`: 5 would become: { "foo": { "bar": 2, "baz": 1 }, "foobar": 5 }
      Returns:
      A mutable nested map with all the current configured counters. The map is mutable to allow the client to further enrich it.
      Throws:
      IllegalStateException - if there is a conflict in a path of two counters for example `foo`: 1 and `foo.bar`: 1.
    • writeTo

      public void writeTo(StreamOutput out) throws IOException
      Description copied from interface: Writeable
      Write this into the StreamOutput.
      Specified by:
      writeTo in interface Writeable
      Throws:
      IOException
    • merge

      public static Counters merge(List<Counters> counters)