File: metrics.py

package info (click to toggle)
python-jaeger-client 4.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: python: 5,578; makefile: 93; sh: 26; awk: 16
file content (141 lines) | stat: -rw-r--r-- 4,585 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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 typing import Any, Optional, Callable, Dict


class MetricsFactory(object):
    """Generates new metrics."""

    def _noop(self, *args):
        pass

    def create_counter(
        self, name: str, tags: Optional[Dict[str, str]] = None
    ) -> Callable[[int], None]:
        """
        Generates a new counter from the given name and tags and returns
        a callable function used to increment the counter.
        :param name: name of the counter
        :param tags: tags for the counter
        :return: a callable function which takes the value to increase
        the counter by ie. def increment(value)
        """
        return self._noop

    def create_timer(
        self, name: str, tags: Optional[Dict[str, str]] = None
    ) -> Callable[[float], None]:
        """
        Generates a new timer from the given name and tags and returns
        a callable function used to record a float duration in microseconds.
        :param name: name of the timer
        :param tags: tags for the timer
        :return: a callable function which takes the duration to
        record ie. def record(duration)
        """
        return self._noop

    def create_gauge(
        self, name: str, tags: Optional[Dict[str, str]] = None
    ) -> Callable[[float], None]:
        """
        Generates a new gauge from the given name and tags and returns
        a callable function used to update the gauge.
        :param name: name of the gauge
        :param tags: tags for the gauge
        :return: a callable function which takes the value to update
        the gauge with ie. def update(value)
        """
        return self._noop


class LegacyMetricsFactory(MetricsFactory):
    """A MetricsFactory adapter for legacy Metrics class."""

    def __init__(self, metrics: 'Metrics') -> None:
        self._metrics = metrics

    def create_counter(
        self, name: str, tags: Optional[Dict[str, str]] = None
    ) -> Callable[[int], None]:
        key = self._get_key(name, tags)

        def increment(value: int) -> Optional[Any]:
            return self._metrics.count(key, value)
        return increment

    def create_timer(
        self, name: str, tags: Optional[Dict[str, str]] = None
    ) -> Callable[[float], None]:
        key = self._get_key(name, tags)

        def record(value):
            # Convert microseconds to milliseconds for legacy
            return self._metrics.timing(key, value / 1000.0)
        return record

    def create_gauge(
        self, name: str, tags: Optional[Dict[str, str]] = None
    ) -> Callable[[float], None]:
        key = self._get_key(name, tags)

        def update(value):
            return self._metrics.gauge(key, value)
        return update

    def _get_key(self, name, tags=None):
        if not tags:
            return name
        key = name
        for k in sorted(tags.keys()):
            key = key + '.' + str(k) + '_' + str(tags[k])
        return key


class Metrics(object):
    """
    Provides an abstraction of metrics reporting framework.
    This Class has been deprecated, please use MetricsFactory
    instead.
    """

    def __init__(self, count=None, gauge=None, timing=None):
        """
        :param count: function (key, value) to emit counters
        :param gauge: function (key, value) to emit gauges
        :param timing: function (key, value in milliseconds) to
            emit timings
        """
        self._count = count
        self._gauge = gauge
        self._timing = timing
        if not callable(self._count):
            self._count = None
        if not callable(self._gauge):
            self._gauge = None
        if not callable(self._timing):
            self._timing = None

    def count(self, key, value):
        if self._count:
            self._count(key, value)

    def timing(self, key, value):
        if self._timing:
            self._timing(key, value)

    def gauge(self, key, value):
        if self._gauge:
            self._gauge(key, value)