@@ -50,13 +50,6 @@ def add(self, value: ValueT) -> None:
50
50
value: The value to add to the handle.
51
51
"""
52
52
53
- def set (self , value : ValueT ) -> None :
54
- """No-op implementation of `GaugeHandle` set.
55
-
56
- Args:
57
- value: The value to set to the handle.
58
- """
59
-
60
53
def record (self , value : ValueT ) -> None :
61
54
"""No-op implementation of `MeasureHandle` record.
62
55
@@ -74,15 +67,6 @@ def add(self, value: ValueT) -> None:
74
67
"""
75
68
76
69
77
- class GaugeHandle :
78
- def set (self , value : ValueT ) -> None :
79
- """Sets the current value of the handle to ``value``.
80
-
81
- Args:
82
- value: The value to set to the handle.
83
- """
84
-
85
-
86
70
class MeasureHandle :
87
71
def record (self , value : ValueT ) -> None :
88
72
"""Records the given ``value`` to this handle.
@@ -124,7 +108,7 @@ def get_handle(self, label_set: LabelSet) -> "object":
124
108
125
109
Handles are useful to reduce the cost of repeatedly recording a metric
126
110
with a pre-defined set of label values. All metric kinds (counter,
127
- gauge, measure) support declaring a set of required label keys. The
111
+ measure) support declaring a set of required label keys. The
128
112
values corresponding to these keys should be specified in every handle.
129
113
"Unspecified" label values, in cases where a handle is requested but
130
114
a value was not provided are permitted.
@@ -153,14 +137,6 @@ def add(self, value: ValueT, label_set: LabelSet) -> None:
153
137
label_set: `LabelSet` to associate with the returned handle.
154
138
"""
155
139
156
- def set (self , value : ValueT , label_set : LabelSet ) -> None :
157
- """No-op implementation of `Gauge` set.
158
-
159
- Args:
160
- value: The value to set the gauge metric to.
161
- label_set: `LabelSet` to associate with the returned handle.
162
- """
163
-
164
140
def record (self , value : ValueT , label_set : LabelSet ) -> None :
165
141
"""No-op implementation of `Measure` record.
166
142
@@ -186,28 +162,6 @@ def add(self, value: ValueT, label_set: LabelSet) -> None:
186
162
"""
187
163
188
164
189
- class Gauge (Metric ):
190
- """A gauge type metric that expresses a pre-calculated value.
191
-
192
- Gauge metrics have a value that is either ``Set`` by explicit
193
- instrumentation or observed through a callback. This kind of metric
194
- should be used when the metric cannot be expressed as a sum or because
195
- the measurement interval is arbitrary.
196
- """
197
-
198
- def get_handle (self , label_set : LabelSet ) -> "GaugeHandle" :
199
- """Gets a `GaugeHandle`."""
200
- return GaugeHandle ()
201
-
202
- def set (self , value : ValueT , label_set : LabelSet ) -> None :
203
- """Sets the value of the gauge to ``value``.
204
-
205
- Args:
206
- value: The value to set the gauge metric to.
207
- label_set: `LabelSet` to associate with the returned handle.
208
- """
209
-
210
-
211
165
class Measure (Metric ):
212
166
"""A measure type metric that represent raw stats that are recorded.
213
167
@@ -227,6 +181,37 @@ def record(self, value: ValueT, label_set: LabelSet) -> None:
227
181
"""
228
182
229
183
184
+ class Observer (abc .ABC ):
185
+ """An observer type metric instrument used to capture a current set of values.
186
+
187
+
188
+ Observer instruments are asynchronous, a callback is invoked with the
189
+ observer instrument as argument allowing the user to capture multiple
190
+ values per collection interval.
191
+ """
192
+
193
+ @abc .abstractmethod
194
+ def observe (self , value : ValueT , label_set : LabelSet ) -> None :
195
+ """Captures ``value`` to the observer.
196
+
197
+ Args:
198
+ value: The value to capture to this observer metric.
199
+ label_set: `LabelSet` associated to ``value``.
200
+ """
201
+
202
+
203
+ class DefaultObserver (Observer ):
204
+ """No-op implementation of ``Observer``."""
205
+
206
+ def observe (self , value : ValueT , label_set : LabelSet ) -> None :
207
+ """Captures ``value`` to the observer.
208
+
209
+ Args:
210
+ value: The value to capture to this observer metric.
211
+ label_set: `LabelSet` associated to ``value``.
212
+ """
213
+
214
+
230
215
class MeterProvider (abc .ABC ):
231
216
@abc .abstractmethod
232
217
def get_meter (
@@ -277,15 +262,16 @@ def get_meter(
277
262
return DefaultMeter ()
278
263
279
264
280
- MetricT = TypeVar ("MetricT" , Counter , Gauge , Measure )
265
+ MetricT = TypeVar ("MetricT" , Counter , Measure , Observer )
266
+ ObserverCallbackT = Callable [[Observer ], None ]
281
267
282
268
283
269
# pylint: disable=unused-argument
284
270
class Meter (abc .ABC ):
285
271
"""An interface to allow the recording of metrics.
286
272
287
- `Metric` s are used for recording pre-defined aggregation (gauge and
288
- counter), or raw values (measure) in which the aggregation and labels
273
+ `Metric` s are used for recording pre-defined aggregation (counter),
274
+ or raw values (measure) in which the aggregation and labels
289
275
for the exported metric are deferred.
290
276
"""
291
277
@@ -325,14 +311,41 @@ def create_metric(
325
311
Args:
326
312
name: The name of the metric.
327
313
description: Human-readable description of the metric.
328
- unit: Unit of the metric values.
314
+ unit: Unit of the metric values following the UCUM convention
315
+ (https://unitsofmeasure.org/ucum.html).
329
316
value_type: The type of values being recorded by the metric.
330
317
metric_type: The type of metric being created.
331
318
label_keys: The keys for the labels with dynamic values.
332
319
enabled: Whether to report the metric by default.
333
320
Returns: A new ``metric_type`` metric with values of ``value_type``.
334
321
"""
335
322
323
+ @abc .abstractmethod
324
+ def register_observer (
325
+ self ,
326
+ callback : ObserverCallbackT ,
327
+ name : str ,
328
+ description : str ,
329
+ unit : str ,
330
+ value_type : Type [ValueT ],
331
+ label_keys : Sequence [str ] = (),
332
+ enabled : bool = True ,
333
+ ) -> "Observer" :
334
+ """Registers an ``Observer`` metric instrument.
335
+
336
+ Args:
337
+ callback: Callback invoked each collection interval with the
338
+ observer as argument.
339
+ name: The name of the metric.
340
+ description: Human-readable description of the metric.
341
+ unit: Unit of the metric values following the UCUM convention
342
+ (https://unitsofmeasure.org/ucum.html).
343
+ value_type: The type of values being recorded by the metric.
344
+ label_keys: The keys for the labels with dynamic values.
345
+ enabled: Whether to report the metric by default.
346
+ Returns: A new ``Observer`` metric instrument.
347
+ """
348
+
336
349
@abc .abstractmethod
337
350
def get_label_set (self , labels : Dict [str , str ]) -> "LabelSet" :
338
351
"""Gets a `LabelSet` with the given labels.
@@ -367,6 +380,18 @@ def create_metric(
367
380
# pylint: disable=no-self-use
368
381
return DefaultMetric ()
369
382
383
+ def register_observer (
384
+ self ,
385
+ callback : ObserverCallbackT ,
386
+ name : str ,
387
+ description : str ,
388
+ unit : str ,
389
+ value_type : Type [ValueT ],
390
+ label_keys : Sequence [str ] = (),
391
+ enabled : bool = True ,
392
+ ) -> "Observer" :
393
+ return DefaultObserver ()
394
+
370
395
def get_label_set (self , labels : Dict [str , str ]) -> "LabelSet" :
371
396
# pylint: disable=no-self-use
372
397
return DefaultLabelSet ()
0 commit comments