klio.metrics.client

Metrics registry to manage metrics for all configured relay clients.

The MetricsRegistry class is the main client for which to create and emit metrics. For example:

class MyTransform(KlioBaseTransform):
    def __init__(self):
        self.my_counter = self._klio.metrics.counter(
            name="foo",
            value=2,
            transform="mytransform",
            tags={"tag1": "value1"}
        )

    def process(self, element):
        # user code

        # immediately emits a metric
        self.my_counter.inc()
class klio.metrics.client.MetricsRegistry(relay_clients, transform_name)

Main client to create and emit metrics.

Parameters

relay_clients (list(klio.metrics.base.AbstractRelayClient)) – configured relay clients.

counter(name, value=0, **kwargs)

Get or create a counter.

Creates a new counter if one is not found with a key of "counter_{name}_{transform}" of the given transform.

New counters will be stored in memory for simple caching.

Parameters
  • name (str) – name of counter

  • value (int) – starting value of counter; defaults to 0

  • kwargs (dict) – keyword arguments passed to each configured relay clients’ counter object.

Returns

instance of a counter dispatcher

Return type

dispatcher.CounterDispatcher

gauge(name, value=0, **kwargs)

Get or create a gauge.

Creates a new gauge if one is not found with a key of “gauge_{name}_{transform}”.

New gauges will be stored in memory for simple caching.

Parameters
  • name (str) – name of gauge

  • value (int) – starting value of gauge; defaults to 0

  • kwargs (dict) – keyword arguments passed to each configured relay clients’ gauge object.

Returns

instance of a gauge dispatcher

Return type

dispatcher.GaugeDispatcher

timer(name, value=0, timer_unit='ns', **kwargs)

Get or create a timer.

Creates a new timer if one is not found with a key of “timer_{name}_{transform}”.

New timers will be stored in memory for simple caching.

Parameters
  • name (str) – name of timer

  • value (int) – starting value of timer; defaults to 0

  • timer_unit (str) – desired unit of time; defaults to ns

  • kwargs (dict) – keyword arguments passed to each configured relay clients’ timer object.

Returns

instance of a timer dispatcher

Return type

dispatcher.TimerDispatcher

marshal(metric)

Create a dictionary-representation of a given metric.

Used when metric objects need to be pickled.

Parameters

metric (dispatcher.BaseMetricDispatcher) – metric instance to marshal.

Returns

the metric’s data in dictionary form

Return type

dict

unmarshal(metric_data)

Create a metric instance based off of a dictionary.

If “type” is not specified or is not one of “counter”, “gauge”, or “timer”, it defaults to a gauge-type metric.

Used when metrics objects need to be unpickled.

Parameters

metric_data (dict) – dictionary-representation of a given metric.

Returns

a dispatcher relevant to metric type.

Return type

dispatcher.BaseMetricDispatcher