Skip to content

Commit ba97fe9

Browse files
ocelotllzchen
andauthored
Add support for zero or more callbacks (#2602)
* Add support for zero or more callbacks Fixes #2601 * Add changelog entry * Update opentelemetry-api/src/opentelemetry/_metrics/__init__.py Co-authored-by: Leighton Chen <[email protected]> * Update opentelemetry-api/src/opentelemetry/_metrics/__init__.py Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Leighton Chen <[email protected]>
1 parent 77d96ba commit ba97fe9

File tree

12 files changed

+223
-124
lines changed

12 files changed

+223
-124
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.10.0-0.29b0...HEAD)
99

10+
- Add support for zero or more callbacks
11+
([#2602](https://github.com/open-telemetry/opentelemetry-python/pull/2602))
1012
- Fix parsing of trace flags when extracting traceparent
1113
([#2577](https://github.com/open-telemetry/opentelemetry-python/pull/2577))
1214
- Add default aggregation

opentelemetry-api/src/opentelemetry/_metrics/__init__.py

+31-30
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ def create_up_down_counter(
181181

182182
@abstractmethod
183183
def create_observable_counter(
184-
self, name, callback, unit="", description=""
184+
self, name, callbacks=None, unit="", description=""
185185
) -> ObservableCounter:
186186
"""Creates an `ObservableCounter` instrument
187187
188188
An observable counter observes a monotonically increasing count by
189-
calling a provided callback which returns multiple
189+
calling provided callbacks which returns multiple
190190
:class:`~opentelemetry._metrics.measurement.Measurement`.
191191
192192
For example, an observable counter could be used to report system CPU
@@ -207,7 +207,7 @@ def cpu_time_callback() -> Iterable[Measurement]:
207207
208208
meter.create_observable_counter(
209209
"system.cpu.time",
210-
callback=cpu_time_callback,
210+
callbacks=[cpu_time_callback],
211211
unit="s",
212212
description="CPU time"
213213
)
@@ -225,8 +225,8 @@ def cpu_time_callback() -> Iterable[Measurement]:
225225
yield Measurement(int(states[1]) // 100, {"cpu": cpu, "state": "nice"})
226226
# ... other states
227227
228-
Alternatively, you can pass a generator directly instead of a callback,
229-
which should return iterables of
228+
Alternatively, you can pass a sequence of generators directly instead
229+
of a sequence of callbacks, which each should return iterables of
230230
:class:`~opentelemetry._metrics.measurement.Measurement`::
231231
232232
def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Measurement]]:
@@ -246,16 +246,17 @@ def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Measurem
246246
247247
meter.create_observable_counter(
248248
"system.cpu.time",
249-
callback=cpu_time_callback({"user", "system"}),
249+
callbacks=[cpu_time_callback({"user", "system"})],
250250
unit="s",
251251
description="CPU time"
252252
)
253253
254254
Args:
255255
name: The name of the instrument to be created
256-
callback: A callback that returns an iterable of
256+
callbacks: A sequence of callbacks that return an iterable of
257257
:class:`~opentelemetry._metrics.measurement.Measurement`.
258-
Alternatively, can be a generator that yields iterables of
258+
Alternatively, can be a sequence of generators that each yields
259+
iterables of
259260
:class:`~opentelemetry._metrics.measurement.Measurement`.
260261
unit: The unit for measurements this instrument reports. For
261262
example, ``By`` for bytes. UCUM units are recommended.
@@ -275,13 +276,13 @@ def create_histogram(self, name, unit="", description="") -> Histogram:
275276

276277
@abstractmethod
277278
def create_observable_gauge(
278-
self, name, callback, unit="", description=""
279+
self, name, callbacks=None, unit="", description=""
279280
) -> ObservableGauge:
280281
"""Creates an `ObservableGauge` instrument
281282
282283
Args:
283284
name: The name of the instrument to be created
284-
callback: A callback that returns an iterable of
285+
callbacks: A sequence of callbacks that return an iterable of
285286
:class:`~opentelemetry._metrics.measurement.Measurement`.
286287
Alternatively, can be a generator that yields iterables of
287288
:class:`~opentelemetry._metrics.measurement.Measurement`.
@@ -292,13 +293,13 @@ def create_observable_gauge(
292293

293294
@abstractmethod
294295
def create_observable_up_down_counter(
295-
self, name, callback, unit="", description=""
296+
self, name, callbacks=None, unit="", description=""
296297
) -> ObservableUpDownCounter:
297298
"""Creates an `ObservableUpDownCounter` instrument
298299
299300
Args:
300301
name: The name of the instrument to be created
301-
callback: A callback that returns an iterable of
302+
callbacks: A sequence of callbacks that return an iterable of
302303
:class:`~opentelemetry._metrics.measurement.Measurement`.
303304
Alternatively, can be a generator that yields iterables of
304305
:class:`~opentelemetry._metrics.measurement.Measurement`.
@@ -358,15 +359,15 @@ def create_up_down_counter(
358359
return proxy
359360

360361
def create_observable_counter(
361-
self, name, callback, unit="", description=""
362+
self, name, callbacks=None, unit="", description=""
362363
) -> ObservableCounter:
363364
with self._lock:
364365
if self._real_meter:
365366
return self._real_meter.create_observable_counter(
366-
name, callback, unit, description
367+
name, callbacks, unit, description
367368
)
368369
proxy = _ProxyObservableCounter(
369-
name, callback, unit=unit, description=description
370+
name, callbacks, unit=unit, description=description
370371
)
371372
self._instruments.append(proxy)
372373
return proxy
@@ -382,32 +383,32 @@ def create_histogram(self, name, unit="", description="") -> Histogram:
382383
return proxy
383384

384385
def create_observable_gauge(
385-
self, name, callback, unit="", description=""
386+
self, name, callbacks=None, unit="", description=""
386387
) -> ObservableGauge:
387388
with self._lock:
388389
if self._real_meter:
389390
return self._real_meter.create_observable_gauge(
390-
name, callback, unit, description
391+
name, callbacks, unit, description
391392
)
392393
proxy = _ProxyObservableGauge(
393-
name, callback, unit=unit, description=description
394+
name, callbacks, unit=unit, description=description
394395
)
395396
self._instruments.append(proxy)
396397
return proxy
397398

398399
def create_observable_up_down_counter(
399-
self, name, callback, unit="", description=""
400+
self, name, callbacks=None, unit="", description=""
400401
) -> ObservableUpDownCounter:
401402
with self._lock:
402403
if self._real_meter:
403404
return self._real_meter.create_observable_up_down_counter(
404405
name,
405-
callback,
406+
callbacks,
406407
unit,
407408
description,
408409
)
409410
proxy = _ProxyObservableUpDownCounter(
410-
name, callback, unit=unit, description=description
411+
name, callbacks, unit=unit, description=description
411412
)
412413
self._instruments.append(proxy)
413414
return proxy
@@ -454,11 +455,11 @@ def create_up_down_counter(
454455
return DefaultUpDownCounter(name, unit=unit, description=description)
455456

456457
def create_observable_counter(
457-
self, name, callback, unit="", description=""
458+
self, name, callbacks=None, unit="", description=""
458459
) -> ObservableCounter:
459460
"""Returns a no-op ObservableCounter."""
460461
super().create_observable_counter(
461-
name, callback, unit=unit, description=description
462+
name, callbacks, unit=unit, description=description
462463
)
463464
if self._check_instrument_id(
464465
name, DefaultObservableCounter, unit, description
@@ -473,7 +474,7 @@ def create_observable_counter(
473474
)
474475
return DefaultObservableCounter(
475476
name,
476-
callback,
477+
callbacks,
477478
unit=unit,
478479
description=description,
479480
)
@@ -495,11 +496,11 @@ def create_histogram(self, name, unit="", description="") -> Histogram:
495496
return DefaultHistogram(name, unit=unit, description=description)
496497

497498
def create_observable_gauge(
498-
self, name, callback, unit="", description=""
499+
self, name, callbacks=None, unit="", description=""
499500
) -> ObservableGauge:
500501
"""Returns a no-op ObservableGauge."""
501502
super().create_observable_gauge(
502-
name, callback, unit=unit, description=description
503+
name, callbacks, unit=unit, description=description
503504
)
504505
if self._check_instrument_id(
505506
name, DefaultObservableGauge, unit, description
@@ -514,17 +515,17 @@ def create_observable_gauge(
514515
)
515516
return DefaultObservableGauge(
516517
name,
517-
callback,
518+
callbacks,
518519
unit=unit,
519520
description=description,
520521
)
521522

522523
def create_observable_up_down_counter(
523-
self, name, callback, unit="", description=""
524+
self, name, callbacks=None, unit="", description=""
524525
) -> ObservableUpDownCounter:
525526
"""Returns a no-op ObservableUpDownCounter."""
526527
super().create_observable_up_down_counter(
527-
name, callback, unit=unit, description=description
528+
name, callbacks, unit=unit, description=description
528529
)
529530
if self._check_instrument_id(
530531
name, DefaultObservableUpDownCounter, unit, description
@@ -539,7 +540,7 @@ def create_observable_up_down_counter(
539540
)
540541
return DefaultObservableUpDownCounter(
541542
name,
542-
callback,
543+
callbacks,
543544
unit=unit,
544545
description=description,
545546
)

opentelemetry-api/src/opentelemetry/_metrics/instrument.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ def _create_real_instrument(self, meter: "metrics.Meter") -> InstrumentT:
7373

7474

7575
class _ProxyAsynchronousInstrument(_ProxyInstrument[InstrumentT]):
76-
def __init__(self, name, callback, unit, description) -> None:
76+
def __init__(self, name, callbacks, unit, description) -> None:
7777
super().__init__(name, unit, description)
78-
self._callback = callback
78+
self._callbacks = callbacks
7979

8080

8181
class Synchronous(Instrument):
@@ -87,7 +87,7 @@ class Asynchronous(Instrument):
8787
def __init__(
8888
self,
8989
name,
90-
callback,
90+
callbacks=None,
9191
unit="",
9292
description="",
9393
):
@@ -170,8 +170,8 @@ class ObservableCounter(_Monotonic, Asynchronous):
170170

171171

172172
class DefaultObservableCounter(ObservableCounter):
173-
def __init__(self, name, callback, unit="", description=""):
174-
super().__init__(name, callback, unit=unit, description=description)
173+
def __init__(self, name, callbacks=None, unit="", description=""):
174+
super().__init__(name, callbacks, unit=unit, description=description)
175175

176176

177177
class _ProxyObservableCounter(
@@ -181,7 +181,7 @@ def _create_real_instrument(
181181
self, meter: "metrics.Meter"
182182
) -> ObservableCounter:
183183
return meter.create_observable_counter(
184-
self._name, self._callback, self._unit, self._description
184+
self._name, self._callbacks, self._unit, self._description
185185
)
186186

187187

@@ -193,8 +193,8 @@ class ObservableUpDownCounter(_NonMonotonic, Asynchronous):
193193

194194

195195
class DefaultObservableUpDownCounter(ObservableUpDownCounter):
196-
def __init__(self, name, callback, unit="", description=""):
197-
super().__init__(name, callback, unit=unit, description=description)
196+
def __init__(self, name, callbacks=None, unit="", description=""):
197+
super().__init__(name, callbacks, unit=unit, description=description)
198198

199199

200200
class _ProxyObservableUpDownCounter(
@@ -205,7 +205,7 @@ def _create_real_instrument(
205205
self, meter: "metrics.Meter"
206206
) -> ObservableUpDownCounter:
207207
return meter.create_observable_up_down_counter(
208-
self._name, self._callback, self._unit, self._description
208+
self._name, self._callbacks, self._unit, self._description
209209
)
210210

211211

@@ -247,8 +247,8 @@ class ObservableGauge(_Grouping, Asynchronous):
247247

248248

249249
class DefaultObservableGauge(ObservableGauge):
250-
def __init__(self, name, callback, unit="", description=""):
251-
super().__init__(name, callback, unit=unit, description=description)
250+
def __init__(self, name, callbacks=None, unit="", description=""):
251+
super().__init__(name, callbacks, unit=unit, description=description)
252252

253253

254254
class _ProxyObservableGauge(
@@ -259,5 +259,5 @@ def _create_real_instrument(
259259
self, meter: "metrics.Meter"
260260
) -> ObservableGauge:
261261
return meter.create_observable_gauge(
262-
self._name, self._callback, self._unit, self._description
262+
self._name, self._callbacks, self._unit, self._description
263263
)

0 commit comments

Comments
 (0)