userver: pytest_userver.metrics Namespace Reference
Loading...
Searching...
No Matches
pytest_userver.metrics Namespace Reference

Detailed Description

Python module that provides helpers for functional testing of metrics with testsuite; see Functional service tests (testsuite) for an introduction.

Classes

class  Histogram
 Represents the value of a HIST_RATE (a.k.a. More...
class  Metric
 Metric type that contains the labels: dict[str, str] and value: int. More...
class  _MetricsJSONEncoder
class  MetricsSnapshot
 Snapshot of captured metrics that mimics the dict interface. More...

Functions

Mapping[str, Set[Metric]] _apply_common_prefix_labels (Mapping[str, Set[Metric]] values, str common_prefix, Mapping[str, str]|None common_labels)
dict[str, Set[Metric]] _drop_empty_paths (Mapping[str, Set[Metric]] values)
str|None _strip_prefix_segment (str path, str prefix)
bool _is_zero_rate_or_histogram (Metric metric)
bool _type_eq (MetricType lhs, MetricType rhs)
tuple[tuple[str, str],...] _get_labels_tuple (Metric metric)
 Returns labels as a tuple of sorted items.
str _format_metric_line (str path, Metric metric, *, MetricType|None forced_type=None)
 Formats a single metric line as "path: (labels) TYPE VALUE", skipping the labels part (and the extra space) entirely if there are no labels.
set[str] stringify_snapshot_for_diff (Mapping[str, Set[Metric]] values, *, Mapping[str, Set[Metric]] other)
 Renders a snapshot as a set of "path: (labels) TYPE VALUE" strings (the same format as MetricsSnapshot.pretty_print), suitable for use with a generic set-based diff (e.g.
MetricType _resolve_type (str path, Metric metric, MetricsSnapshot other_snapshot)
float _do_compute_percentile (Histogram hist, float percent)
bool _is_histogram_dict (Any node)
set[Metric_collect_layered_metrics (Any node, dict[str, str] labels)
MetricValue _parse_metric_value (Any value)
_FlattenedSnapshot _flatten_snapshot (values, bool ignore_zeros)
str _diff_metric_snapshots (_FlattenedSnapshot lhs, _FlattenedSnapshot rhs, bool ignore_zeros)

Variables

 T = TypeVar('T')
Any _MISSING = object()
str _LABEL_SEPARATOR = ' = '
TypeAlias _FlattenedSnapshot = Set[tuple[str, Metric]]

Function Documentation

◆ _apply_common_prefix_labels()

Mapping[str, Set[Metric]] _apply_common_prefix_labels ( Mapping[str, Set[Metric]] values,
str common_prefix,
Mapping[str, str] | None common_labels )
protected

Definition at line 542 of file metrics.py.

◆ _collect_layered_metrics()

set[Metric] _collect_layered_metrics ( Any node,
dict[str, str] labels )
protected

Definition at line 670 of file metrics.py.

◆ _diff_metric_snapshots()

str _diff_metric_snapshots ( _FlattenedSnapshot lhs,
_FlattenedSnapshot rhs,
bool ignore_zeros )
protected

Definition at line 711 of file metrics.py.

◆ _do_compute_percentile()

float _do_compute_percentile ( Histogram hist,
float percent )
protected

Definition at line 641 of file metrics.py.

◆ _drop_empty_paths()

dict[str, Set[Metric]] _drop_empty_paths ( Mapping[str, Set[Metric]] values)
protected

Definition at line 560 of file metrics.py.

◆ _flatten_snapshot()

_FlattenedSnapshot _flatten_snapshot ( values,
bool ignore_zeros )
protected

Definition at line 702 of file metrics.py.

◆ _format_metric_line()

str _format_metric_line ( str path,
Metric metric,
* ,
MetricType | None forced_type = None )
protected

Formats a single metric line as "path: (labels) TYPE VALUE", skipping the labels part (and the extra space) entirely if there are no labels.

forced_type overrides metric's own type, if given

Definition at line 599 of file metrics.py.

◆ _get_labels_tuple()

tuple[tuple[str, str], ...] _get_labels_tuple ( Metric metric)
protected

Returns labels as a tuple of sorted items.

Definition at line 594 of file metrics.py.

◆ _is_histogram_dict()

bool _is_histogram_dict ( Any node)
protected

Definition at line 663 of file metrics.py.

◆ _is_zero_rate_or_histogram()

bool _is_zero_rate_or_histogram ( Metric metric)
protected

Definition at line 584 of file metrics.py.

◆ _parse_metric_value()

MetricValue _parse_metric_value ( Any value)
protected

Definition at line 684 of file metrics.py.

◆ _resolve_type()

MetricType _resolve_type ( str path,
Metric metric,
MetricsSnapshot other_snapshot )
protected

Definition at line 632 of file metrics.py.

◆ _strip_prefix_segment()

str | None _strip_prefix_segment ( str path,
str prefix )
protected

Definition at line 564 of file metrics.py.

◆ _type_eq()

bool _type_eq ( MetricType lhs,
MetricType rhs )
protected

Definition at line 590 of file metrics.py.

◆ stringify_snapshot_for_diff()

set[str] stringify_snapshot_for_diff ( Mapping[str, Set[Metric]] values,
* ,
Mapping[str, Set[Metric]] other )

Renders a snapshot as a set of "path: (labels) TYPE VALUE" strings (the same format as MetricsSnapshot.pretty_print), suitable for use with a generic set-based diff (e.g.

testsuite's CompareVisitor machinery).

A metric with an UNSPECIFIED type has its type resolved from the matching (by path and labels) metric in other, if any, mirroring the wildcard-matching behavior of Metric.__eq__.

Definition at line 608 of file metrics.py.

Variable Documentation

◆ _FlattenedSnapshot

TypeAlias pytest_userver.metrics._FlattenedSnapshot = Set[tuple[str, Metric]]
protected

Definition at line 699 of file metrics.py.

◆ _LABEL_SEPARATOR

str pytest_userver.metrics._LABEL_SEPARATOR = ' = '
protected

Definition at line 667 of file metrics.py.

◆ _MISSING

Any pytest_userver.metrics._MISSING = object()
protected

Definition at line 76 of file metrics.py.

◆ T

pytest_userver.metrics.T = TypeVar('T')

Definition at line 75 of file metrics.py.