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.

Example with await monitor_client.metrics(path_prefix, labels):

async def test_engine_logger_metrics(service_client, monitor_client: pytest_userver.client.ClientMonitor):
metrics_dict: pytest_userver.metrics.MetricsSnapshot = await monitor_client.metrics(
prefix='logger.',
labels={'logger': 'default'},
)
assert metrics_dict
assert 'logger.total' in metrics_dict
assert metrics_dict.value_at('logger.total') > 0
assert (
metrics_dict.value_at(
'logger.dropped',
labels={
'logger': 'default',
'version': '2',
},
)
== 0
)

Definition at line 130 of file metrics.py.

Public Member Functions

 __init__ (self, Mapping[str, Set[Metric]] values)
 
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, dict[str, str]|None labels=None, *, MetricValue|None default=None)
 Returns a single metric value at specified path.
 
list[Metricmetrics_at (self, str path, dict[str, str]|None require_labels=None)
 Metrics path must exactly equal the given path.
 
bool has_metrics_at (self, str path, dict[str, str]|None require_labels=None)
 
None assert_equals (self, Mapping[str, 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

MetricsSnapshot from_json (str json_str)
 Construct MetricsSnapshot from a JSON string.
 

Protected Attributes

 _values = values
 

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 141 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 156 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 162 of file metrics.py.

◆ __getitem__()

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

Returns a list of metrics by specified path.

Definition at line 144 of file metrics.py.

◆ __iter__()

pytest_userver.metrics.MetricsSnapshot.__iter__ ( self)

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

Definition at line 152 of file metrics.py.

◆ __len__()

int pytest_userver.metrics.MetricsSnapshot.__len__ ( self)

Returns count of metrics paths.

Definition at line 148 of file metrics.py.

◆ __repr__()

str pytest_userver.metrics.MetricsSnapshot.__repr__ ( self)

Definition at line 168 of file metrics.py.

◆ __str__()

str pytest_userver.metrics.MetricsSnapshot.__str__ ( self)

Definition at line 171 of file metrics.py.

◆ assert_equals()

None pytest_userver.metrics.MetricsSnapshot.assert_equals ( self,
Mapping[str, 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 276 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 325 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 174 of file metrics.py.

◆ has_metrics_at()

bool pytest_userver.metrics.MetricsSnapshot.has_metrics_at ( self,
str path,
dict[str, str] | None require_labels = None )

Definition at line 267 of file metrics.py.

◆ items()

pytest_userver.metrics.MetricsSnapshot.items ( self)

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

Definition at line 180 of file metrics.py.

◆ keys()

pytest_userver.metrics.MetricsSnapshot.keys ( self)

Returns an iterable over paths of metrics.

Definition at line 184 of file metrics.py.

◆ metrics_at()

list[Metric] pytest_userver.metrics.MetricsSnapshot.metrics_at ( self,
str path,
dict[str, str] | None 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

async def test_engine_logger_metrics(service_client, monitor_client: pytest_userver.client.ClientMonitor):
metrics_dict: pytest_userver.metrics.MetricsSnapshot = await monitor_client.metrics(
prefix='logger.',
labels={'logger': 'default'},
)
assert metrics_dict
assert 'logger.total' in metrics_dict
assert metrics_dict.value_at('logger.total') > 0
assert (
metrics_dict.value_at(
'logger.dropped',
labels={
'logger': 'default',
'version': '2',
},
)
== 0
)

Definition at line 228 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 290 of file metrics.py.

◆ to_json()

str pytest_userver.metrics.MetricsSnapshot.to_json ( self)

Serialize to a JSON string.

Definition at line 342 of file metrics.py.

◆ value_at()

MetricValue pytest_userver.metrics.MetricsSnapshot.value_at ( self,
str path,
dict[str, str] | None labels = None,
* ,
MetricValue | None 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
async def test_engine_logger_metrics(service_client, monitor_client: pytest_userver.client.ClientMonitor):
metrics_dict: pytest_userver.metrics.MetricsSnapshot = await monitor_client.metrics(
prefix='logger.',
labels={'logger': 'default'},
)
assert metrics_dict
assert 'logger.total' in metrics_dict
assert metrics_dict.value_at('logger.total') > 0
assert (
metrics_dict.value_at(
'logger.dropped',
labels={
'logger': 'default',
'version': '2',
},
)
== 0
)

Definition at line 192 of file metrics.py.

◆ values()

pytest_userver.metrics.MetricsSnapshot.values ( self)

Returns an iterable over lists of metrics.

Definition at line 188 of file metrics.py.

Member Data Documentation

◆ _values

pytest_userver.metrics.MetricsSnapshot._values = values
protected

Definition at line 142 of file metrics.py.


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