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):
There are 3 ways to construct a MetricsSnapshot:
Definition at line 135 of file metrics.py.
Public Member Functions | |
| __init__ (self, Mapping[str, Set[Metric]] values, *, str common_prefix='', Mapping[str, str]|None common_labels=None) | |
| 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. | |
| MetricsSnapshot | sliced (self, str|None prefix, dict[str, str]|None labels=None) |
| Returns a new MetricsSnapshot restricted to the metrics whose path starts with prefix as a whole '. | |
| MetricsSnapshot | unsliced (self) |
| Returns a new MetricsSnapshot with the prefix accumulated from the preceding (possibly chained) sliced() call(s) prepended back to every surviving metric's path. | |
| MetricValue | value_at (self, str path, dict[str, str]|None labels=None) |
| MetricValue|T | value_at (self, str path, dict[str, str]|None labels, *, T default) |
| MetricValue|Any | value_at (self, str path, dict[str, str]|None labels=None, *, Any default=_MISSING) |
| Returns a single metric value at specified path. | |
| list[Metric] | metrics_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) |
| MetricsSnapshot | without_zero_rates (self) |
| Returns a new snapshot with "empty" RATE and HIST_RATE metrics removed: a RATE metric is removed if its value is zero, a HIST_RATE metric is removed if its histogram has zero count in every bucket and in inf. | |
| 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_dict (Mapping[str, Any] data) |
| Construct MetricsSnapshot from a JSON dict in the json userver metrics format. | |
| MetricsSnapshot | from_json (str json_str) |
| Construct MetricsSnapshot from a JSON string in the json userver metrics format. | |
| MetricsSnapshot | from_layered_dict (Mapping[str, Any] data, *, str common_prefix='', Mapping[str, str]|None common_labels=None) |
| Construct MetricsSnapshot from a layered dict format that avoids repeating a label's name for every metric that only differs by that label's value. | |
Protected Attributes | |
| _values = _apply_common_prefix_labels(values, common_prefix, common_labels) | |
| str|None | _sliced_prefix = None |
| dict | _sliced_labels = {} |
| pytest_userver.metrics.MetricsSnapshot.__init__ | ( | self, | |
| Mapping[str, Set[Metric]] | values, | ||
| * | , | ||
| str | common_prefix = '', | ||
| Mapping[str, str] | None | common_labels = None ) |
| values | Metrics keyed by path, as a dict[str(path), Set[Metric]] (the same format MetricsSnapshot itself exposes via items()). |
| common_prefix | If provided, prepended to each path (separated by a dot). |
| common_labels | If provided, these labels are added to every metric, merged with (and overridden by) that metric's own labels. |
Definition at line 160 of file metrics.py.
| 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 190 of file metrics.py.
| bool pytest_userver.metrics.MetricsSnapshot.__eq__ | ( | self, | |
| object | other ) |
Compares the snapshot with a dict of metrics or with another snapshot.
A path mapped to an empty set of metrics is treated the same as an absent path.
Definition at line 196 of file metrics.py.
| Set[Metric] pytest_userver.metrics.MetricsSnapshot.__getitem__ | ( | self, | |
| str | path ) |
Returns a list of metrics by specified path.
Definition at line 178 of file metrics.py.
| pytest_userver.metrics.MetricsSnapshot.__iter__ | ( | self | ) |
Returns a (path, list) iterable over the metrics.
Definition at line 186 of file metrics.py.
| int pytest_userver.metrics.MetricsSnapshot.__len__ | ( | self | ) |
Returns count of metrics paths.
Definition at line 182 of file metrics.py.
| str pytest_userver.metrics.MetricsSnapshot.__repr__ | ( | self | ) |
Definition at line 210 of file metrics.py.
| str pytest_userver.metrics.MetricsSnapshot.__str__ | ( | self | ) |
Definition at line 213 of file metrics.py.
| None pytest_userver.metrics.MetricsSnapshot.assert_equals | ( | self, | |
| Mapping[str, Set[Metric]] | other, | ||
| * | , | ||
| bool | ignore_zeros = False ) |
Definition at line 412 of file metrics.py.
|
static |
Construct MetricsSnapshot from a JSON dict in the json userver metrics format.
Definition at line 469 of file metrics.py.
|
static |
Construct MetricsSnapshot from a JSON string in the json userver metrics format.
Definition at line 487 of file metrics.py.
|
static |
Construct MetricsSnapshot from a layered dict format that avoids repeating a label's name for every metric that only differs by that label's value.
Top-level keys of data are metric paths, used as-is. Within a path's value, each dict key names a label as 'name = value' (with exactly one space on each side of =: everything before is the label name, everything after is its value); the corresponding child value is interpreted the same way recursively, so several labels can be layered one inside another. A dict with bounds and buckets keys is a leaf value instead of being recursed into, parsed as a Histogram; any other non-dict value is a plain leaf metric value.
If common_prefix is provided, it is prepended to each path (separated by a dot) so that paths in data can omit a shared prefix.
If common_labels is provided, these labels are added to every metric in the snapshot, merged with any labels from the layered dict structure.
Example: {'a': {'x = foo': 1, 'x = bar': 2}} is equivalent to MetricsSnapshot({'a': {Metric({'x': 'foo'}, 1), Metric({'x': 'bar'}, 2)}}).
Definition at line 494 of file metrics.py.
| 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 216 of file metrics.py.
| bool pytest_userver.metrics.MetricsSnapshot.has_metrics_at | ( | self, | |
| str | path, | ||
| dict[str, str] | None | require_labels = None ) |
Definition at line 403 of file metrics.py.
| pytest_userver.metrics.MetricsSnapshot.items | ( | self | ) |
Returns a (path, list) iterable over the metrics.
Definition at line 222 of file metrics.py.
| pytest_userver.metrics.MetricsSnapshot.keys | ( | self | ) |
Returns an iterable over paths of metrics.
Definition at line 226 of file metrics.py.
| 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
Definition at line 376 of file metrics.py.
| 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:
Definition at line 442 of file metrics.py.
| MetricsSnapshot pytest_userver.metrics.MetricsSnapshot.sliced | ( | self, | |
| str | None | prefix, | ||
| dict[str, str] | None | labels = None ) |
Returns a new MetricsSnapshot restricted to the metrics whose path starts with prefix as a whole '.
'-separated segment, and, if labels is given, whose labels are a superset of labels (same subset-match semantics as require_labels in metrics_at).
prefix may be:
Calling sliced() several times in a row composes: each prefix is matched against the already-stripped paths of the previous sliced() call, and labels requirements accumulate.
Slicing only ever affects filtering (which metrics are visible, and under which path): it never touches the Metric objects themselves. metrics_at(), value_at() and iteration over a sliced snapshot all keep returning the exact same, untouched Metric objects (same labels, value, identity), just possibly under a shorter path and/or a smaller surrounding set.
Intended use: carve out a small, closed slice of a snapshot (e.g. one metric path with a handful of varying labels) to compare it with == against a compact expected snapshot, or to look up several label combinations with value_at/metrics_at without repeating the common prefix and labels in every call.
| AssertionError | if stripping prefix from a metric path would leave an empty remainder, e.g. path 'a.b.' sliced by prefix='a.b', which would be indistinguishable from path 'a.b'. |
Definition at line 234 of file metrics.py.
| str pytest_userver.metrics.MetricsSnapshot.to_json | ( | self | ) |
Serialize to a JSON string.
Definition at line 531 of file metrics.py.
| MetricsSnapshot pytest_userver.metrics.MetricsSnapshot.unsliced | ( | self | ) |
Returns a new MetricsSnapshot with the prefix accumulated from the preceding (possibly chained) sliced() call(s) prepended back to every surviving metric's path.
Does not mutate self.
Metrics that were filtered out by sliced() (because their path did not match prefix, or their labels did not match labels) do NOT come back: unsliced() only restores the path of what remains in the snapshot, it does not undo the filtering itself.
If this snapshot was never sliced() (i.e. self is the original snapshot, or the result of operations other than sliced()), returns an equivalent snapshot unchanged.
Definition at line 300 of file metrics.py.
| MetricValue | T pytest_userver.metrics.MetricsSnapshot.value_at | ( | self, | |
| str | path, | ||
| dict[str, str] | None | labels, | ||
| * | , | ||
| T | default ) |
Definition at line 326 of file metrics.py.
| MetricValue pytest_userver.metrics.MetricsSnapshot.value_at | ( | self, | |
| str | path, | ||
| dict[str, str] | None | labels = None ) |
Definition at line 319 of file metrics.py.
| MetricValue | Any pytest_userver.metrics.MetricsSnapshot.value_at | ( | self, | |
| str | path, | ||
| dict[str, str] | None | labels = None, | ||
| * | , | ||
| Any | default = _MISSING ) |
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'}).
If default is provided, it is returned instead of asserting when the metric is not found.
| AssertionError | if not one metric by path and no default is given |
Definition at line 334 of file metrics.py.
| pytest_userver.metrics.MetricsSnapshot.values | ( | self | ) |
Returns an iterable over lists of metrics.
Definition at line 230 of file metrics.py.
| MetricsSnapshot pytest_userver.metrics.MetricsSnapshot.without_zero_rates | ( | self | ) |
Returns a new snapshot with "empty" RATE and HIST_RATE metrics removed: a RATE metric is removed if its value is zero, a HIST_RATE metric is removed if its histogram has zero count in every bucket and in inf.
GAUGE (and untyped) metrics are kept as-is, because a zero GAUGE value can be meaningful.
Definition at line 427 of file metrics.py.
|
protected |
Definition at line 176 of file metrics.py.
|
protected |
Definition at line 175 of file metrics.py.
|
protected |
Definition at line 174 of file metrics.py.