nti.fakestatsd¶
nti.fakestatsd is a testing client for verifying StatsD metrics emitted by perfmetrics.
It’s easy to create a new client for use in testing:
>>> from nti.fakestatsd import FakeStatsDClient
>>> test_client = FakeStatsDClient()
This client exposes the same public interface as
perfmetrics.statsd.StatsdClient
. For example we can increment
counters, set gauges, etc:
>>> test_client.incr('request_c')
>>> test_client.gauge('active_sessions', 320)
Unlike perfmetrics.statsd.StatsdClient
, FakeStatsDClient
simply
tracks the statsd packets that would be sent. This information is
exposed on our test_client
both as the raw statsd packet, and for
conveninece this information is also parsed and exposed as Metric
objects. For complete details see FakeStatsDClient
and Metric
.
>>> test_client.packets
['request_c:1|c', 'active_sessions:320|g']
>>> test_client.metrics
[<nti.fakestatsd.metric.Metric object at ...>, <nti.fakestatsd.metric.Metric object at ...>]
For validating metrics we provide a set of hamcrest matchers for use in test assertions:
>>> from hamcrest import assert_that
>>> from hamcrest import contains
>>> from nti.fakestatsd.matchers import is_metric
>>> from nti.fakestatsd.matchers import is_gauge
>>> assert_that(test_client,
... contains(is_metric('c', 'request_c', '1'),
... is_gauge('active_sessions', '320')))
>>> assert_that(test_client,
... contains(is_gauge('request_c', '1'),
... is_gauge('active_sessions', '320')))
Traceback (most recent call last):
...
AssertionError:
Expected: a sequence containing [Metric of form <request_c:1|g>, Metric of form <active_sessions:320|g>]
but: item 0: was <request_c:1|c>
For complete details and the changelog, see the documentation.
API Reference¶
-
class
FakeStatsDClient
(prefix='')[source]¶ Bases:
perfmetrics.statsd.StatsdClient
A mock statsd client that tracks sent statsd metrics in memory rather than pushing them over a socket. This class is a drop in replacement for
perfmetrics.statsd.StatsdClient
that collects statsd packets andMetric
that are sent through the client.-
iter_metrics
()¶ Iterates the
Metrics
provided to this statsd client.
-
-
METRIC_COUNTER_KIND
= 'c'¶ The statsd metric kind for Counters
-
METRIC_GAUGE_KIND
= 'g'¶ The statsd metric kind for Gauges
-
METRIC_SET_KIND
= 's'¶ The statsd metric kind for Sets
-
METRIC_TIMER_KIND
= 'ms'¶ The statsd metric kind for Timers
-
class
Metric
(name, value, kind, sampling_rate=None)[source]¶ Bases:
object
The representation of a single statsd metric.
-
kind
= None¶ The statsd code for the type of metric. e.g. one of the
METRIC_*_KIND
constants
-
classmethod
make
(packet)[source]¶ Creates a metric from the provided statsd packet.
Raises: ValueError – if packet is a multi metric packet or otherwise invalid.
-
classmethod
make_all
(packet)[source]¶ Makes a list of metrics from the provided statsd packet.
Like
make
but supports multi metric packets
-
name
= None¶ The metric name
-
sampling_rate
= None¶ The rate with which this event has been sampled from (optional)
-
value
= None¶ The value provided for the metric
-
Hamcrest Matchers¶
-
is_metric
(kind=<object object>, name=<object object>, value=<object object>, sampling_rate=<object object>)[source]¶ A hamcrest matcher that validates the specific parts of a
Metric
.Parameters: - kind (str) – A hamcrest matcher or string that matches the kind for this metric
- name (str) – A hamcrest matcher or string that matches the name for this metric
- value – A hamcrest matcher or string that matches the value for this metric
- sampling_rate – A hamcrest matcher or number that matches the sampling rate this metric was collected with
-
is_counter
(name=<object object>, value=<object object>, sampling_rate=<object object>)[source]¶ A hamcrest matcher validating the parts of a
Metric
of kindMETRIC_COUNTER_KIND
See also
-
is_gauge
(name=<object object>, value=<object object>, sampling_rate=<object object>)[source]¶ A hamcrest matcher validating the parts of a
Metric
of kindMETRIC_GAUGE_KIND
See also
-
is_set
(name=<object object>, value=<object object>, sampling_rate=<object object>)[source]¶ A hamcrest matcher validating the parts of a
Metric
of kindMETRIC_SET_KIND
See also
-
is_timer
(name=<object object>, value=<object object>, sampling_rate=<object object>)[source]¶ A hamcrest matcher validating the parts of a
Metric
of kindMETRIC_TIMER_KIND
See also