userver: pytest_userver.metrics.MetricsSnapshot Class Reference
Loading...
Searching...
No Matches
pytest_userver.metrics.MetricsSnapshot Class Reference

Detailed Description

Snapshot of captured metrics that mimics the dict interface.

Metrics have the 'Dict[str(path), Set[Metric]]' format.

async def test_engine_metrics(service_client, monitor_client):
metric = await monitor_client.single_metric(
'engine.task-processors.tasks.finished',
labels={'task_processor': 'main-task-processor'},
)
assert metric.value > 0
assert metric.labels == {'task_processor': 'main-task-processor'}
metrics_dict = await monitor_client.metrics(
prefix='http.', labels={'http_path': '/ping'},
)
assert metrics_dict
assert 'http.handler.cancelled-by-deadline' in metrics_dict
assert (
metrics_dict.value_at(
'http.handler.in-flight',
labels={
'http_path': '/ping',
'http_handler': 'handler-ping',
'version': '2',
},
)
== 0
)

Definition at line 132 of file metrics.py.

Public Member Functions

 __init__ (self, typing.Mapping[str, typing.Set[Metric]] values)
 
typing.Set[Metric__getitem__ (self, str path)
 Returns a list of metrics by specified path.
 
int __len__ (self)
 Returns count of metrics paths.
 
 __iter__ (self)
 Returns a (path, list) iterable over the metrics.
 
bool __contains__ (self, str path)
 Returns True if metric with specified path is in the snapshot, False otherwise.
 
bool __eq__ (self, object other)
 Compares the snapshot with a dict of metrics or with another snapshot.
 
str __repr__ (self)
 
str __str__ (self)
 
 get (self, str path, default=None)
 Returns an list of metrics by path or default if there's no such path.
 
 items (self)
 Returns a (path, list) iterable over the metrics.
 
 keys (self)
 Returns an iterable over paths of metrics.
 
 values (self)
 Returns an iterable over lists of metrics.
 
MetricValue value_at (self, str path, typing.Optional[typing.Dict] labels=None, *typing.Optional[MetricValue] default=None)
 Returns a single metric value at specified path.
 
typing.List[Metricmetrics_at (self, str path, typing.Optional[typing.Dict] require_labels=None)
 Metrics path must exactly equal the given path.
 
bool has_metrics_at (self, str path, typing.Optional[typing.Dict] require_labels=None)
 
None assert_equals (self, typing.Mapping[str, typing.Set[Metric]] other, *bool ignore_zeros=False)
 Compares the snapshot with a dict of metrics or with another snapshot, displaying a nice diff on mismatch.
 
str pretty_print (self)
 Multiline linear print: path: (label=value),(label=value) TYPE VALUE path: (label=value),(label=value) TYPE VALUE Usage:
 
str to_json (self)
 Serialize to a JSON string.
 

Static Public Member Functions

'MetricsSnapshotfrom_json (str json_str)
 Construct MetricsSnapshot from a JSON string.
 

Protected Attributes

 _values
 

Constructor & Destructor Documentation

◆ __init__()

pytest_userver.metrics.MetricsSnapshot.__init__ ( self,
typing.Mapping[str, typing.Set[Metric]] values )

Definition at line 142 of file metrics.py.

Member Function Documentation

◆ __contains__()

bool pytest_userver.metrics.MetricsSnapshot.__contains__ ( self,
str path )

Returns True if metric with specified path is in the snapshot, False otherwise.

Definition at line 157 of file metrics.py.

◆ __eq__()

bool pytest_userver.metrics.MetricsSnapshot.__eq__ ( self,
object other )

Compares the snapshot with a dict of metrics or with another snapshot.

Definition at line 164 of file metrics.py.

◆ __getitem__()

typing.Set[Metric] pytest_userver.metrics.MetricsSnapshot.__getitem__ ( self,
str path )

Returns a list of metrics by specified path.

Definition at line 145 of file metrics.py.

◆ __iter__()

pytest_userver.metrics.MetricsSnapshot.__iter__ ( self)

Returns a (path, list) iterable over the metrics.

Definition at line 153 of file metrics.py.

◆ __len__()

int pytest_userver.metrics.MetricsSnapshot.__len__ ( self)

Returns count of metrics paths.

Definition at line 149 of file metrics.py.

◆ __repr__()

str pytest_userver.metrics.MetricsSnapshot.__repr__ ( self)

Definition at line 171 of file metrics.py.

◆ __str__()

str pytest_userver.metrics.MetricsSnapshot.__str__ ( self)

Definition at line 174 of file metrics.py.

◆ assert_equals()

None pytest_userver.metrics.MetricsSnapshot.assert_equals ( self,
typing.Mapping[str, typing.Set[Metric]] other,
*bool ignore_zeros = False )

Compares the snapshot with a dict of metrics or with another snapshot, displaying a nice diff on mismatch.

Definition at line 282 of file metrics.py.

◆ from_json()

'MetricsSnapshot' pytest_userver.metrics.MetricsSnapshot.from_json ( str json_str)
static

Construct MetricsSnapshot from a JSON string.

Definition at line 337 of file metrics.py.

◆ get()

pytest_userver.metrics.MetricsSnapshot.get ( self,
str path,
default = None )

Returns an list of metrics by path or default if there's no such path.

Definition at line 177 of file metrics.py.

◆ has_metrics_at()

bool pytest_userver.metrics.MetricsSnapshot.has_metrics_at ( self,
str path,
typing.Optional[typing.Dict] require_labels = None )

Definition at line 275 of file metrics.py.

◆ items()

pytest_userver.metrics.MetricsSnapshot.items ( self)

Returns a (path, list) iterable over the metrics.

Definition at line 184 of file metrics.py.

◆ keys()

pytest_userver.metrics.MetricsSnapshot.keys ( self)

Returns an iterable over paths of metrics.

Definition at line 188 of file metrics.py.

◆ metrics_at()

typing.List[Metric] pytest_userver.metrics.MetricsSnapshot.metrics_at ( self,
str path,
typing.Optional[typing.Dict] require_labels = None )

Metrics path must exactly equal the given path.

A required subset of labels is specified by require_labels Example: require_labels={'a':'b', 'c':'d'} { 'a':'b', 'c':'d'} - exact match { 'a':'b', 'c':'d', 'e': 'f', 'h':'k'} - match { 'a':'x', 'c':'d'} - no match, incorrect value for label 'a' { 'a' : 'b'} - required label not found Usage:

for m in metrics_with_labels(path='something.something.sensor',
require_labels={ 'label1': 'value1' }):
assert m.value > 0

Definition at line 235 of file metrics.py.

◆ pretty_print()

str pytest_userver.metrics.MetricsSnapshot.pretty_print ( self)

Multiline linear print: path: (label=value),(label=value) TYPE VALUE path: (label=value),(label=value) TYPE VALUE Usage:

assert 'some.thing.sensor' in metric, metric.pretty_print()

Definition at line 296 of file metrics.py.

◆ to_json()

str pytest_userver.metrics.MetricsSnapshot.to_json ( self)

Serialize to a JSON string.

Definition at line 354 of file metrics.py.

◆ value_at()

MetricValue pytest_userver.metrics.MetricsSnapshot.value_at ( self,
str path,
typing.Optional[typing.Dict] labels = None,
*typing.Optional[MetricValue] default = None )

Returns a single metric value at specified path.

If a dict of labels is provided, does en exact match of labels (i.e. {} stands for no labels; {'a': 'b', 'c': 'd'} matches only {'a': 'b', 'c': 'd'} or {'c': 'd', 'a': 'b'} but neither match {'a': 'b'} nor {'a': 'b', 'c': 'd', 'e': 'f'}).

Exceptions
AssertionErrorif not one metric by path

Definition at line 196 of file metrics.py.

◆ values()

pytest_userver.metrics.MetricsSnapshot.values ( self)

Returns an iterable over lists of metrics.

Definition at line 192 of file metrics.py.

Member Data Documentation

◆ _values

pytest_userver.metrics.MetricsSnapshot._values
protected

Definition at line 143 of file metrics.py.


The documentation for this class was generated from the following file: