userver: pytest_userver.client.MetricsDiffer Class Reference
Loading...
Searching...
No Matches
pytest_userver.client.MetricsDiffer Class Reference

Detailed Description

A helper class for computing metric differences.

See also
ClientMonitor.metrics_diff

Example with await monitor_client.metrics_diff():

async def test_diff_metrics(service_client, monitor_client: pytest_userver.client.ClientMonitor):
async with monitor_client.metrics_diff(prefix='sample-metrics') as differ:
# Do something that makes the service update its metrics
response = await service_client.get('/metrics')
assert response.status == 200
# Checking diff of 'sample-metrics.foo' metric
assert differ.value_at('foo') == 1

Definition at line 541 of file client.py.

Public Member Functions

pytest_userver.metrics.MetricsSnapshot baseline (self)
None baseline (self, pytest_userver.metrics.MetricsSnapshot value)
pytest_userver.metrics.MetricsSnapshot current (self)
None current (self, pytest_userver.metrics.MetricsSnapshot value)
pytest_userver.metrics.MetricsSnapshot diff (self)
pytest_userver.metrics.MetricValue value_at (self, str|None subpath=None, dict[str, str]|None add_labels=None)
pytest_userver.metrics.MetricValue|T value_at (self, str|None subpath, dict[str, str]|None add_labels, *, T default)
pytest_userver.metrics.MetricValue|Any value_at (self, str|None subpath=None, dict[str, str]|None add_labels=None, *, Any default=_MISSING)
 Returns a single metric value at the specified path, prepending the path provided at construction.
pytest_userver.metrics.MetricsSnapshot fetch (self)
 Returns metric values from the service without mutating the differ.
None fetch_baseline (self)
 Fetches metric values from the service and stores them as self.baseline.
None fetch_current (self)
 Fetches metric values from the service and stores them as self.current, updating self.diff accordingly.
MetricsDiffer __aenter__ (self)
None __aexit__ (self, exc_type, exc, exc_tb)

Public Attributes

pytest_userver.metrics.MetricsSnapshot baseline = await self.fetch()
pytest_userver.metrics.MetricsSnapshot current = await self.fetch()

Protected Attributes

 _baseline = value
 _diff
 _current = value

Member Function Documentation

◆ __aenter__()

MetricsDiffer pytest_userver.client.MetricsDiffer.__aenter__ ( self)

Definition at line 680 of file client.py.

◆ __aexit__()

None pytest_userver.client.MetricsDiffer.__aexit__ ( self,
exc_type,
exc,
exc_tb )

Definition at line 685 of file client.py.

◆ baseline() [1/2]

pytest_userver.metrics.MetricsSnapshot pytest_userver.client.MetricsDiffer.baseline ( self)

Definition at line 576 of file client.py.

◆ baseline() [2/2]

None pytest_userver.client.MetricsDiffer.baseline ( self,
pytest_userver.metrics.MetricsSnapshot value )

Definition at line 581 of file client.py.

◆ current() [1/2]

pytest_userver.metrics.MetricsSnapshot pytest_userver.client.MetricsDiffer.current ( self)

Definition at line 591 of file client.py.

◆ current() [2/2]

None pytest_userver.client.MetricsDiffer.current ( self,
pytest_userver.metrics.MetricsSnapshot value )

Definition at line 596 of file client.py.

◆ diff()

pytest_userver.metrics.MetricsSnapshot pytest_userver.client.MetricsDiffer.diff ( self)

Definition at line 606 of file client.py.

◆ fetch()

pytest_userver.metrics.MetricsSnapshot pytest_userver.client.MetricsDiffer.fetch ( self)

Returns metric values from the service without mutating the differ.

Definition at line 651 of file client.py.

◆ fetch_baseline()

None pytest_userver.client.MetricsDiffer.fetch_baseline ( self)

Fetches metric values from the service and stores them as self.baseline.

Useful as an alternative to async with monitor_client.metrics_diff(...) when the diffing scope doesn't map cleanly onto a single with block.

Definition at line 662 of file client.py.

◆ fetch_current()

None pytest_userver.client.MetricsDiffer.fetch_current ( self)

Fetches metric values from the service and stores them as self.current, updating self.diff accordingly.

Exceptions
AssertionErrorif self.baseline was not set first

Definition at line 671 of file client.py.

◆ value_at() [1/3]

pytest_userver.metrics.MetricValue | T pytest_userver.client.MetricsDiffer.value_at ( self,
str | None subpath,
dict[str, str] | None add_labels,
* ,
T default )

Definition at line 618 of file client.py.

◆ value_at() [2/3]

pytest_userver.metrics.MetricValue pytest_userver.client.MetricsDiffer.value_at ( self,
str | None subpath = None,
dict[str, str] | None add_labels = None )

Definition at line 611 of file client.py.

◆ value_at() [3/3]

pytest_userver.metrics.MetricValue | Any pytest_userver.client.MetricsDiffer.value_at ( self,
str | None subpath = None,
dict[str, str] | None add_labels = None,
* ,
Any default = _MISSING )

Returns a single metric value at the specified path, prepending the path provided at construction.

If a dict of labels is provided, does an exact match of labels, prepending the labels provided at construction.

If default is provided, it is returned instead of asserting when the metric is not found.

Parameters
subpathSuffix of the metric path; the path provided at construction is prepended
add_labelsLabels that the metric must have in addition to the labels provided at construction
defaultAn optional default value in case the metric is missing
Exceptions
AssertionErrorif not one metric by path and no default is given

Definition at line 626 of file client.py.

Member Data Documentation

◆ _baseline

pytest_userver.client.MetricsDiffer._baseline = value
protected

Definition at line 582 of file client.py.

◆ _current

pytest_userver.client.MetricsDiffer._current = value
protected

Definition at line 597 of file client.py.

◆ _diff

pytest_userver.client.MetricsDiffer._diff
protected
Initial value:
= _subtract_metrics_snapshots(
self._current,
self._baseline,
self._diff_gauge,
)

Definition at line 584 of file client.py.

◆ baseline

pytest_userver.metrics.MetricsSnapshot pytest_userver.client.MetricsDiffer.baseline = await self.fetch()

Definition at line 669 of file client.py.

◆ current

pytest_userver.metrics.MetricsSnapshot pytest_userver.client.MetricsDiffer.current = await self.fetch()

Definition at line 678 of file client.py.


The documentation for this class was generated from the following file:
  • /data/code/userver/testsuite/pytest_plugins/pytest_userver/client.py