Advanced usage¶
Measuring the execution of a block of code¶
The metric_block
method can be used as context manager to retrieve metrics on the execution of a
block of code.
with client.block_metric("foo"):
...
This sample of code will send the following metrics (before formatting is applied):
- foo.executions with a value of 1
- If an exception was raised
- foo.errors with a value of 1
- Else
- foo.processing_time with a value representing the time the block took to execute
The responsability is yours to agregate the metrics, for example with carbon-aggregate.
Measuring the execution of a function¶
You can retrieve the same metrics for a function using the
decorator
method of
the client.
@cient.decorator
def foo():
pass
The following form, without argument, will use the name of the function as a base metric name. In this case, it will be foo.executions and so on.
@client.decorator("bar")
def foo():
pass
This second sample will send the metrics with the base metric name bar.