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

Detailed Description

Asyncio userver client for monitor listeners, typically retrieved from plugins.service_client.monitor_client fixture.

Compatible with werkzeug interface.

Definition at line 403 of file client.py.

+ Inheritance diagram for pytest_userver.client.ClientMonitor:

Public Member Functions

MetricsDiffer metrics_diff (self, *, str|None path=None, str|None prefix=None, dict[str, str]|None labels=None, bool diff_gauge=False)
 Creates a MetricsDiffer that fetches metrics using this client.
 
pytest_userver.metrics.MetricsSnapshot metrics (self, *, str|None path=None, str|None prefix=None, dict[str, str]|None labels=None)
 Returns a dict of metric names to Metric.
 
pytest_userver.metrics.Metric|None single_metric_optional (self, str path, *, dict[str, str]|None labels=None)
 Either return a pytest_userver.metrics.Metric or None if there's no such metric.
 
pytest_userver.metrics.Metric|None single_metric (self, str path, *, dict[str, str]|None labels=None)
 Returns the pytest_userver.metrics.Metric.
 
dict[str, pytest_userver.metrics.Metricmetrics_raw (self, str output_format, *, str|None path=None, str|None prefix=None, dict[str, str]|None labels=None)
 Low level function that returns metrics in a specific format.
 
 get_metrics (self, prefix=None)
 
 get_metric (self, metric_name)
 
 fired_alerts (self)
 
http.ClientResponse post (self, str path, JsonAnyOptional json=None, Any data=None, dict[str, str]|None params=None, str|None bearer=None, str|None x_real_ip=None, dict[str, str]|None headers=None, **kwargs)
 Make a HTTP POST request.
 
http.ClientResponse put (self, path, JsonAnyOptional json=None, Any data=None, dict[str, str]|None params=None, str|None bearer=None, str|None x_real_ip=None, dict[str, str]|None headers=None, **kwargs)
 Make a HTTP PUT request.
 
http.ClientResponse patch (self, path, JsonAnyOptional json=None, Any data=None, dict[str, str]|None params=None, str|None bearer=None, str|None x_real_ip=None, dict[str, str]|None headers=None, **kwargs)
 Make a HTTP PATCH request.
 
http.ClientResponse get (self, str path, dict[str, str]|None headers=None, str|None bearer=None, str|None x_real_ip=None, **kwargs)
 Make a HTTP GET request.
 
http.ClientResponse delete (self, str path, dict[str, str]|None headers=None, str|None bearer=None, str|None x_real_ip=None, **kwargs)
 Make a HTTP DELETE request.
 
http.ClientResponse options (self, str path, dict[str, str]|None headers=None, str|None bearer=None, str|None x_real_ip=None, **kwargs)
 Make a HTTP OPTIONS request.
 
http.ClientResponse request (self, str http_method, str path, **kwargs)
 Make a HTTP request with the specified method.
 
 raw_aiohttp_client (self)
 

Protected Member Functions

Awaitable[http.ClientResponse] _wrap_client_response (self, aiohttp.ClientResponse response)
 

Protected Attributes

 _client = client
 

Member Function Documentation

◆ _wrap_client_response()

Awaitable[http.ClientResponse] pytest_userver.client.ClientWrapper._wrap_client_response ( self,
aiohttp.ClientResponse response )
protectedinherited

Definition at line 277 of file client.py.

◆ delete()

http.ClientResponse pytest_userver.client.ClientWrapper.delete ( self,
str path,
dict[str, str] | None headers = None,
str | None bearer = None,
str | None x_real_ip = None,
** kwargs )
inherited

Make a HTTP DELETE request.

Definition at line 218 of file client.py.

◆ fired_alerts()

pytest_userver.client.ClientMonitor.fired_alerts ( self)

Definition at line 547 of file client.py.

◆ get()

http.ClientResponse pytest_userver.client.ClientWrapper.get ( self,
str path,
dict[str, str] | None headers = None,
str | None bearer = None,
str | None x_real_ip = None,
** kwargs )
inherited

Make a HTTP GET request.

Definition at line 198 of file client.py.

◆ get_metric()

pytest_userver.client.ClientMonitor.get_metric ( self,
metric_name )
Deprecated
Use metrics() or single_metric() instead

Definition at line 540 of file client.py.

◆ get_metrics()

pytest_userver.client.ClientMonitor.get_metrics ( self,
prefix = None )
Deprecated
Use metrics() or single_metric() instead

Definition at line 533 of file client.py.

◆ metrics()

pytest_userver.metrics.MetricsSnapshot pytest_userver.client.ClientMonitor.metrics ( self,
* ,
str | None path = None,
str | None prefix = None,
dict[str, str] | None labels = None )

Returns a dict of metric names to Metric.

Parameters
pathOptional full metric path
prefixOptional prefix on which the metric paths should start
labelsOptional dictionary of labels that must be in the metric
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 446 of file client.py.

◆ metrics_diff()

MetricsDiffer pytest_userver.client.ClientMonitor.metrics_diff ( self,
* ,
str | None path = None,
str | None prefix = None,
dict[str, str] | None labels = None,
bool diff_gauge = False )

Creates a MetricsDiffer that fetches metrics using this client.

It's recommended to use this method over metrics to make sure the tests don't affect each other.

With diff_gauge off, only RATE metrics are differentiated. With diff_gauge on, GAUGE metrics are differentiated as well, which may lead to nonsensical results for those.

Parameters
pathOptional full metric path
prefixOptional prefix on which the metric paths should start
labelsOptional dictionary of labels that must be in the metric
diff_gaugeWhether to differentiate GAUGE metrics
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 413 of file client.py.

◆ metrics_raw()

dict[str, pytest_userver.metrics.Metric] pytest_userver.client.ClientMonitor.metrics_raw ( self,
str output_format,
* ,
str | None path = None,
str | None prefix = None,
dict[str, str] | None labels = None )

Low level function that returns metrics in a specific format.

Use metrics and single_metric instead if possible.

Parameters
output_formatpytest_userver.metrics.Metric output format. See server.handlers.ServerMonitor for a list of supported formats.
pathOptional full metric path
prefixOptional prefix on which the metric paths should start
labelsOptional dictionary of labels that must be in the metric

Definition at line 507 of file client.py.

◆ options()

http.ClientResponse pytest_userver.client.ClientWrapper.options ( self,
str path,
dict[str, str] | None headers = None,
str | None bearer = None,
str | None x_real_ip = None,
** kwargs )
inherited

Make a HTTP OPTIONS request.

Definition at line 238 of file client.py.

◆ patch()

http.ClientResponse pytest_userver.client.ClientWrapper.patch ( self,
path,
JsonAnyOptional json = None,
Any data = None,
dict[str, str] | None params = None,
str | None bearer = None,
str | None x_real_ip = None,
dict[str, str] | None headers = None,
** kwargs )
inherited

Make a HTTP PATCH request.

Definition at line 171 of file client.py.

◆ post()

http.ClientResponse pytest_userver.client.ClientWrapper.post ( self,
str path,
JsonAnyOptional json = None,
Any data = None,
dict[str, str] | None params = None,
str | None bearer = None,
str | None x_real_ip = None,
dict[str, str] | None headers = None,
** kwargs )
inherited

Make a HTTP POST request.

Definition at line 117 of file client.py.

◆ put()

http.ClientResponse pytest_userver.client.ClientWrapper.put ( self,
path,
JsonAnyOptional json = None,
Any data = None,
dict[str, str] | None params = None,
str | None bearer = None,
str | None x_real_ip = None,
dict[str, str] | None headers = None,
** kwargs )
inherited

Make a HTTP PUT request.

Definition at line 144 of file client.py.

◆ raw_aiohttp_client()

pytest_userver.client.ClientWrapper.raw_aiohttp_client ( self)
inherited
Deprecated
Use pytest_userver.client.Client directly instead.

Definition at line 271 of file client.py.

◆ request()

http.ClientResponse pytest_userver.client.ClientWrapper.request ( self,
str http_method,
str path,
** kwargs )
inherited

Make a HTTP request with the specified method.

Definition at line 258 of file client.py.

◆ single_metric()

pytest_userver.metrics.Metric | None pytest_userver.client.ClientMonitor.single_metric ( self,
str path,
* ,
dict[str, str] | None labels = None )

Returns the pytest_userver.metrics.Metric.

Parameters
pathFull metric path
labelsOptional dictionary of labels that must be in the metric
Exceptions
AssertionErrorif more than one metric or no metric found
async def test_engine_tasks_alive_metric(service_client, monitor_client: pytest_userver.client.ClientMonitor):
metric: pytest_userver.metrics.Metric = await monitor_client.single_metric(
'engine.task-processors.tasks.alive',
labels={'task_processor': 'main-task-processor'},
)
assert metric.value > 0
assert metric.labels == {'task_processor': 'main-task-processor'}

Definition at line 488 of file client.py.

◆ single_metric_optional()

pytest_userver.metrics.Metric | None pytest_userver.client.ClientMonitor.single_metric_optional ( self,
str path,
* ,
dict[str, str] | None labels = None )

Either return a pytest_userver.metrics.Metric or None if there's no such metric.

Parameters
pathFull metric path
labelsOptional dictionary of labels that must be in the metric
Exceptions
AssertionErrorif more than one metric returned
async def test_some_optional_metric(service_client, monitor_client: pytest_userver.client.ClientMonitor):
metric: pytest_userver.metrics.Metric | None = await monitor_client.single_metric_optional(
'some.metric.error',
labels={'task_processor': 'main-task-processor'},
)
assert metric is None or metric.value == 0

Definition at line 469 of file client.py.

Member Data Documentation

◆ _client

pytest_userver.client.ClientWrapper._client = client
protectedinherited

Definition at line 115 of file client.py.


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