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 variadic arguments to metric exporter/reader interfaces #2654

Merged
merged 5 commits into from
May 4, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.11.1-0.30b1...HEAD)

- Add variadic arguments to metric exporter/reader interfaces
([#2654](https://github.com/open-telemetry/opentelemetry-python/pull/2654))
- Move Metrics API behind internal package
([#2651](https://github.com/open-telemetry/opentelemetry-python/pull/2651))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ def _translate_data(
)
)

def export(self, metrics: Sequence[Metric]) -> MetricExportResult:
def export(
self, metrics: Sequence[Metric], *args, **kwargs
) -> MetricExportResult:
return self._export(metrics)

def shutdown(self):
def shutdown(self, *args, **kwargs):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ def __init__(self, prefix: str = "") -> None:
REGISTRY.register(self._collector)
self._collector._callback = self.collect

def _receive_metrics(self, metrics: Iterable[Metric]) -> None:
def _receive_metrics(
self, metrics: Iterable[Metric], *args, **kwargs
) -> None:
if metrics is None:
return
self._collector.add_metrics_data(metrics)

def shutdown(self) -> bool:
def shutdown(self, *args, **kwargs) -> bool:
REGISTRY.unregister(self._collector)
return True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class MetricExporter(ABC):
"""

@abstractmethod
def export(self, metrics: Sequence[Metric]) -> "MetricExportResult":
def export(
self, metrics: Sequence[Metric], *args, **kwargs
) -> "MetricExportResult":
"""Exports a batch of telemetry data.

Args:
Expand All @@ -63,7 +65,7 @@ def export(self, metrics: Sequence[Metric]) -> "MetricExportResult":
"""

@abstractmethod
def shutdown(self) -> None:
def shutdown(self, *args, **kwargs) -> None:
"""Shuts down the exporter.

Called when the SDK is shut down.
Expand All @@ -87,13 +89,15 @@ def __init__(
self.out = out
self.formatter = formatter

def export(self, metrics: Sequence[Metric]) -> MetricExportResult:
def export(
self, metrics: Sequence[Metric], *args, **kwargs
) -> MetricExportResult:
for metric in metrics:
self.out.write(self.formatter(metric))
self.out.flush()
return MetricExportResult.SUCCESS

def shutdown(self) -> None:
def shutdown(self, *args, **kwargs) -> None:
pass


Expand Down Expand Up @@ -123,11 +127,11 @@ def get_metrics(self) -> List[Metric]:
self._metrics = []
return metrics

def _receive_metrics(self, metrics: Iterable[Metric]):
def _receive_metrics(self, metrics: Iterable[Metric], *args, **kwargs):
with self._lock:
self._metrics = list(metrics)

def shutdown(self):
def shutdown(self, *args, **kwargs):
pass


Expand Down Expand Up @@ -193,7 +197,9 @@ def _ticker(self) -> None:
# one last collection below before shutting down completely
self.collect()

def _receive_metrics(self, metrics: Iterable[Metric]) -> None:
def _receive_metrics(
self, metrics: Iterable[Metric], *args, **kwargs
) -> None:
if metrics is None:
return
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
Expand All @@ -203,7 +209,7 @@ def _receive_metrics(self, metrics: Iterable[Metric]) -> None:
_logger.exception("Exception while exporting metrics %s", str(e))
detach(token)

def shutdown(self):
def shutdown(self, *args, **kwargs):
def _shutdown():
self._shutdown = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ def _set_collect_callback(
self._collect = func

@abstractmethod
def _receive_metrics(self, metrics: Iterable[Metric]):
def _receive_metrics(self, metrics: Iterable[Metric], *args, **kwargs):
"""Called by `MetricReader.collect` when it receives a batch of metrics"""

@abstractmethod
def shutdown(self):
def shutdown(self, *args, **kwargs):
"""Shuts down the MetricReader. This method provides a way
for the MetricReader to do any cleanup required. A metric reader can
only be shutdown once, any subsequent calls are ignored and return
Expand Down