CompositeCounters

Aggregates all counters from component references into a single one.

Implements: ICounters, IReconfigurable, ICounterTimingCallback

Description

The CompositeCounters allows you to aggregate all counters from different component references into a single one.

Important points

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

References

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

Constructors

Creates a new instance of the counters.

CompositeCounters(references: IReferences = None)

  • references: IReferences - references to locate the component dependencies.

Fields

_counters

A list containig the aggregated counters.

_counters: List[ICounters] = []

Instance methods

begin_timing

Begins measurement of execution time interval. It returns CounterTiming object which has to be called at CounterTiming.end_timing to end the measurement and update the counter.

begin_timing(name: str): CounterTiming

  • name: str - a counter name of Interval type.
  • returns: CounterTiming - a callback object to end timing.

end_timing

Ends measurement of execution elapsed time and updates specified counter.

end_timing(name: str, elapsed: float)

  • name: str - a counter name
  • elapsed: float - execution elapsed time in milliseconds to update the counter.

increment

Increments counter by given value.

increment(name: str, value: float)

  • name: str - a counter name of Increment type.
  • value: float - a value to add to the counter.

increment_one

Increments counter by 1.

increment_one(name: str)

  • name: str - a counter name of Increment type.

last

Records the last calculated measurement value. Usually this method is used by metrics calculated externally.

last(name: str, value: float)

  • name: str - a counter name of Last type.
  • value: float - last value to record.

set_references

Sets references to dependent components.

set_references(references: IReferences)

  • references: IReferences - references to locate the component dependencies.

stats

Calculates min/average/max statistics based on the current and previous values.

stats(name: str, value: float)

  • name: str - a counter name of Statistics type
  • value: float - a value to update statistics

timestamp

Records the given timestamp.

timestamp(name: str, value: datetime.datetime)

  • name: str - a counter name of Timestamp type.
  • value: float - a timestamp to record.

timestamp_now

Records the current time as a timestamp.

timestamp_now(name: str)

  • name: str - a counter name of Timestamp type.

Examples

class MyComponent(IReferenceable):
    _counters: CompositeCounters = CompositeCounters()
    
    def setReferences(self, references: IReferences):
        self._counters.set_references(references)
        ...
    
    
    def myMethod(self):
        this._counters.increment("mycomponent.mymethod.calls")
        var timing = this._counters.begin_timing("mycomponent.mymethod.exec_time")
        try:
            ...
        except:
            timing.end_timing()

See also