|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -# pylint: disable=function-redefined |
16 |
| -# pylint: disable=dangerous-default-value |
17 |
| -# Classes in this module use dictionaries as default arguments. This is |
18 |
| -# considered dangerous by pylint because the default dictionary is shared by |
19 |
| -# all instances. Implementations of these classes must not make any change to |
20 |
| -# this default dictionary in __init__. |
| 15 | +# pylint: disable=too-many-ancestors |
21 | 16 |
|
| 17 | +from typing import Dict, Generator, Iterable, Union |
| 18 | + |
| 19 | +from opentelemetry._metrics.instrument import CallbackT |
22 | 20 | from opentelemetry._metrics.instrument import Counter as APICounter
|
23 | 21 | from opentelemetry._metrics.instrument import Histogram as APIHistogram
|
24 | 22 | from opentelemetry._metrics.instrument import (
|
|
30 | 28 | from opentelemetry._metrics.instrument import (
|
31 | 29 | ObservableUpDownCounter as APIObservableUpDownCounter,
|
32 | 30 | )
|
33 |
| -from opentelemetry._metrics.instrument import TCallback |
34 | 31 | from opentelemetry._metrics.instrument import UpDownCounter as APIUpDownCounter
|
| 32 | +from opentelemetry.sdk._metrics.measurement import Measurement |
35 | 33 | from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
|
36 | 34 |
|
37 | 35 |
|
38 |
| -class _Instrument: |
| 36 | +class _Synchronous: |
39 | 37 | def __init__(
|
40 | 38 | self,
|
41 | 39 | instrumentation_info: InstrumentationInfo,
|
42 | 40 | name: str,
|
43 |
| - *args, |
44 | 41 | unit: str = "",
|
45 | 42 | description: str = "",
|
46 | 43 | ):
|
47 | 44 | self._instrumentation_info = instrumentation_info
|
48 |
| - super().__init__(name, *args, unit=unit, description=description) |
| 45 | + super().__init__(name, unit=unit, description=description) |
49 | 46 |
|
50 | 47 |
|
51 |
| -class Counter(_Instrument, APICounter): |
| 48 | +class _Asynchronous: |
52 | 49 | def __init__(
|
53 | 50 | self,
|
54 | 51 | instrumentation_info: InstrumentationInfo,
|
55 | 52 | name: str,
|
| 53 | + callback: CallbackT, |
56 | 54 | unit: str = "",
|
57 | 55 | description: str = "",
|
58 | 56 | ):
|
59 |
| - super().__init__( |
60 |
| - instrumentation_info, |
61 |
| - name, |
62 |
| - unit=unit, |
63 |
| - description=description, |
64 |
| - ) |
65 |
| - |
66 |
| - def add(self, amount, attributes=None): |
67 |
| - # FIXME check that the amount is non negative |
68 |
| - pass |
69 | 57 |
|
| 58 | + self._instrumentation_info = instrumentation_info |
| 59 | + super().__init__(name, callback, unit=unit, description=description) |
70 | 60 |
|
71 |
| -class UpDownCounter(_Instrument, APIUpDownCounter): |
72 |
| - def __init__( |
73 |
| - self, |
74 |
| - instrumentation_info: InstrumentationInfo, |
75 |
| - name: str, |
76 |
| - unit: str = "", |
77 |
| - description: str = "", |
78 |
| - ): |
79 |
| - super().__init__( |
80 |
| - instrumentation_info, |
81 |
| - name, |
82 |
| - unit=unit, |
83 |
| - description=description, |
84 |
| - ) |
85 |
| - |
86 |
| - def add(self, amount, attributes=None): |
87 |
| - pass |
| 61 | + self._callback = callback |
88 | 62 |
|
| 63 | + if isinstance(callback, Generator): |
89 | 64 |
|
90 |
| -class ObservableCounter(_Instrument, APIObservableCounter): |
91 |
| - def __init__( |
92 |
| - self, |
93 |
| - instrumentation_info: InstrumentationInfo, |
94 |
| - name: str, |
95 |
| - callback: TCallback, |
96 |
| - unit: str = "", |
97 |
| - description: str = "", |
98 |
| - ): |
99 |
| - super().__init__( |
100 |
| - instrumentation_info, |
101 |
| - name, |
102 |
| - callback, |
103 |
| - unit=unit, |
104 |
| - description=description, |
105 |
| - ) |
| 65 | + def inner() -> Iterable[Measurement]: |
| 66 | + return next(callback) |
106 | 67 |
|
| 68 | + self._callback = inner |
107 | 69 |
|
108 |
| -class ObservableUpDownCounter(_Instrument, APIObservableUpDownCounter): |
109 |
| - def __init__( |
110 |
| - self, |
111 |
| - instrumentation_info: InstrumentationInfo, |
112 |
| - name: str, |
113 |
| - callback: TCallback, |
114 |
| - unit: str = "", |
115 |
| - description: str = "", |
| 70 | + @property |
| 71 | + def callback(self) -> CallbackT: |
| 72 | + return self._callback |
| 73 | + |
| 74 | + |
| 75 | +class Counter(_Synchronous, APICounter): |
| 76 | + def add( |
| 77 | + self, amount: Union[int, float], attributes: Dict[str, str] = None |
116 | 78 | ):
|
117 |
| - super().__init__( |
118 |
| - instrumentation_info, |
119 |
| - name, |
120 |
| - callback, |
121 |
| - unit=unit, |
122 |
| - description=description, |
123 |
| - ) |
| 79 | + if amount < 0: |
| 80 | + raise Exception("amount must be non negative") |
124 | 81 |
|
125 | 82 |
|
126 |
| -class Histogram(_Instrument, APIHistogram): |
127 |
| - def __init__( |
128 |
| - self, |
129 |
| - instrumentation_info: InstrumentationInfo, |
130 |
| - name: str, |
131 |
| - unit: str = "", |
132 |
| - description: str = "", |
| 83 | +class UpDownCounter(_Synchronous, APIUpDownCounter): |
| 84 | + def add( |
| 85 | + self, amount: Union[int, float], attributes: Dict[str, str] = None |
133 | 86 | ):
|
134 |
| - super().__init__( |
135 |
| - instrumentation_info, |
136 |
| - name, |
137 |
| - unit=unit, |
138 |
| - description=description, |
139 |
| - ) |
| 87 | + pass |
| 88 | + |
140 | 89 |
|
| 90 | +class ObservableCounter(_Asynchronous, APIObservableCounter): |
| 91 | + pass |
| 92 | + |
| 93 | + |
| 94 | +class ObservableUpDownCounter(_Asynchronous, APIObservableUpDownCounter): |
| 95 | + pass |
| 96 | + |
| 97 | + |
| 98 | +class Histogram(_Synchronous, APIHistogram): |
141 | 99 | def record(self, amount, attributes=None):
|
142 | 100 | pass
|
143 | 101 |
|
144 | 102 |
|
145 |
| -class ObservableGauge(_Instrument, APIObservableGauge): |
146 |
| - def __init__( |
147 |
| - self, |
148 |
| - instrumentation_info: InstrumentationInfo, |
149 |
| - name: str, |
150 |
| - callback: TCallback, |
151 |
| - unit: str = "", |
152 |
| - description: str = "", |
153 |
| - ): |
154 |
| - super().__init__( |
155 |
| - instrumentation_info, |
156 |
| - name, |
157 |
| - callback, |
158 |
| - unit=unit, |
159 |
| - description=description, |
160 |
| - ) |
| 103 | +class ObservableGauge(_Asynchronous, APIObservableGauge): |
| 104 | + pass |
0 commit comments