Skip to content

Create new metrics¤

Example

class MyApplication(asab.Application):
    async def initialize(self):
        from asab.metrics import Module # (1)!
        self.add_module(Module)
        self.MetricsService = self.get_service('asab.MetricsService') # (2)!
        self.MyCounter = self.MetricsService.create_counter("mycounter", tags={'foo': 'bar'}, init_values={'v1': 0, 'v2': 0}) # (3)!

if __name__ == '__main__':
    app = MyApplication()
    app.run()
  1. Import the asab.metrics module and add it to the application.
  2. Then, you can localize MetricsService.
  3. Use MetricsService to initialize the counter.

See the full example here.

Warning

All methods that create new metrics objects can be found in the Metrics Service reference. You should never initiate a new metrics object on its own, but always through Metrics Service. Metris initialization is meant to be done in the init time of your application and should not be done during runtime.

asab.metrics.service.MetricsService ¤

Bases: Service

create_gauge(metric_name, tags=None, init_values=None, help=None, unit=None) ¤

The create_gauge function creates and returns a Gauge metric with specified parameters.

Parameters:

Name Type Description Default
metric_name str

The name of the metric you want to create.

required
tags dict

Dictionary where the keys represent the tag names and the values represent the tag values. It allows you to categorize and filter metrics based on different dimensions or attributes.

None
init_values dict

The init_values parameter is used to set the initial value of the gauge metric. Provide pairs of value names and their initial values. If not provided, the gauge metric will be initialized with a value of 0.

None
help str

The "help" parameter is used to provide a description or explanation of the metric. It can be used to provide additional information about what the metric measures or how it should be interpreted.

None
unit str

The "unit" parameter is used to specify the unit of measurement for the metric. It helps provide context and understanding for the metric being measured. For example, if the metric is measuring the temperature, the unit could be "degrees Celsius".

None

Returns:

Type Description

an instance of the Gauge class.

Raises:

Type Description
AssertionError

tags dictionary has to be of type 'str': 'str'.

create_counter(metric_name, tags=None, init_values=None, reset=True, help=None, unit=None, dynamic_tags=False) ¤

The function creates a counter metric with optional dynamic tags and adds it to a metric collection.

Parameters:

Name Type Description Default
metric_name str

The name of the metric you want to create.

required
tags dict

Dictionary where the keys represent the tag names and the values represent the tag values. It allows you to categorize and filter metrics based on different dimensions or attributes.

None
init_values dict

The init_values parameter is used to set the initial value of the metric. Provide pairs of value names and their initial values. If not provided, the metric will be initialized with a value of 0.

None
reset bool

The "reset" parameter is a boolean value that determines whether the counter should be reset to initial values every 60 seconds. Defaults to True

True
help str

The "help" parameter is used to provide a description or explanation of the metric. It can be used to provide additional information about what the metric measures or how it should be interpreted.

None
unit str

The "unit" parameter is used to specify the unit of measurement for the metric. It helps provide context and understanding for the metric being measured. For example, if the metric is measuring the temperature, the unit could be "degrees Celsius".

None
dynamic_tags bool

Boolean flag. If set to True, the counter will be an instance of the "CounterWithDynamicTags" class, which allows tags to be added or removed dynamically. Defaults to False

False

Returns:

Type Description

the created counter object.

Raises:

Type Description
AssertionError

tags dictionary has to be of type 'str': 'str'.

create_eps_counter(metric_name, tags=None, init_values=None, reset=True, help=None, unit=None) ¤

The function creates an EPSCounter object, and returns the object.

Parameters:

Name Type Description Default
metric_name str

The name of the metric you want to create.

required
tags dict

Dictionary where the keys represent the tag names and the values represent the tag values. It allows you to categorize and filter metrics based on different dimensions or attributes.

None
init_values dict

The init_values parameter is used to set the initial value of the metric. Provide pairs of value names and their initial values. If not provided, the metric will be initialized with a value of 0.

None
reset bool

The "reset" parameter is a boolean value that determines whether the counter should be reset to initial values every 60 seconds. Defaults to True

True
help str

The "help" parameter is used to provide a description or explanation of the metric. It can be used to provide additional information about what the metric measures or how it should be interpreted.

None
unit str

