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

Snapshot of captured metrics that mimics the dict interface. More...

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)
 
 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.
 
float value_at (self, str path, typing.Optional[typing.Dict] labels=None, *typing.Optional[float] default=None)
 Returns a single metric value at specified path.
 
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 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
 

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 46 of file metrics.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 56 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 71 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 78 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 59 of file metrics.py.

◆ __iter__()

pytest_userver.metrics.MetricsSnapshot.__iter__ (   self)

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

Definition at line 67 of file metrics.py.

◆ __len__()

int pytest_userver.metrics.MetricsSnapshot.__len__ (   self)

Returns count of metrics paths.

Definition at line 63 of file metrics.py.

◆ __repr__()

str pytest_userver.metrics.MetricsSnapshot.__repr__ (   self)

Definition at line 85 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 146 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 161 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 88 of file metrics.py.

◆ items()

pytest_userver.metrics.MetricsSnapshot.items (   self)

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

Definition at line 95 of file metrics.py.

◆ keys()

pytest_userver.metrics.MetricsSnapshot.keys (   self)

Returns an iterable over paths of metrics.

Definition at line 99 of file metrics.py.

◆ to_json()

str pytest_userver.metrics.MetricsSnapshot.to_json (   self)

Serialize to a JSON string.

Definition at line 174 of file metrics.py.

◆ value_at()

float pytest_userver.metrics.MetricsSnapshot.value_at (   self,
str  path,
typing.Optional[typing.Dict]   labels = None,
*typing.Optional[float]   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 107 of file metrics.py.

◆ values()

pytest_userver.metrics.MetricsSnapshot.values (   self)

Returns an iterable over lists of metrics.

Definition at line 103 of file metrics.py.

Member Data Documentation

◆ _values

pytest_userver.metrics.MetricsSnapshot._values
protected

Definition at line 57 of file metrics.py.


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