Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
hist
  • Loading branch information
lzchen committed Apr 1, 2022
commit b844c31f0413679791c21092720318d476ba74b3
13 changes: 10 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,25 @@ def collect(self) -> HistogramPoint:
value = self._bucket_counts
start_time_unix_nano = self._start_time_unix_nano
histogram_sum = self._sum
if self._record_min_max:
histogram_max = self._max
histogram_min = self._min

self._bucket_counts = self._get_empty_bucket_counts()
self._start_time_unix_nano = now + 1
self._sum = 0
self._min = inf
self._max = -inf

return HistogramPoint(
start_time_unix_nano=start_time_unix_nano,
time_unix_nano=now,
aggregation_temporality=AggregationTemporality.DELTA,
bucket_counts=tuple(value),
explicit_bounds=self._boundaries,
aggregation_temporality=AggregationTemporality.DELTA,
max=histogram_max,
min=histogram_min,
start_time_unix_nano=start_time_unix_nano,
sum=histogram_sum,
time_unix_nano=now,
)


Expand Down
14 changes: 8 additions & 6 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import json
from dataclasses import asdict, dataclass
from enum import IntEnum
from typing import Sequence, Union
from typing import Optional, Sequence, Union

from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
Expand All @@ -30,11 +30,11 @@ class AggregationTemporality(IntEnum):

@dataclass(frozen=True)
class Sum:
aggregation_temporality: AggregationTemporality
is_monotonic: bool
start_time_unix_nano: int
time_unix_nano: int
value: Union[int, float]
aggregation_temporality: AggregationTemporality
is_monotonic: bool


@dataclass(frozen=True)
Expand All @@ -45,12 +45,14 @@ class Gauge:

@dataclass(frozen=True)
class Histogram:
start_time_unix_nano: int
time_unix_nano: int
aggregation_temporality: AggregationTemporality
bucket_counts: Sequence[int]
explicit_bounds: Sequence[float]
max: Optional[int]
min: Optional[int]
start_time_unix_nano: int
sum: Union[int, float]
aggregation_temporality: AggregationTemporality
time_unix_nano: int


PointT = Union[Sum, Gauge, Histogram]
Expand Down
18 changes: 11 additions & 7 deletions opentelemetry-sdk/tests/metrics/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ class TestDatapointToJSON(TestCase):
def test_sum(self):
point = _create_metric(
Sum(
aggregation_temporality=2,
is_monotonic=True,
start_time_unix_nano=10,
time_unix_nano=20,
value=9,
aggregation_temporality=2,
is_monotonic=True,
)
)
print(point.to_json())
self.assertEqual(
'{"attributes": {"attr-key": "test-val"}, "description": "test-description", "instrumentation_info": "InstrumentationInfo(name, version, None)", "name": "test-name", "resource": "BoundedAttributes({\'resource-key\': \'resource-val\'}, maxlen=None)", "unit": "test-unit", "point": {"start_time_unix_nano": 10, "time_unix_nano": 20, "value": 9, "aggregation_temporality": 2, "is_monotonic": true}}',
'{"attributes": {"attr-key": "test-val"}, "description": "test-description", "instrumentation_info": "InstrumentationInfo(name, version, None)", "name": "test-name", "resource": "BoundedAttributes({\'resource-key\': \'resource-val\'}, maxlen=None)", "unit": "test-unit", "point": {"aggregation_temporality": 2, "is_monotonic": true, "start_time_unix_nano": 10, "time_unix_nano": 20, "value": 9}}',
point.to_json(),
)

Expand All @@ -59,16 +60,19 @@ def test_gauge(self):
def test_histogram(self):
point = _create_metric(
Histogram(
start_time_unix_nano=50,
time_unix_nano=60,
aggregation_temporality=1,
bucket_counts=[0, 0, 1, 0],
explicit_bounds=[0.1, 0.5, 0.9, 1],
aggregation_temporality=1,
max=0.8,
min=0.8,
start_time_unix_nano=50,
sum=0.8,
time_unix_nano=60,
)
)
self.maxDiff = None
print(point.to_json())
self.assertEqual(
'{"attributes": {"attr-key": "test-val"}, "description": "test-description", "instrumentation_info": "InstrumentationInfo(name, version, None)", "name": "test-name", "resource": "BoundedAttributes({\'resource-key\': \'resource-val\'}, maxlen=None)", "unit": "test-unit", "point": {"start_time_unix_nano": 50, "time_unix_nano": 60, "bucket_counts": [0, 0, 1, 0], "explicit_bounds": [0.1, 0.5, 0.9, 1], "sum": 0.8, "aggregation_temporality": 1}}',
'{"attributes": {"attr-key": "test-val"}, "description": "test-description", "instrumentation_info": "InstrumentationInfo(name, version, None)", "name": "test-name", "resource": "BoundedAttributes({\'resource-key\': \'resource-val\'}, maxlen=None)", "unit": "test-unit", "point": {"aggregation_temporality": 1, "bucket_counts": [0, 0, 1, 0], "explicit_bounds": [0.1, 0.5, 0.9, 1], "max": 0.8, "min": 0.8, "start_time_unix_nano": 50, "sum": 0.8, "time_unix_nano": 60}}',
point.to_json(),
)