26
26
27
27
28
28
"""
29
- from typing import Callable , Optional , Sequence , Tuple , Type , TypeVar
29
+ import abc
30
+ from typing import Callable , Dict , Optional , Sequence , Tuple , Type , TypeVar
30
31
31
32
from opentelemetry .util import loader
32
33
@@ -67,14 +68,33 @@ def record(self, value: ValueT) -> None:
67
68
"""
68
69
69
70
71
+ class LabelSet (abc .ABC ):
72
+ """A canonicalized set of labels useful for preaggregation
73
+
74
+ Re-usable LabelSet objects provide a potential optimization for scenarios
75
+ where handles might not be effective. For example, if the LabelSet will be
76
+ re-used but only used once per metrics, handles do not offer any
77
+ optimization. It may best to pre-compute a canonicalized LabelSet once and
78
+ re-use it with the direct calling convention. LabelSets are immutable and
79
+ should be opaque in implementation.
80
+ """
81
+
82
+
83
+ class DefaultLabelSet (LabelSet ):
84
+ """The default LabelSet.
85
+
86
+ Used when no LabelSet implementation is available.
87
+ """
88
+
89
+
70
90
class Metric :
71
91
"""Base class for various types of metrics.
72
92
73
93
Metric class that inherit from this class are specialized with the type of
74
94
handle that the metric holds.
75
95
"""
76
96
77
- def get_handle (self , label_values : Sequence [ str ] ) -> "object" :
97
+ def get_handle (self , label_set : LabelSet ) -> "object" :
78
98
"""Gets a handle, used for repeated-use of metrics instruments.
79
99
80
100
Handles are useful to reduce the cost of repeatedly recording a metric
@@ -85,34 +105,34 @@ def get_handle(self, label_values: Sequence[str]) -> "object":
85
105
a value was not provided are permitted.
86
106
87
107
Args:
88
- label_values: Values to associate with the returned handle.
108
+ label_set: `LabelSet` to associate with the returned handle.
89
109
"""
90
110
91
111
92
112
class DefaultMetric (Metric ):
93
113
"""The default Metric used when no Metric implementation is available."""
94
114
95
- def get_handle (self , label_values : Sequence [ str ] ) -> "DefaultMetricHandle" :
115
+ def get_handle (self , label_set : LabelSet ) -> "DefaultMetricHandle" :
96
116
"""Gets a `DefaultMetricHandle`.
97
117
98
118
Args:
99
- label_values: The label values associated with the handle.
119
+ label_set: `LabelSet` to associate with the returned handle.
100
120
"""
101
121
return DefaultMetricHandle ()
102
122
103
123
104
124
class Counter (Metric ):
105
125
"""A counter type metric that expresses the computation of a sum."""
106
126
107
- def get_handle (self , label_values : Sequence [ str ] ) -> "CounterHandle" :
127
+ def get_handle (self , label_set : LabelSet ) -> "CounterHandle" :
108
128
"""Gets a `CounterHandle`."""
109
129
return CounterHandle ()
110
130
111
- def add (self , label_values : Sequence [ str ] , value : ValueT ) -> None :
131
+ def add (self , label_set : LabelSet , value : ValueT ) -> None :
112
132
"""Increases the value of the counter by ``value``.
113
133
114
134
Args:
115
- label_values: The label values associated with the metric .
135
+ label_set: `LabelSet` to associate with the returned handle .
116
136
value: The value to add to the counter metric.
117
137
"""
118
138
@@ -126,15 +146,15 @@ class Gauge(Metric):
126
146
the measurement interval is arbitrary.
127
147
"""
128
148
129
- def get_handle (self , label_values : Sequence [ str ] ) -> "GaugeHandle" :
149
+ def get_handle (self , label_set : LabelSet ) -> "GaugeHandle" :
130
150
"""Gets a `GaugeHandle`."""
131
151
return GaugeHandle ()
132
152
133
- def set (self , label_values : Sequence [ str ] , value : ValueT ) -> None :
153
+ def set (self , label_set : LabelSet , value : ValueT ) -> None :
134
154
"""Sets the value of the gauge to ``value``.
135
155
136
156
Args:
137
- label_values: The label values associated with the metric .
157
+ label_set: `LabelSet` to associate with the returned handle .
138
158
value: The value to set the gauge metric to.
139
159
"""
140
160
@@ -147,15 +167,15 @@ class Measure(Metric):
147
167
Negative inputs will be discarded when monotonic is True.
148
168
"""
149
169
150
- def get_handle (self , label_values : Sequence [ str ] ) -> "MeasureHandle" :
170
+ def get_handle (self , label_set : LabelSet ) -> "MeasureHandle" :
151
171
"""Gets a `MeasureHandle` with a float value."""
152
172
return MeasureHandle ()
153
173
154
- def record (self , label_values : Sequence [ str ] , value : ValueT ) -> None :
174
+ def record (self , label_set : LabelSet , value : ValueT ) -> None :
155
175
"""Records the ``value`` to the measure.
156
176
157
177
Args:
158
- label_values: The label values associated with the metric .
178
+ label_set: `LabelSet` to associate with the returned handle .
159
179
value: The value to record to this measure metric.
160
180
"""
161
181
@@ -174,7 +194,7 @@ class Meter:
174
194
175
195
def record_batch (
176
196
self ,
177
- label_values : Sequence [ str ] ,
197
+ label_set : LabelSet ,
178
198
record_tuples : Sequence [Tuple ["Metric" , ValueT ]],
179
199
) -> None :
180
200
"""Atomically records a batch of `Metric` and value pairs.
@@ -184,7 +204,7 @@ def record_batch(
184
204
match the key-value pairs in the label tuples.
185
205
186
206
Args:
187
- label_values : The label values associated with all measurements in
207
+ label_set : The `LabelSet` associated with all measurements in
188
208
the batch. A measurement is a tuple, representing the `Metric`
189
209
being recorded and the corresponding value to record.
190
210
record_tuples: A sequence of pairs of `Metric` s and the
@@ -211,8 +231,6 @@ def create_metric(
211
231
value_type: The type of values being recorded by the metric.
212
232
metric_type: The type of metric being created.
213
233
label_keys: The keys for the labels with dynamic values.
214
- Order of the sequence is important as the same order must be
215
- used on recording when suppling values for these labels.
216
234
enabled: Whether to report the metric by default.
217
235
monotonic: Whether to only allow non-negative values.
218
236
@@ -221,6 +239,17 @@ def create_metric(
221
239
# pylint: disable=no-self-use
222
240
return DefaultMetric ()
223
241
242
+ def get_label_set (self , labels : Dict [str , str ]) -> "LabelSet" :
243
+ """Gets a `LabelSet` with the given labels.
244
+
245
+ Args:
246
+ labels: A dictionary representing label key to label value pairs.
247
+
248
+ Returns: A `LabelSet` object canonicalized using the given input.
249
+ """
250
+ # pylint: disable=no-self-use
251
+ return DefaultLabelSet ()
252
+
224
253
225
254
# Once https://github.com/python/mypy/issues/7092 is resolved,
226
255
# the following type definition should be replaced with
0 commit comments