File: test_metrics.py

package info (click to toggle)
python-jaeger-client 4.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 940 kB
  • sloc: python: 5,578; makefile: 93; sh: 26; awk: 16
file content (98 lines) | stat: -rw-r--r-- 2,889 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from unittest import mock

from jaeger_client.metrics import MetricsFactory, Metrics,\
    LegacyMetricsFactory


def test_metrics_factory_noop():
    mf = MetricsFactory()
    mf.create_counter('foo')(1)
    mf.create_timer('foo')(1)
    mf.create_gauge('foo')(1)


def test_metrics_count_func_called():
    m = mock.MagicMock()
    metrics = Metrics(count=m)
    metrics.count('foo', 1)
    assert m.call_args == (('foo', 1),)


def test_metrics_timing_func_called():
    m = mock.MagicMock()
    metrics = Metrics(timing=m)
    metrics.timing('foo', 1)
    assert m.call_args == (('foo', 1),)


def test_metrics_gauge_func_called():
    m = mock.MagicMock()
    metrics = Metrics(gauge=m)
    metrics.gauge('foo', 1)
    assert m.call_args == (('foo', 1),)


def test_metrics_count_func_noops_if_given_uncallable_count_found():
    metrics = Metrics(count=123)
    metrics.count('foo', 1)


def test_metrics_timing_func_noops_if_given_uncallable_timing_found():
    metrics = Metrics(timing=123)
    metrics.timing('foo', 1)


def test_metrics_gauge_func_noops_if_given_uncallable_gauge_found():
    metrics = Metrics(gauge=123)
    metrics.gauge('foo', 1)


def test_legacy_metrics_factory():
    cm = mock.MagicMock()
    tm = mock.MagicMock()
    gm = mock.MagicMock()
    mf = LegacyMetricsFactory(Metrics(count=cm, timing=tm, gauge=gm))
    counter = mf.create_counter(name='foo', tags={'k': 'v', 'a': 'counter'})
    counter(1)
    assert cm.call_args == (('foo.a_counter.k_v', 1),)

    gauge = mf.create_gauge(name='bar', tags={'k': 'v', 'a': 'gauge'})
    gauge(2)
    assert gm.call_args == (('bar.a_gauge.k_v', 2),)

    timing = mf.create_timer(name='rawr', tags={'k': 'v', 'a': 'timer'})
    timing(3)
    assert tm.call_args == (('rawr.a_timer.k_v', 0.003),)

    mf = LegacyMetricsFactory(Metrics(timing=tm))
    timing = mf.create_timer(name='wow')
    timing(4)
    assert tm.call_args == (('wow', 0.004),), \
        'building a timer with no tags should work'


def test_legacy_metrics_factory_noop():
    mf = LegacyMetricsFactory(Metrics())
    counter = mf.create_counter(name='foo', tags={'a': 'counter'})
    counter(1)

    gauge = mf.create_gauge(name='bar', tags={'a': 'gauge'})
    gauge(2)

    timing = mf.create_timer(name='rawr', tags={'a': 'timer'})
    timing(3)