Class CompositeCounters

java.lang.Object
org.pipservices3.components.count.CompositeCounters
All Implemented Interfaces:
org.pipservices3.commons.refer.IReferenceable, ICounters, ICounterTimingCallback

public class CompositeCounters extends Object implements ICounters, ICounterTimingCallback, org.pipservices3.commons.refer.IReferenceable
Aggregates all counters from component references under a single component.

It allows to capture metrics and conveniently send them to multiple destinations.

### References ###

  • *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements

### Example ###

 
 class MyComponent implements IReferenceable {
     CompositeCounters _counters = new CompositeCounters();
 
     public void setReferences(IReferences references) {
         this._counters.setReferences(references);
         ...
     }
 
     public void myMethod() {
         this._counters.increment("mycomponent.mymethod.calls");
         CounterTiming timing = this._counters.beginTiming("mycomponent.mymethod.exec_time");
         try {
            ...
         } finally {
             timing.endTiming();
         }
     }
 }
 
 
See Also:
  • Constructor Details

    • CompositeCounters

      public CompositeCounters()
      Creates a new instance of the counters.
  • Method Details

    • setReferences

      public void setReferences(org.pipservices3.commons.refer.IReferences references) throws org.pipservices3.commons.refer.ReferenceException
      Sets references to dependent components.
      Specified by:
      setReferences in interface org.pipservices3.commons.refer.IReferenceable
      Parameters:
      references - references to locate the component dependencies.
      Throws:
      org.pipservices3.commons.refer.ReferenceException - when no references found.
    • beginTiming

      public CounterTiming beginTiming(String name)
      Begins measurement of execution time interval. It returns CounterTiming object which has to be called at CounterTiming.endTiming() to end the measurement and update the counter.
      Specified by:
      beginTiming in interface ICounters
      Parameters:
      name - a counter name of Interval type.
      Returns:
      a CounterTiming callback object to end timing.
    • endTiming

      public void endTiming(String name, float elapsed)
      Ends measurement of execution elapsed time and updates specified counter.
      Specified by:
      endTiming in interface ICounterTimingCallback
      Parameters:
      name - a counter name
      elapsed - execution elapsed time in milliseconds to update the counter.
      See Also:
    • stats

      public void stats(String name, float value)
      Calculates min/average/max statistics based on the current and previous values.
      Specified by:
      stats in interface ICounters
      Parameters:
      name - a counter name of Statistics type
      value - a value to update statistics
    • last

      public void last(String name, float value)
      Records the last calculated measurement value. Usually this method is used by metrics calculated externally.
      Specified by:
      last in interface ICounters
      Parameters:
      name - a counter name of Last type.
      value - a last value to record.
    • timestampNow

      public void timestampNow(String name)
      Records the current time as a timestamp.
      Specified by:
      timestampNow in interface ICounters
      Parameters:
      name - a counter name of Timestamp type.
    • timestamp

      public void timestamp(String name, ZonedDateTime value)
      Records the given timestamp.
      Specified by:
      timestamp in interface ICounters
      Parameters:
      name - a counter name of Timestamp type.
      value - a timestamp to record.
    • incrementOne

      public void incrementOne(String name)
      Increments counter by 1.
      Specified by:
      incrementOne in interface ICounters
      Parameters:
      name - a counter name of Increment type.
    • increment

      public void increment(String name, int value)
      Increments counter by given value.
      Specified by:
      increment in interface ICounters
      Parameters:
      name - a counter name of Increment type.
      value - a value to add to the counter.