The "unit" parameter is used to specify the unit of measurement for the metric. It helps provide context and understanding for the metric being measured. For example, if the metric is measuring the temperature, the unit could be "degrees Celsius".

None

Returns:

Type Description

an instance of the EPSCounter class.

Raises:

Type Description
AssertionError

tags dictionary has to be of type 'str': 'str'.

create_duty_cycle(metric_name, tags=None, init_values=None, help=None, unit=None) ¤

The function creates a duty cycle metric and returns the object.

Parameters:

Name Type Description Default
metric_name str

The name of the metric you want to create.

required
tags dict

Dictionary where the keys represent the tag names and the values represent the tag values. It allows you to categorize and filter metrics based on different dimensions or attributes.

None
init_values dict

The init_values parameter is used to set the initial value of the metric. Provide pairs of value names and their initial values. If not provided, the metric will be initialized with a value of 0.

None
help str

The "help" parameter is used to provide a description or explanation of the metric. It can be used to provide additional information about what the metric measures or how it should be interpreted.

None
unit str

The "unit" parameter is used to specify the unit of measurement for the metric. It helps provide context and understanding for the metric being measured. For example, if the metric is measuring the temperature, the unit could be "degrees Celsius".

None

Returns:

Type Description

an instance of the DutyCycle class.

Raises:

Type Description
AssertionError

tags dictionary has to be of type 'str': 'str'.

create_aggregation_counter(metric_name, tags=None, init_values=None, reset=True, aggregator=max, help=None, unit=None, dynamic_tags=False) ¤

The function creates a counter metric with optional dynamic tags and adds it to a metric collection.

Parameters:

Name Type Description Default
metric_name str

The name of the metric you want to create.

required
tags dict

Dictionary where the keys represent the tag names and the values represent the tag values. It allows you to categorize and filter metrics based on different dimensions or attributes.

None
init_values dict

The init_values parameter is used to set the initial value of the metric. Provide pairs of value names and their initial values. If not provided, the metric will be initialized with a value of 0.

None
reset bool

The "reset" parameter is a boolean value that determines whether the counter should be reset to initial values every 60 seconds. Defaults to True

True
help str

The "help" parameter is used to provide a description or explanation of the metric. It can be used to provide additional information about what the metric measures or how it should be interpreted.

None
unit str

The "unit" parameter is used to specify the unit of measurement for the metric. It helps provide context and understanding for the metric being measured. For example, if the metric is measuring the temperature, the unit could be "degrees Celsius".

None
dynamic_tags

Boolean flag. If set to True, the counter will be an instance of the "AggregationCounterWithDynamicTags" class, which allows tags to be added or removed dynamically. Defaults to False

False

Returns:

Type Description

the created counter object.

Raises:

Type Description
AssertionError

tags dictionary has to be of type 'str': 'str'.

create_histogram(metric_name, buckets, tags=None, init_values=None, reset=True, help=None, unit=None, dynamic_tags=False) ¤

The function creates a histogram metric.

Parameters:

Name Type Description Default
metric_name str

The name of the metric you want to create.

required
buckets list

The "buckets" parameter is a list that specifies the boundaries for the histogram

required
is [10, 20, 30], it means that the histogram will have three buckets

one for values less than 10, second for values less than 20, third for values less than 30

required
tags dict

Dictionary where the keys represent the tag names and the values represent the tag values. It allows you to categorize and filter metrics based on different dimensions or attributes.

None
init_values dict

The init_values parameter is used to set the initial value of the metric. Provide pairs of value names and their initial values. If not provided, the metric will be initialized with a value of 0.

None
reset bool

The "reset" parameter is a boolean value that determines whether the histogram should be reset to initial values every 60 seconds. Defaults to True

True
help str

The "help" parameter is used to provide a description or explanation of the metric. It can be used to provide additional information about what the metric measures or how it should be interpreted.

None
unit str

The "unit" parameter is used to specify the unit of measurement for the metric. It helps provide context and understanding for the metric being measured. For example, if the metric is measuring the temperature, the unit could be "degrees Celsius".

None
dynamic_tags

Boolean flag. If set to True, the counter will be an instance of the "AggregationCounterWithDynamicTags" class, which allows tags to be added or removed dynamically. Defaults to False

False

Returns:

Type Description

a histogram object

Raises:

Type Description
AssertionError

tags dictionary has to be of type 'str': 'str'.