-
Notifications
You must be signed in to change notification settings - Fork 657
/
__init__.py
610 lines (474 loc) · 18.5 KB
/
__init__.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# Copyright The OpenTelemetry Authors
#
# 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.
"""
The OpenTelemetry metrics API describes the classes used to report raw
measurements, as well as metrics with known aggregation and labels.
The `Meter` class is used to construct `Metric` s to record raw statistics
as well as metrics with predefined aggregation.
`Meter` s can be obtained via the `MeterProvider`, corresponding to the name
of the instrumenting library and (optionally) a version.
See the `metrics api`_ spec for terminology and context clarification.
.. _metrics api:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md
"""
import abc
from logging import getLogger
from typing import (
Callable,
Dict,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
)
from opentelemetry.util import _load_meter_provider
logger = getLogger(__name__)
ValueT = TypeVar("ValueT", int, float)
class BoundCounter(abc.ABC):
@abc.abstractmethod
def add(self, value: ValueT) -> None:
"""Increases the value of the bound counter by ``value``.
Args:
value: The value to add to the bound counter. Must be positive.
"""
class DefaultBoundCounter(BoundCounter):
"""The default bound counter instrument.
Used when no bound counter implementation is available.
"""
def add(self, value: ValueT) -> None:
pass
class BoundUpDownCounter(abc.ABC):
@abc.abstractmethod
def add(self, value: ValueT) -> None:
"""Increases the value of the bound updowncounter by ``value``.
Args:
value: The value to add to the bound updowncounter. Can be positive
or negative.
"""
class DefaultBoundUpDownCounter(BoundUpDownCounter):
"""The default bound updowncounter instrument.
Used when no bound updowncounter implementation is available.
"""
def add(self, value: ValueT) -> None:
pass
class BoundValueRecorder(abc.ABC):
@abc.abstractmethod
def record(self, value: ValueT) -> None:
"""Records the given ``value`` to this bound valuerecorder.
Args:
value: The value to record to the bound valuerecorder.
"""
class DefaultBoundValueRecorder(BoundValueRecorder):
"""The default bound valuerecorder instrument.
Used when no bound valuerecorder implementation is available.
"""
def record(self, value: ValueT) -> None:
pass
class Metric(abc.ABC):
"""Base class for various types of metrics.
Metric class that inherit from this class are specialized with the type of
bound metric instrument that the metric holds.
"""
@abc.abstractmethod
def bind(self, labels: Dict[str, str]) -> "object":
"""Gets a bound metric instrument.
Bound metric instruments are useful to reduce the cost of repeatedly
recording a metric with a pre-defined set of label values.
Args:
labels: Labels to associate with the bound instrument.
"""
class Counter(Metric):
"""A counter type metric that expresses the computation of a sum."""
@abc.abstractmethod
def add(self, value: ValueT, labels: Dict[str, str]) -> None:
"""Increases the value of the counter by ``value``.
Args:
value: The value to add to the counter metric. Should be positive
or zero. For a Counter that can decrease, use
`UpDownCounter`.
labels: Labels to associate with the bound instrument.
"""
class DefaultCounter(Counter):
"""The default counter instrument.
Used when no `Counter` implementation is available.
"""
def bind(self, labels: Dict[str, str]) -> "DefaultBoundCounter":
return DefaultBoundCounter()
def add(self, value: ValueT, labels: Dict[str, str]) -> None:
pass
class UpDownCounter(Metric):
"""A counter type metric that expresses the computation of a sum,
allowing negative increments."""
@abc.abstractmethod
def add(self, value: ValueT, labels: Dict[str, str]) -> None:
"""Increases the value of the counter by ``value``.
Args:
value: The value to add to the counter metric. Can be positive or
negative. For a Counter that is never decreasing, use
`Counter`.
labels: Labels to associate with the bound instrument.
"""
class DefaultUpDownCounter(UpDownCounter):
"""The default updowncounter instrument.
Used when no `UpDownCounter` implementation is available.
"""
def bind(self, labels: Dict[str, str]) -> "DefaultBoundUpDownCounter":
return DefaultBoundUpDownCounter()
def add(self, value: ValueT, labels: Dict[str, str]) -> None:
pass
class ValueRecorder(Metric):
"""A valuerecorder type metric that represent raw stats."""
@abc.abstractmethod
def record(self, value: ValueT, labels: Dict[str, str]) -> None:
"""Records the ``value`` to the valuerecorder.
Args:
value: The value to record to this valuerecorder metric.
labels: Labels to associate with the bound instrument.
"""
class DefaultValueRecorder(ValueRecorder):
"""The default valuerecorder instrument.
Used when no `ValueRecorder` implementation is available.
"""
def bind(self, labels: Dict[str, str]) -> "DefaultBoundValueRecorder":
return DefaultBoundValueRecorder()
def record(self, value: ValueT, labels: Dict[str, str]) -> None:
pass
class Observer(abc.ABC):
"""An observer type metric instrument used to capture a current set of
values.
Observer instruments are asynchronous, a callback is invoked with the
observer instrument as argument allowing the user to capture multiple
values per collection interval.
"""
@abc.abstractmethod
def observe(self, value: ValueT, labels: Dict[str, str]) -> None:
"""Captures ``value`` to the observer.
Args:
value: The value to capture to this observer metric.
labels: Labels associated to ``value``.
"""
# pylint: disable=W0223
class SumObserver(Observer):
"""Asynchronous instrument used to capture a monotonic sum."""
class DefaultSumObserver(SumObserver):
"""No-op implementation of ``SumObserver``."""
def observe(self, value: ValueT, labels: Dict[str, str]) -> None:
pass
# pylint: disable=W0223
class UpDownSumObserver(Observer):
"""Asynchronous instrument used to capture a non-monotonic count."""
class DefaultUpDownSumObserver(UpDownSumObserver):
"""No-op implementation of ``UpDownSumObserver``."""
def observe(self, value: ValueT, labels: Dict[str, str]) -> None:
pass
# pylint: disable=W0223
class ValueObserver(Observer):
"""Asynchronous instrument used to capture grouping measurements."""
class DefaultValueObserver(ValueObserver):
"""No-op implementation of ``ValueObserver``."""
def observe(self, value: ValueT, labels: Dict[str, str]) -> None:
pass
class MeterProvider(abc.ABC):
@abc.abstractmethod
def get_meter(
self,
instrumenting_module_name: str,
instrumenting_library_version: str = "",
) -> "Meter":
"""Returns a `Meter` for use by the given instrumentation library.
This function may return different `Meter` types (e.g. a no-op meter
vs. a functional meter).
Args:
instrumenting_module_name: The name of the instrumenting module
(usually just ``__name__``).
This should *not* be the name of the module that is
instrumented but the name of the module doing the instrumentation.
E.g., instead of ``"requests"``, use
``"opentelemetry.instrumentation.requests"``.
instrumenting_library_version: Optional. The version string of the
instrumenting library. Usually this should be the same as
``pkg_resources.get_distribution(instrumenting_library_name).version``.
"""
class DefaultMeterProvider(MeterProvider):
"""The default MeterProvider, used when no implementation is available.
All operations are no-op.
"""
def get_meter(
self,
instrumenting_module_name: str,
instrumenting_library_version: str = "",
) -> "Meter":
# pylint:disable=no-self-use,unused-argument
return DefaultMeter()
InstrumentT = TypeVar("InstrumentT", bound=Union[Metric, Observer])
ObserverCallbackT = Callable[[Observer], None]
# pylint: disable=unused-argument
class Meter(abc.ABC):
"""An interface to allow the recording of metrics.
`Metric` s or metric instruments, are devices used for capturing raw
measurements. Each metric instrument supports a single method, each with
fixed interpretation to capture measurements.
"""
@abc.abstractmethod
def record_batch(
self,
labels: Dict[str, str],
record_tuples: Sequence[Tuple["Metric", ValueT]],
) -> None:
"""Atomically records a batch of `Metric` and value pairs.
Allows the functionality of acting upon multiple metrics with a single
API call. Implementations should find bound metric instruments that
match the key-value pairs in the labels.
Args:
labels: Labels associated with all measurements in the
batch.
record_tuples: A sequence of pairs of `Metric` s and the
corresponding value to record for that metric.
"""
@abc.abstractmethod
def create_counter(
self,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
enabled: bool = True,
) -> "Counter":
"""Creates a `Counter` metric with type ``value_type``.
Args:
name: The name of the metric.
description: Human-readable description of the metric.
unit: Unit of the metric values following the UCUM convention
(https://unitsofmeasure.org/ucum.html).
value_type: The type of values being recorded by the metric.
enabled: Whether to report the metric by default.
"""
@abc.abstractmethod
def create_updowncounter(
self,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
enabled: bool = True,
) -> "UpDownCounter":
"""Creates a `UpDownCounter` metric with type ``value_type``.
Args:
name: The name of the metric.
description: Human-readable description of the metric.
unit: Unit of the metric values following the UCUM convention
(https://unitsofmeasure.org/ucum.html).
value_type: The type of values being recorded by the metric.
enabled: Whether to report the metric by default.
"""
@abc.abstractmethod
def create_valuerecorder(
self,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
enabled: bool = True,
) -> "ValueRecorder":
"""Creates a `ValueRecorder` metric with type ``value_type``.
Args:
name: The name of the metric.
description: Human-readable description of the metric.
unit: Unit of the metric values following the UCUM convention
(https://unitsofmeasure.org/ucum.html).
value_type: The type of values being recorded by the metric.
enabled: Whether to report the metric by default.
"""
@abc.abstractmethod
def register_sumobserver(
self,
callback: ObserverCallbackT,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
label_keys: Sequence[str] = (),
enabled: bool = True,
) -> "SumObserver":
"""Registers an ``SumObserver`` metric instrument.
Args:
callback: Callback invoked each collection interval with the
observer as argument.
name: The name of the metric.
description: Human-readable description of the metric.
unit: Unit of the metric values following the UCUM convention
(https://unitsofmeasure.org/ucum.html).
value_type: The type of values being recorded by the metric.
label_keys: The keys for the labels with dynamic values.
enabled: Whether to report the metric by default.
Returns: A new ``SumObserver`` metric instrument.
"""
@abc.abstractmethod
def register_updownsumobserver(
self,
callback: ObserverCallbackT,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
label_keys: Sequence[str] = (),
enabled: bool = True,
) -> "UpDownSumObserver":
"""Registers an ``UpDownSumObserver`` metric instrument.
Args:
callback: Callback invoked each collection interval with the
observer as argument.
name: The name of the metric.
description: Human-readable description of the metric.
unit: Unit of the metric values following the UCUM convention
(https://unitsofmeasure.org/ucum.html).
value_type: The type of values being recorded by the metric.
label_keys: The keys for the labels with dynamic values.
enabled: Whether to report the metric by default.
Returns: A new ``UpDownSumObserver`` metric instrument.
"""
@abc.abstractmethod
def register_valueobserver(
self,
callback: ObserverCallbackT,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
label_keys: Sequence[str] = (),
enabled: bool = True,
) -> "ValueObserver":
"""Registers an ``ValueObserver`` metric instrument.
Args:
callback: Callback invoked each collection interval with the
observer as argument.
name: The name of the metric.
description: Human-readable description of the metric.
unit: Unit of the metric values following the UCUM convention
(https://unitsofmeasure.org/ucum.html).
value_type: The type of values being recorded by the metric.
label_keys: The keys for the labels with dynamic values.
enabled: Whether to report the metric by default.
Returns: A new ``ValueObserver`` metric instrument.
"""
@abc.abstractmethod
def unregister_observer(self, observer: "Observer") -> None:
"""Unregisters an ``Observer`` metric instrument.
Args:
observer: The observer to unregister.
"""
class DefaultMeter(Meter):
"""The default Meter used when no Meter implementation is available."""
def record_batch(
self,
labels: Dict[str, str],
record_tuples: Sequence[Tuple["Metric", ValueT]],
) -> None:
pass
def create_counter(
self,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
enabled: bool = True,
) -> "Counter":
# pylint: disable=no-self-use
return DefaultCounter()
def create_updowncounter(
self,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
enabled: bool = True,
) -> "UpDownCounter":
# pylint: disable=no-self-use
return DefaultUpDownCounter()
def create_valuerecorder(
self,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
enabled: bool = True,
) -> "ValueRecorder":
# pylint: disable=no-self-use
return DefaultValueRecorder()
def register_sumobserver(
self,
callback: ObserverCallbackT,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
label_keys: Sequence[str] = (),
enabled: bool = True,
) -> "DefaultSumObserver":
return DefaultSumObserver()
def register_updownsumobserver(
self,
callback: ObserverCallbackT,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
label_keys: Sequence[str] = (),
enabled: bool = True,
) -> "DefaultUpDownSumObserver":
return DefaultUpDownSumObserver()
def register_valueobserver(
self,
callback: ObserverCallbackT,
name: str,
description: str,
unit: str,
value_type: Type[ValueT],
label_keys: Sequence[str] = (),
enabled: bool = True,
) -> "DefaultValueObserver":
return DefaultValueObserver()
def unregister_observer(self, observer: "Observer") -> None:
pass
_METER_PROVIDER = None
def get_meter(
instrumenting_module_name: str,
instrumenting_library_version: str = "",
meter_provider: Optional[MeterProvider] = None,
) -> "Meter":
"""Returns a `Meter` for use by the given instrumentation library.
This function is a convenience wrapper for
opentelemetry.metrics.get_meter_provider().get_meter
If meter_provider is omitted the current configured one is used.
"""
if meter_provider is None:
meter_provider = get_meter_provider()
return meter_provider.get_meter(
instrumenting_module_name, instrumenting_library_version
)
def set_meter_provider(meter_provider: MeterProvider) -> None:
"""Sets the current global :class:`~.MeterProvider` object."""
global _METER_PROVIDER # pylint: disable=global-statement
if _METER_PROVIDER is not None:
logger.warning("Overriding of current MeterProvider is not allowed")
return
_METER_PROVIDER = meter_provider
def get_meter_provider() -> MeterProvider:
"""Gets the current global :class:`~.MeterProvider` object."""
global _METER_PROVIDER # pylint: disable=global-statement
if _METER_PROVIDER is None:
_METER_PROVIDER = _load_meter_provider("meter_provider")
return _METER_PROVIDER