19
19
from abc import ABC , abstractmethod
20
20
from collections import abc as collections_abc
21
21
from logging import getLogger
22
- from re import compile as compile_
23
22
from typing import Callable , Generator , Iterable , Union
24
23
25
24
from opentelemetry .metrics .measurement import Measurement
33
32
34
33
35
34
class Instrument (ABC ):
36
-
37
- _name_regex = compile_ (r"[a-zA-Z][-.\w]{0,62}" )
38
-
39
- @property
40
- def name (self ):
41
- return self ._name
42
-
43
- @property
44
- def unit (self ):
45
- return self ._unit
46
-
47
- @property
48
- def description (self ):
49
- return self ._description
50
-
51
35
@abstractmethod
52
36
def __init__ (self , name , unit = "" , description = "" ):
37
+ pass
53
38
54
- if name is None or self ._name_regex .fullmatch (name ) is None :
55
- _logger .error ("Invalid instrument name %s" , name )
56
-
57
- else :
58
- self ._name = name
59
-
60
- if unit is None :
61
- self ._unit = ""
62
- elif len (unit ) > 63 :
63
- _logger .error ("unit must be 63 characters or shorter" )
64
-
65
- elif any (ord (character ) > 127 for character in unit ):
66
- _logger .error ("unit must only contain ASCII characters" )
67
- else :
68
- self ._unit = unit
69
-
70
- if description is None :
71
- description = ""
72
-
73
- self ._description = description
39
+ # FIXME check that the instrument name is valid
40
+ # FIXME check that the unit is 63 characters or shorter
41
+ # FIXME check that the unit contains only ASCII characters
74
42
75
43
76
44
class Synchronous (Instrument ):
@@ -96,11 +64,10 @@ def __init__(
96
64
self ._callback = callback
97
65
elif isinstance (callback , collections_abc .Generator ):
98
66
self ._callback = self ._wrap_generator_callback (callback )
99
- else :
100
- _logger .error ("callback must be a callable or generator" )
67
+ # FIXME check that callback is a callable or generator
101
68
69
+ @staticmethod
102
70
def _wrap_generator_callback (
103
- self ,
104
71
generator_callback : _TInstrumentCallbackGenerator ,
105
72
) -> _TInstrumentCallback :
106
73
"""Wraps a generator style callback into a callable one"""
@@ -115,30 +82,13 @@ def inner() -> Iterable[Measurement]:
115
82
return next (generator_callback )
116
83
except StopIteration :
117
84
has_items = False
118
- _logger .error (
119
- "callback generator for instrument %s ran out of measurements" ,
120
- self ._name ,
121
- )
85
+ # FIXME handle the situation where the callback generator has
86
+ # run out of measurements
122
87
return []
123
88
124
89
return inner
125
90
126
- def callback (self ):
127
- measurements = self ._callback ()
128
- if not isinstance (measurements , collections_abc .Iterable ):
129
- _logger .error (
130
- "Callback must return an iterable of Measurement, got %s" ,
131
- type (measurements ),
132
- )
133
- return
134
- for measurement in measurements :
135
- if not isinstance (measurement , Measurement ):
136
- _logger .error (
137
- "Callback must return an iterable of Measurement, "
138
- "iterable contained type %s" ,
139
- type (measurement ),
140
- )
141
- yield measurement
91
+ # FIXME check that callbacks return an iterable of Measurements
142
92
143
93
144
94
class _Adding (Instrument ):
@@ -160,8 +110,8 @@ class _NonMonotonic(_Adding):
160
110
class Counter (_Monotonic , Synchronous ):
161
111
@abstractmethod
162
112
def add (self , amount , attributes = None ):
163
- if amount < 0 :
164
- _logger . error ( "Amount must be non-negative" )
113
+ # FIXME check that the amount is non negative
114
+ pass
165
115
166
116
167
117
class DefaultCounter (Counter ):
@@ -187,13 +137,7 @@ def add(self, amount, attributes=None):
187
137
188
138
189
139
class ObservableCounter (_Monotonic , Asynchronous ):
190
- def callback (self ):
191
- measurements = super ().callback ()
192
-
193
- for measurement in measurements :
194
- if measurement .value < 0 :
195
- _logger .error ("Amount must be non-negative" )
196
- yield measurement
140
+ pass
197
141
198
142
199
143
class DefaultObservableCounter (ObservableCounter ):
0 commit comments