Class StatCounter

java.lang.Object
net.solarnetwork.util.StatCounter

public class StatCounter extends Object
Count statistic helper.

This class keeps track of an atomic array of long count values. It is designed to help provide runtime operational information in a structured way, so services can define a set of named counters they wish to track and expose to users.

An enum that implements StatCounter.Stat can be used to make the counter indexes have meaningful names in code.

This class supports a "base" set of statistics and a "non-base" set of statistics. This design supports a common set of statistics that might be applicable across several different services, along with another unique set of statistics specific to the service. For example, all MQTT clients might track a "base" set of message count statistics while one client service might track specific message types differently than another client service.

Since:
1.78
Version:
1.0
Author:
matt
  • Constructor Details

    • StatCounter

      public StatCounter(String name, String uid, org.slf4j.Logger log, int logFrequency, StatCounter.Stat[] baseStats)
      Constructor.
      Parameters:
      name - the name to use (appears on logs)
      uid - the UID to use (appears on logs)
      log - the Logger to use, or null for no logging
      logFrequency - a frequency at which to log INFO level statistic messages
      baseStats - the "base" statistics to track; can not be null
      Throws:
      IllegalArgumentException - if any required argument is null, or stats is provided and the component type is assignable from the baseStats component type
    • StatCounter

      public StatCounter(String name, String uid, StatCounter.Stat[] baseStats, StatCounter.Stat[] stats)
      Constructor without logging capability.
      Parameters:
      name - the name to use (appears on logs)
      uid - the UID to use (appears on logs)
      baseStats - the "base" statistics to track; can not be null
      stats - the non-base statistics to track; can be null
      Throws:
      IllegalArgumentException - if any required argument is null, or stats is provided and the component type is assignable from the baseStats component type
    • StatCounter

      public StatCounter(String name, String uid, org.slf4j.Logger log, int logFrequency, StatCounter.Stat[] baseStats, StatCounter.Stat[] stats)
      Constructor.
      Parameters:
      name - the name to use (appears on logs)
      uid - the UID to use (appears on logs)
      log - the Logger to use, or null for no logging
      logFrequency - a frequency at which to log INFO level statistic messages
      baseStats - the "base" statistics to track; can not be null
      stats - the non-base statistics to track; can be null
      Throws:
      IllegalArgumentException - if any required argument is null, or stats is provided and the component type is assignable from the baseStats component type
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getLogFrequency

      public int getLogFrequency()
      Get the log frequency.
      Returns:
      the log frequency
    • setLogFrequency

      public void setLogFrequency(int logFrequency)
      Set the log frequency.
      Parameters:
      logFrequency - the frequency
    • getUid

      public String getUid()
      Get the unique ID.
      Returns:
      the unique ID
    • setUid

      public void setUid(String uid)
      Set the unique ID.
      Parameters:
      uid - the unique ID, or null for none
    • get

      public long get(StatCounter.Stat stat)
      Get a current count value.
      Parameters:
      stat - the statistic to get the count for
      Returns:
      the current count value
      Throws:
      IllegalArgumentException - if stat is null or not the same type as the configured baseStats or stats component type
    • incrementAndGet

      public long incrementAndGet(StatCounter.Stat stat)
      Increment and get the current count value.
      Parameters:
      stat - the count to increment and get
      Returns:
      the incremented count value
      Throws:
      IllegalArgumentException - if stat is null or not the same type as the configured baseStats or stats component type
    • incrementAndGet

      public long incrementAndGet(StatCounter.Stat stat, boolean quiet)
      Increment and get the current count value.
      Parameters:
      stat - the count to increment and get
      quiet - true to ignore logging
      Returns:
      the incremented count value
      Throws:
      IllegalArgumentException - if stat is null or not the same type as the configured baseStats or stats component type
    • addAndGet

      public long addAndGet(StatCounter.Stat stat, long count)
      Add to and get the current count value.
      Parameters:
      stat - the count to add to and get
      count - the amount to add
      Returns:
      the added count value
      Throws:
      IllegalArgumentException - if stat is null or not the same type as the configured baseStats or stats component type
    • addAndGet

      public long addAndGet(StatCounter.Stat stat, long count, boolean quiet)
      Add to and get the current count value.
      Parameters:
      stat - the count to add to and get
      count - the amount to add
      quiet - true to ignore logging
      Returns:
      the added count value
      Throws:
      IllegalArgumentException - if stat is null or not the same type as the configured baseStats or stats component type