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

PeriodicExportingMetricsReader with value = infinity to support explicit metric collection #3059

Merged
merged 29 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f99a4fb
ExportingMetricsReporter and unit tests added,
howardyoo Nov 29, 2022
3dd484c
included ExportingMetricReader to the list of importable classes
howardyoo Nov 29, 2022
aa5b196
fixed incorrect parameter for the method.
howardyoo Nov 29, 2022
e5f6101
fixed minor bug in unit test code
howardyoo Nov 29, 2022
95e0cc6
fixed a lint issue.
howardyoo Nov 29, 2022
8d6d6df
fixing lint issues on the order of library imports
howardyoo Nov 29, 2022
523b428
another lint fix on the test code on the order of the library imports
howardyoo Nov 29, 2022
b9a2ee2
Merge branch 'main' into main
srikanthccv Dec 10, 2022
3fdca7d
Merge branch 'main' into main
srikanthccv Jan 2, 2023
809c049
1. removed ExportMetricReader related tests and codes
howardyoo Jan 4, 2023
4df9ddc
Merge branch 'main' into main
srikanthccv Jan 8, 2023
d7b52d1
Added example for zero interval periodic exporting metrics reader,
howardyoo Jan 9, 2023
2e4fad3
reformatting...
howardyoo Jan 10, 2023
d0a0664
Merge branch 'main' into main
lzchen Jan 12, 2023
e8bb84a
removed the examples for non-interval reader as a result of SIG discu…
howardyoo Jan 13, 2023
83266b0
Merge branch 'main' into main
lzchen Jan 13, 2023
9b4979a
minor changes in the code comment and changelog
howardyoo Jan 17, 2023
1dc1852
Merge branch 'open-telemetry:main' into main
howardyoo Jan 28, 2023
a891ae0
1) added support for math.inf which will disable interval thread
howardyoo Jan 28, 2023
97fe654
Update CHANGELOG.md
howardyoo Jan 30, 2023
d59be9c
Update opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/expo…
howardyoo Jan 30, 2023
bd4c62e
Merge branch 'main' into main
lzchen Jan 30, 2023
5c06eac
Update opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/expo…
howardyoo Feb 2, 2023
2b5029a
Merge branch 'main' into main
srikanthccv Feb 2, 2023
c042bed
Merge branch 'main' into main
srikanthccv Feb 2, 2023
a5cbed0
Update opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/expo…
howardyoo Feb 2, 2023
24da061
Merge branch 'open-telemetry:main' into main
howardyoo Feb 3, 2023
16330da
fixed incorrect reference to self.
howardyoo Feb 3, 2023
436ad0f
make sure to have the _daemon_thread set to None
howardyoo Feb 3, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Version 1.15.0/0.36b0 (2022-12-09)

- PeriodicExportingMetricsReader with +Inf interval
to support explicit metric collection
([#3059](https://github.com/open-telemetry/opentelemetry-python/pull/3059))
- Regenerate opentelemetry-proto to be compatible with protobuf 3 and 4
([#3070](https://github.com/open-telemetry/opentelemetry-python/pull/3070))
- Rename parse_headers to parse_env_headers and improve error message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import IO, Callable, Dict, Iterable, Optional

from typing_extensions import final
import math

# This kind of import is needed to avoid Sphinx errors.
import opentelemetry.sdk.metrics._internal
Expand Down Expand Up @@ -414,7 +415,8 @@ def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
class PeriodicExportingMetricReader(MetricReader):
"""`PeriodicExportingMetricReader` is an implementation of `MetricReader`
that collects metrics based on a user-configurable time interval, and passes the
metrics to the configured exporter.
metrics to the configured exporter. If the time interval is set to `math.inf`, the
reader will not invoke periodic collection.

The configured exporter's :py:meth:`~MetricExporter.export` method will not be called
concurrently.
Expand Down Expand Up @@ -463,16 +465,25 @@ def __init__(
self._shutdown = False
self._shutdown_event = Event()
self._shutdown_once = Once()
self._daemon_thread = Thread(
name="OtelPeriodicExportingMetricReader",
target=self._ticker,
daemon=True,
)
self._daemon_thread.start()
if hasattr(os, "register_at_fork"):
os.register_at_fork(
after_in_child=self._at_fork_reinit
) # pylint: disable=protected-access
if (
self._export_interval_millis > 0
and self._export_interval_millis < math.inf
):
self._daemon_thread = Thread(
name="OtelPeriodicExportingMetricReader",
target=self._ticker,
daemon=True,
)
self._daemon_thread.start()
if hasattr(os, "register_at_fork"):
os.register_at_fork(
after_in_child=self._at_fork_reinit
) # pylint: disable=protected-access
elif self._export_interval_millis <= 0:
raise ValueError(
f"interval value {self._export_interval_millis} is invalid \
and needs to be larger than zero and lower than infinity."
)

def _at_fork_reinit(self):
self._daemon_thread = Thread(
Expand Down Expand Up @@ -519,7 +530,10 @@ def _shutdown():
return

self._shutdown_event.set()
self._daemon_thread.join(timeout=(deadline_ns - time_ns()) / 10**9)
if this._daemon_thread:
howardyoo marked this conversation as resolved.
Show resolved Hide resolved
self._daemon_thread.join(
timeout=(deadline_ns - time_ns()) / 10**9
)
self._exporter.shutdown(timeout=(deadline_ns - time_ns()) / 10**6)

def force_flush(self, timeout_millis: float = 10_000) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from unittest.mock import Mock

from flaky import flaky
import math

from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics._internal import _Counter
Expand Down Expand Up @@ -134,6 +135,32 @@ def test_ticker_called(self):
self.assertTrue(collect_mock.assert_called_once)
pmr.shutdown()

def test_ticker_not_called_on_infinity(self):
collect_mock = Mock()
exporter = FakeMetricsExporter()
exporter.export = Mock()
pmr = PeriodicExportingMetricReader(exporter, export_interval_millis=math.inf)
pmr._set_collect_callback(collect_mock)
sleep(0.1)
self.assertTrue(collect_mock.assert_not_called)
pmr.shutdown()

def test_ticker_value_exception_on_zero(self):
exporter = FakeMetricsExporter()
exporter.export = Mock()
self.assertRaises(
ValueError, PeriodicExportingMetricReader,
exporter, export_interval_millis=0
)

def test_ticker_value_exception_on_negative(self):
exporter = FakeMetricsExporter()
exporter.export = Mock()
self.assertRaises(
ValueError, PeriodicExportingMetricReader,
exporter, export_interval_millis=-100
)

@flaky(max_runs=3, min_passes=1)
def test_ticker_collects_metrics(self):
exporter = FakeMetricsExporter()
Expand Down