Class Metric

  • All Implemented Interfaces:
    Serializable
    Direct Known Subclasses:
    EventCounter, VMUsage

    public abstract class Metric
    extends Object
    implements Serializable
    Abstract superclass for EventCounter, SessionCounter, and other metric classes.

    Each Metric is associated with a descriptive label that is used in reporting to suggest what is being counted.

    A Metric views time as a series of non-overlapping time intervals. The duration of an interval is a number of seconds and does not vary for the intervals within a given Metric. At any point in time, a Metric maintains knowledge of the current interval, as an Interval object, as well as one or more Intervals that have were noted in the history of this metric.

    Choosing the duration of an interval. Each instance of a Metric has a fixed duration for each interval. The choice of duration makes no difference in the actual count of event occurrences. It may, however, influence other calculations relevant to a particular subclass of Metric. For example, an EventCounter is able to report the peak frequency of events. If you are counting train departures from a station, it may be reasonable to configure the interval duration at one hour, and learn the peak frequency rate in departures/hour. If you used the default duration of one second, the peak frequency would probably never exceed 1.0 per second during even the busiest part of the day. If, on the other hand, you are counting database commits, then a one second interval may be appropriate, since detecting a spike at 100 per second might be very helpful and could go unnoticed in a commits per hour rate.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int intervalSeconds
      How many seconds are in each interval
      protected long startTime
      When the Metric was constructed or reset
      protected long totalIntervals
      Number of intervals that have occurred so far during the life of this Metric
    • Constructor Summary

      Constructors 
      Constructor Description
      Metric()
      No-arg constructor necessary for serialization.
      Metric​(String label)
      Constructs a new Metric with an interval duration of 1 second.
      Metric​(String label, int intervalDurationInSeconds)
      Constructs a new Metric.
    • Field Detail

      • startTime

        protected long startTime
        When the Metric was constructed or reset
      • intervalSeconds

        protected int intervalSeconds
        How many seconds are in each interval
      • totalIntervals

        protected long totalIntervals
        Number of intervals that have occurred so far during the life of this Metric
    • Constructor Detail

      • Metric

        public Metric​(String label,
                      int intervalDurationInSeconds)
        Constructs a new Metric.
        Parameters:
        label - descriptive label for this Metric
        intervalDurationInSeconds - number of seconds in each interval
      • Metric

        public Metric​(String label)
        Constructs a new Metric with an interval duration of 1 second.
        Parameters:
        label - - descriptive label for this Metric
      • Metric

        public Metric()
        No-arg constructor necessary for serialization.
    • Method Detail

      • getIntervalSeconds

        public int getIntervalSeconds()
        Gets the number of seconds in each interval.
        Returns:
        number of seconds in each interval
      • getLabel

        public String getLabel()
        Gets the descriptive label associated with this Metric instance.
        Returns:
        descriptive label for this Metric
      • setLabel

        public void setLabel​(String label)
        Sets the descriptive label associated with this Metric instance.
        Parameters:
        label - descriptive label for this Metric
      • getAgeMillis

        public long getAgeMillis()
        Gets the age of this metric in milliseconds.
        Returns:
        age in milliseconds of this Metric
      • getAge

        public float getAge()
        Gets the age of this metric in seconds.
        Returns:
        age of this Metric in seconds
      • add

        public void add()
        Adds a single event.

        Equivalent to add(1).

      • add

        public abstract void add​(int count)
        Adds a number of events.

        All events are considered to have happened simultaneously.

        Parameters:
        count - number of events counted by this call
      • getCount

        public abstract long getCount()
        Gets the total count of events.
        Returns:
        count
      • getTotalIntervals

        public long getTotalIntervals()
        Gets number of intervals that have occurred during the life of this Metric.
        Returns:
        number of intervals
      • reset

        public void reset()
        Reset this Metric to its "zero" condition.
      • uptime

        public static float uptime()
        Return the uptime in hours, measured from when the Metric class was loaded into the VM by the classloader.
        Returns:
        Description of the Return Value