Skip to content

Commit e8a9bc5

Browse files
committed
Handle all cases for current and previous buckets and scale
1 parent 73190ae commit e8a9bc5

File tree

1 file changed

+47
-9
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal

1 file changed

+47
-9
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

+47-9
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,34 @@ def collect(
809809

810810
# TODO, implement this case
811811

812-
if current_value_positive is None:
813-
# This happens if collect is called before aggregate is
814-
# called or if collect is called twice with no calls to
815-
# aggregate in between.
812+
if (
813+
current_value_positive is None and
814+
self._previous_cumulative_value_positive is None
815+
):
816+
# This happens if collect is called for the first time
817+
# and aggregate has not yet been called.
816818
current_value_positive = Buckets()
817-
if current_value_negative is None:
819+
self._previous_cumulative_value_positive = (
820+
current_value_positive.copy_empty()
821+
)
822+
if (
823+
current_value_negative is None and
824+
self._previous_cumulative_value_negative is None
825+
):
818826
current_value_negative = Buckets()
827+
self._previous_cumulative_value_negative = (
828+
current_value_negative.copy_empty()
829+
)
830+
if scale is None and self._previous_scale is None:
831+
scale = self._mapping.scale
832+
self._previous_scale = scale
819833

820-
if self._previous_cumulative_value_positive is None:
821-
# This happens when collect is called the very first time.
834+
if (
835+
current_value_positive is not None and
836+
self._previous_cumulative_value_positive is None
837+
):
838+
# This happens when collect is called the very first time
839+
# and aggregate has been called before.
822840

823841
# We need previous buckets to add them to the current ones.
824842
# When collect is called for the first time, there are no
@@ -839,13 +857,33 @@ def collect(
839857
self._previous_cumulative_value_positive = (
840858
current_value_positive.copy_empty()
841859
)
842-
if self._previous_cumulative_value_negative is None:
860+
if (
861+
current_value_negative is not None and
862+
self._previous_cumulative_value_negative is None
863+
):
843864
self._previous_cumulative_value_negative = (
844865
current_value_negative.copy_empty()
845866
)
846-
if self._previous_scale is None:
867+
if scale is not None and self._previous_scale is None:
847868
self._previous_scale = scale
848869

870+
if (
871+
current_value_positive is None and
872+
self._previous_cumulative_value_positive is not None
873+
):
874+
current_value_positive = (
875+
self._previous_cumulative_value_positive.copy_empty()
876+
)
877+
if (
878+
current_value_negative is None and
879+
self._previous_cumulative_value_negative is not None
880+
):
881+
current_value_negative = (
882+
self._previous_cumulative_value_negative.copy_empty()
883+
)
884+
if scale is None and self._previous_scale is not None:
885+
scale = self._previous_scale
886+
849887
min_scale = min(self._previous_scale, scale)
850888

851889
low_positive, high_positive = (

0 commit comments

Comments
 (0)