@@ -332,73 +332,120 @@ def create_observable_counter(
332332 For example, an observable counter could be used to report system CPU
333333 time periodically. Here is a basic implementation::
334334
335- def cpu_time_callback(options: CallbackOptions) -> Iterable[Observation]:
335+ def cpu_time_callback(
336+ options: CallbackOptions,
337+ ) -> Iterable[Observation]:
336338 observations = []
337339 with open("/proc/stat") as procstat:
338340 procstat.readline() # skip the first line
339341 for line in procstat:
340- if not line.startswith("cpu"): break
342+ if not line.startswith("cpu"):
343+ break
341344 cpu, *states = line.split()
342- observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
343- observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
344- observations.append(Observation(int(states[2]) // 100, {"cpu": cpu, "state": "system"}))
345+ observations.append(
346+ Observation(
347+ int(states[0]) // 100,
348+ {"cpu": cpu, "state": "user"},
349+ )
350+ )
351+ observations.append(
352+ Observation(
353+ int(states[1]) // 100,
354+ {"cpu": cpu, "state": "nice"},
355+ )
356+ )
357+ observations.append(
358+ Observation(
359+ int(states[2]) // 100,
360+ {"cpu": cpu, "state": "system"},
361+ )
362+ )
345363 # ... other states
346364 return observations
347365
366+
348367 meter.create_observable_counter(
349368 "system.cpu.time",
350369 callbacks=[cpu_time_callback],
351370 unit="s",
352- description="CPU time"
371+ description="CPU time",
353372 )
354373
355374 To reduce memory usage, you can use generator callbacks instead of
356375 building the full list::
357376
358- def cpu_time_callback(options: CallbackOptions) -> Iterable[Observation]:
377+ def cpu_time_callback(
378+ options: CallbackOptions,
379+ ) -> Iterable[Observation]:
359380 with open("/proc/stat") as procstat:
360381 procstat.readline() # skip the first line
361382 for line in procstat:
362- if not line.startswith("cpu"): break
383+ if not line.startswith("cpu"):
384+ break
363385 cpu, *states = line.split()
364- yield Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"})
365- yield Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"})
386+ yield Observation(
387+ int(states[0]) // 100,
388+ {"cpu": cpu, "state": "user"},
389+ )
390+ yield Observation(
391+ int(states[1]) // 100,
392+ {"cpu": cpu, "state": "nice"},
393+ )
366394 # ... other states
367395
368396 Alternatively, you can pass a sequence of generators directly instead of a sequence of
369397 callbacks, which each should return iterables of :class:`~opentelemetry.metrics.Observation`::
370398
371- def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Observation]]:
399+ def cpu_time_callback(
400+ states_to_include: set[str],
401+ ) -> Iterable[Iterable[Observation]]:
372402 # accept options sent in from OpenTelemetry
373403 options = yield
374404 while True:
375405 observations = []
376406 with open("/proc/stat") as procstat:
377407 procstat.readline() # skip the first line
378408 for line in procstat:
379- if not line.startswith("cpu"): break
409+ if not line.startswith("cpu"):
410+ break
380411 cpu, *states = line.split()
381412 if "user" in states_to_include:
382- observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
413+ observations.append(
414+ Observation(
415+ int(states[0]) // 100,
416+ {"cpu": cpu, "state": "user"},
417+ )
418+ )
383419 if "nice" in states_to_include:
384- observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
420+ observations.append(
421+ Observation(
422+ int(states[1]) // 100,
423+ {"cpu": cpu, "state": "nice"},
424+ )
425+ )
385426 # ... other states
386427 # yield the observations and receive the options for next iteration
387428 options = yield observations
388429
430+
389431 meter.create_observable_counter(
390432 "system.cpu.time",
391433 callbacks=[cpu_time_callback({"user", "system"})],
392434 unit="s",
393- description="CPU time"
435+ description="CPU time",
394436 )
395437
396438 The :class:`~opentelemetry.metrics.CallbackOptions` contain a timeout which the
397439 callback should respect. For example if the callback does asynchronous work, like
398440 making HTTP requests, it should respect the timeout::
399441
400- def scrape_http_callback(options: CallbackOptions) -> Iterable[Observation]:
401- r = requests.get('http://scrapethis.com', timeout=options.timeout_millis / 10**3)
442+ def scrape_http_callback(
443+ options: CallbackOptions,
444+ ) -> Iterable[Observation]:
445+ r = requests.get(
446+ "http://scrapethis.com",
447+ timeout=options.timeout_millis / 10**3,
448+ )
402449 for value in r.json():
403450 yield Observation(value)
404451
0 commit comments