Skip to content

Commit

Permalink
Add mean property for distribution value (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
marianhromiak authored Jun 25, 2020
1 parent 05b1b99 commit 1846676
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
3 changes: 3 additions & 0 deletions contrib/opencensus-ext-stackdriver/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Add mean property for distribution values
([#919](https://github.com/census-instrumentation/opencensus-python/pull/919))

## 0.7.2
Released 2019-08-26

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def _convert_point(self, metric, ts, point, sd_point):
sd_dist_val.count = point.value.count
sd_dist_val.sum_of_squared_deviation =\
point.value.sum_of_squared_deviation
sd_dist_val.mean = point.value.sum / sd_dist_val.count

assert sd_dist_val.bucket_options.explicit_buckets.bounds == []
sd_dist_val.bucket_options.explicit_buckets.bounds.extend(
Expand Down
60 changes: 40 additions & 20 deletions contrib/opencensus-ext-stackdriver/tests/test_stackdriver_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,20 +822,16 @@ def test_create_timeseries(self, monitor_resource_mock):

self.assertEqual(len(time_series.points), 1)
value = time_series.points[0].value
self.assertEqual(value.distribution_value.count, 1)

time_series_list = exporter.create_time_series_list(v_data)

self.assertEqual(len(time_series_list), 1)
time_series = time_series_list[0]
self.check_labels(
time_series.metric.labels, {FRONTEND_KEY_CLEAN: "1200"},
include_opencensus=True)
self.assertIsNotNone(time_series.resource)

self.assertEqual(len(time_series.points), 1)
value = time_series.points[0].value
self.assertEqual(value.distribution_value.count, 1)
expected_distb = google.api.distribution_pb2.Distribution(
count=1,
mean=26214400.0,
bucket_options=google.api.distribution_pb2.Distribution.BucketOptions( # noqa
explicit_buckets=google.api.distribution_pb2.Distribution.BucketOptions.Explicit( # noqa
bounds=[0.0, 16777216.0, 268435456.0])),
bucket_counts=[0, 0, 1, 0]
)
self.assertEqual(value.distribution_value, expected_distb)

@mock.patch('opencensus.ext.stackdriver.stats_exporter.'
'monitored_resource.get_instance')
Expand Down Expand Up @@ -1213,7 +1209,16 @@ def test_create_timeseries_multiple_tag_values(self,

self.assertEqual(len(ts1.points), 1)
value1 = ts1.points[0].value
self.assertEqual(value1.distribution_value.count, 1)

expected_distb = google.api.distribution_pb2.Distribution(
count=1,
mean=26214400.0,
bucket_options=google.api.distribution_pb2.Distribution.BucketOptions( # noqa
explicit_buckets=google.api.distribution_pb2.Distribution.BucketOptions.Explicit( # noqa
bounds=[0.0, 16777216.0, 268435456.0])),
bucket_counts=[0, 0, 1, 0]
)
self.assertEqual(value1.distribution_value, expected_distb)

# Verify second time series
self.assertEqual(ts2.resource.type, "global")
Expand All @@ -1224,7 +1229,16 @@ def test_create_timeseries_multiple_tag_values(self,

self.assertEqual(len(ts2.points), 1)
value2 = ts2.points[0].value
self.assertEqual(value2.distribution_value.count, 1)

expected_distb = google.api.distribution_pb2.Distribution(
count=1,
mean=12582912.0,
bucket_options=google.api.distribution_pb2.Distribution.BucketOptions( # noqa
explicit_buckets=google.api.distribution_pb2.Distribution.BucketOptions.Explicit( # noqa
bounds=[0.0, 16777216.0, 268435456.0])),
bucket_counts=[0, 1, 0, 0]
)
self.assertEqual(value2.distribution_value, expected_distb)

@mock.patch('opencensus.ext.stackdriver.stats_exporter.'
'monitored_resource.get_instance',
Expand Down Expand Up @@ -1315,12 +1329,18 @@ def test_create_timeseries_from_distribution(self):
include_opencensus=True)
self.assertEqual(len(time_series.points), 1)
[point] = time_series.points

dv = point.value.distribution_value
self.assertEqual(100, dv.count)
self.assertEqual(825.0, dv.sum_of_squared_deviation)
self.assertEqual([0, 20, 20, 20, 20, 20], dv.bucket_counts)
self.assertEqual([0, 2, 4, 6, 8],
dv.bucket_options.explicit_buckets.bounds)
expected_distb = google.api.distribution_pb2.Distribution(
count=100,
mean=4.5,
sum_of_squared_deviation=825.0,
bucket_options=google.api.distribution_pb2.Distribution.BucketOptions( # noqa
explicit_buckets=google.api.distribution_pb2.Distribution.BucketOptions.Explicit( # noqa
bounds=[0, 2, 4, 6, 8])),
bucket_counts=[0, 20, 20, 20, 20, 20]
)
self.assertEqual(dv, expected_distb)

def test_create_timeseries_multiple_tags(self):
"""Check that exporter creates timeseries for multiple tag values.
Expand Down

0 comments on commit 1846676

Please sign in to comment.