Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MetricReader #2299

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 3 additions & 50 deletions opentelemetry-api/src/opentelemetry/_metrics/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,12 @@
# type: ignore

from abc import ABC, abstractmethod
from collections import abc as collections_abc
from logging import getLogger
from typing import (
Callable,
Generator,
Generic,
Iterable,
Optional,
TypeVar,
Union,
)
from typing import Generic, Optional, TypeVar

# pylint: disable=unused-import; needed for typing and sphinx
from opentelemetry import _metrics as metrics
from opentelemetry._metrics.measurement import Measurement

_TInstrumentCallback = Callable[[], Iterable[Measurement]]
_TInstrumentCallbackGenerator = Generator[Iterable[Measurement], None, None]
TCallback = Union[_TInstrumentCallback, _TInstrumentCallbackGenerator]
InstrumentT = TypeVar("InstrumentT", bound="Instrument")


Expand Down Expand Up @@ -87,45 +74,11 @@ class Asynchronous(Instrument):
def __init__(
self,
name,
callback: TCallback,
*args,
callback,
unit="",
description="",
**kwargs
):
super().__init__(
name, *args, unit=unit, description=description, **kwargs
)

if isinstance(callback, collections_abc.Callable):
self._callback = callback
elif isinstance(callback, collections_abc.Generator):
self._callback = self._wrap_generator_callback(callback)
# FIXME check that callback is a callable or generator

@staticmethod
def _wrap_generator_callback(
generator_callback: _TInstrumentCallbackGenerator,
) -> _TInstrumentCallback:
"""Wraps a generator style callback into a callable one"""
has_items = True

def inner() -> Iterable[Measurement]:
nonlocal has_items
if not has_items:
return []

try:
return next(generator_callback)
except StopIteration:
has_items = False
# FIXME handle the situation where the callback generator has
# run out of measurements
return []

return inner

# FIXME check that callbacks return an iterable of Measurements
super().__init__(name, unit=unit, description=description)


class _Adding(Instrument):
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ include_package_data = True
install_requires =
opentelemetry-api == 1.7.1
opentelemetry-semantic-conventions == 0.26b1
# FIXME Remove when 3.6 is no longer supported
dataclasses == 0.8; python_version < '3.7'

[options.packages.find]
where = src
Expand Down
Loading