Class StatCounter
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
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionStatCounter(String name, String uid, StatCounter.Stat[] baseStats, StatCounter.Stat[] stats) Constructor without logging capability.StatCounter(String name, String uid, org.slf4j.Logger log, int logFrequency, StatCounter.Stat[] baseStats) Constructor.StatCounter(String name, String uid, org.slf4j.Logger log, int logFrequency, StatCounter.Stat[] baseStats, StatCounter.Stat[] stats) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionlongaddAndGet(StatCounter.Stat stat, long count) Add to and get the current count value.longaddAndGet(StatCounter.Stat stat, long count, boolean quiet) Add to and get the current count value.longget(StatCounter.Stat stat) Get a current count value.intGet the log frequency.getUid()Get the unique ID.longIncrement and get the current count value.longincrementAndGet(StatCounter.Stat stat, boolean quiet) Increment and get the current count value.voidsetLogFrequency(int logFrequency) Set the log frequency.voidSet the unique ID.toString()
-
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 logginglogFrequency- a frequency at which to log INFO level statistic messagesbaseStats- the "base" statistics to track; can not be null- Throws:
IllegalArgumentException- if any required argument is null, orstatsis provided and the component type is assignable from thebaseStatscomponent type
-
StatCounter
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 nullstats- the non-base statistics to track; can be null- Throws:
IllegalArgumentException- if any required argument is null, orstatsis provided and the component type is assignable from thebaseStatscomponent 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 logginglogFrequency- a frequency at which to log INFO level statistic messagesbaseStats- the "base" statistics to track; can not be nullstats- the non-base statistics to track; can be null- Throws:
IllegalArgumentException- if any required argument is null, orstatsis provided and the component type is assignable from thebaseStatscomponent type
-
-
Method Details
-
toString
-
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
Get the unique ID.- Returns:
- the unique ID
-
setUid
Set the unique ID.- Parameters:
uid- the unique ID, or null for none
-
get
Get a current count value.- Parameters:
stat- the statistic to get the count for- Returns:
- the current count value
- Throws:
IllegalArgumentException- ifstatis null or not the same type as the configuredbaseStatsorstatscomponent type
-
incrementAndGet
Increment and get the current count value.- Parameters:
stat- the count to increment and get- Returns:
- the incremented count value
- Throws:
IllegalArgumentException- ifstatis null or not the same type as the configuredbaseStatsorstatscomponent type
-
incrementAndGet
Increment and get the current count value.- Parameters:
stat- the count to increment and getquiet- true to ignore logging- Returns:
- the incremented count value
- Throws:
IllegalArgumentException- ifstatis null or not the same type as the configuredbaseStatsorstatscomponent type
-
addAndGet
Add to and get the current count value.- Parameters:
stat- the count to add to and getcount- the amount to add- Returns:
- the added count value
- Throws:
IllegalArgumentException- ifstatis null or not the same type as the configuredbaseStatsorstatscomponent type
-
addAndGet
Add to and get the current count value.- Parameters:
stat- the count to add to and getcount- the amount to addquiet- true to ignore logging- Returns:
- the added count value
- Throws:
IllegalArgumentException- ifstatis null or not the same type as the configuredbaseStatsorstatscomponent type
-