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 last_updated_timestamp for observers #522

Merged
merged 16 commits into from
Apr 2, 2020
32 changes: 22 additions & 10 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ def take_checkpoint(self):
def merge(self, other):
with self._lock:
self.checkpoint += other.checkpoint
self.last_update_timestamp = (
other.last_update_timestamp or self.last_update_timestamp
)
if (other.last_update_timestamp is not None and
self.last_update_timestamp is not None):
if self.last_update_timestamp < other.last_update_timestamp:
self.last_update_timestamp = other.last_update_timestamp
elif self.last_update_timestamp is None:
self.last_update_timestamp = other.last_update_timestamp


class MinMaxSumCountAggregator(Aggregator):
Expand Down Expand Up @@ -120,9 +123,12 @@ def merge(self, other):
self.checkpoint = self._merge_checkpoint(
self.checkpoint, other.checkpoint
)
self.last_update_timestamp = (
other.last_update_timestamp or self.last_update_timestamp
)
if (other.last_update_timestamp is not None and
self.last_update_timestamp is not None):
if self.last_update_timestamp < other.last_update_timestamp:
self.last_update_timestamp = other.last_update_timestamp
elif self.last_update_timestamp is None:
self.last_update_timestamp = other.last_update_timestamp


class ObserverAggregator(Aggregator):
Expand All @@ -148,12 +154,18 @@ def take_checkpoint(self):

def merge(self, other):
self.mmsc.merge(other.mmsc)
last = self.checkpoint.last
if (other.last_update_timestamp is not None and
self.last_update_timestamp is not None):
if self.last_update_timestamp < other.last_update_timestamp:
self.last_update_timestamp = other.last_update_timestamp
last = other.checkpoint.last
elif self.last_update_timestamp is None:
self.last_update_timestamp = other.last_update_timestamp
last = other.checkpoint.last
self.checkpoint = self._TYPE(
*(
self.mmsc.checkpoint
+ (other.checkpoint.last or self.checkpoint.last or 0,)
+ (last,)
)
)
self.last_update_timestamp = (
other.last_update_timestamp or self.last_update_timestamp
)
3 changes: 3 additions & 0 deletions opentelemetry-sdk/tests/metrics/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def test_merge(self):
mmsc1.checkpoint = checkpoint1
mmsc2.checkpoint = checkpoint2

mmsc1.last_update_timestamp = 100
mmsc2.last_update_timestamp = 123

mmsc1.merge(mmsc2)
Expand Down Expand Up @@ -491,6 +492,7 @@ def test_merge(self):
observer1.mmsc.checkpoint = mmsc_checkpoint1
observer2.mmsc.checkpoint = mmsc_checkpoint2

observer1.last_update_timestamp = 100
observer2.last_update_timestamp = 123

observer1.checkpoint = checkpoint1
Expand Down Expand Up @@ -519,6 +521,7 @@ def test_merge_with_empty(self):

observer1.mmsc.checkpoint = mmsc_checkpoint1
observer1.checkpoint = checkpoint1
observer1.last_update_timestamp = 100

observer1.merge(observer2)

Expand Down