Class Counter

  • All Implemented Interfaces:
    Collector.Describable

    public class Counter
    extends SimpleCollector<Counter.Child>
    implements Collector.Describable
    Counter metric, to track counts of events or running totals.

    Example of Counters include:

    • Number of requests processed
    • Number of items that were inserted into a queue
    • Total amount of data a system has processed
    Counters can only go up (and be reset), if your use case can go down you should use a Gauge instead. Use the rate() function in Prometheus to calculate the rate of increase of a Counter. By convention, the names of Counters are suffixed by _total.

    An example Counter:

     
       class YourClass {
         static final Counter requests = Counter.build()
             .name("requests_total").help("Total requests.").register();
         static final Counter failedRequests = Counter.build()
             .name("requests_failed_total").help("Total failed requests.").register();
    
         void processRequest() {
            requests.inc();
            try {
              // Your code here.
            } catch (Exception e) {
              failedRequests.inc();
              throw e;
            }
         }
       }
     
     

    You can also use labels to track different types of metric:

     
       class YourClass {
         static final Counter requests = Counter.build()
             .name("requests_total").help("Total requests.")
             .labelNames("method").register();
    
         void processGetRequest() {
            requests.labels("get").inc();
            // Your code here.
         }
         void processPostRequest() {
            requests.labels("post").inc();
            // Your code here.
         }
       }
     
     
    These can be aggregated and processed together much more easily in the Prometheus server than individual metrics for each labelset.
    • Method Detail

      • build

        public static Counter.Builder build​(String name,
                                            String help)
        Return a Builder to allow configuration of a new Counter. Ensures required fields are provided.
        Parameters:
        name - The name of the metric
        help - The help string of the metric
      • build

        public static Counter.Builder build()
        Return a Builder to allow configuration of a new Counter.
      • inc

        public void inc()
        Increment the counter with no labels by 1.
      • inc

        public void inc​(double amt)
        Increment the counter with no labels by the given amount.
        Throws:
        IllegalArgumentException - If amt is negative.
      • get

        public double get()
        Get the value of the counter.
      • describe

        public List<Collector.MetricFamilySamplesdescribe()
        Description copied from interface: Collector.Describable
        Provide a list of metric families this Collector is expected to return. These should exclude the samples. This is used by the registry to detect collisions and duplicate registrations. Usually custom collectors do not have to implement Describable. If Describable is not implemented and the CollectorRegistry was created with auto describe enabled (which is the case for the default registry) then Collector.collect() will be called at registration time instead of describe. If this could cause problems, either implement a proper describe, or if that's not practical have describe return an empty list.
        Specified by:
        describe in interface Collector.Describable