Debian Codegraph @

jaeger_client/metrics/metrics.py

raw
1 # Copyright (c) 2016 Uber Technologies, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from typing import Any, Optional, Callable, Dict
16
17
18 class MetricsFactory(object):
19 """Generates new metrics."""
20
21 def _noop(self, *args):
22 pass
23
24 def create_counter(
25 self, name: str, tags: Optional[Dict[str, str]] = None
26 ) -> Callable[[int], None]:
27 """
28 Generates a new counter from the given name and tags and returns
29 a callable function used to increment the counter.
30 :param name: name of the counter
31 :param tags: tags for the counter
32 :return: a callable function which takes the value to increase
33 the counter by ie. def increment(value)
34 """
35 return self._noop
36
37 def create_timer(
38 self, name: str, tags: Optional[Dict[str, str]] = None
39 ) -> Callable[[float], None]:
40 """
41 Generates a new timer from the given name and tags and returns
42 a callable function used to record a float duration in microseconds.
43 :param name: name of the timer
44 :param tags: tags for the timer
45 :return: a callable function which takes the duration to
46 record ie. def record(duration)
47 """
48 return self._noop
49
50 def create_gauge(
51 self, name: str, tags: Optional[Dict[str, str]] = None
52 ) -> Callable[[float], None]:
53 """
54 Generates a new gauge from the given name and tags and returns
55 a callable function used to update the gauge.
56 :param name: name of the gauge
57 :param tags: tags for the gauge
58 :return: a callable function which takes the value to update
59 the gauge with ie. def update(value)
60 """
61 return self._noop
62
63
64 class LegacyMetricsFactory(MetricsFactory):
65 """A MetricsFactory adapter for legacy Metrics class."""
66
67 def __init__(self, metrics: 'Metrics') -> None:
68 self._metrics = metrics
69
70 def create_counter(
71 self, name: str, tags: Optional[Dict[str, str]] = None
72 ) -> Callable[[int], None]:
73 key = self._get_key(name, tags)
74
75 def increment(value: int) -> Optional[Any]:
76 return self._metrics.count(key, value)
77 return increment
78
79 def create_timer(
80 self, name: str, tags: Optional[Dict[str, str]] = None
81 ) -> Callable[[float], None]:
82 key = self._get_key(name, tags)
83
84 def record(value):
85 # Convert microseconds to milliseconds for legacy
86 return self._metrics.timing(key, value / 1000.0)
87 return record
88
89 def create_gauge(
90 self, name: str, tags: Optional[Dict[str, str]] = None
91 ) -> Callable[[float], None]:
92 key = self._get_key(name, tags)
93
94 def update(value):
95 return self._metrics.gauge(key, value)
96 return update
97
98 def _get_key(self, name, tags=None):
99 if not tags:
100 return name
101 key = name
102 for k in sorted(tags.keys()):
103 key = key + '.' + str(k) + '_' + str(tags[k])
104 return key
105
106
107 class Metrics(object):
108 """
109 Provides an abstraction of metrics reporting framework.
110 This Class has been deprecated, please use MetricsFactory
111 instead.
112 """
113
114 def __init__(self, count=None, gauge=None, timing=None):
115 """
116 :param count: function (key, value) to emit counters
117 :param gauge: function (key, value) to emit gauges
118 :param timing: function (key, value in milliseconds) to
119 emit timings
120 """
121 self._count = count
122 self._gauge = gauge
123 self._timing = timing
124 if not callable(self._count):
125 self._count = None
126 if not callable(self._gauge):
127 self._gauge = None
128 if not callable(self._timing):
129 self._timing = None
130
131 def count(self, key, value):
132 if self._count:
133 self._count(key, value)
134
135 def timing(self, key, value):
136 if self._timing:
137 self._timing(key, value)
138
139 def gauge(self, key, value):
140 if self._gauge:
141 self._gauge(key, value)