Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 07e94f7

Browse files
authored
Metrics: Fix DistributionValue buckets properties (#258)
* Fix DistributionValue buckets * Make exemplar optional As per metrics.proto, we omit exemplar field when the distribution does not have a histogram.
1 parent 52c304e commit 07e94f7

4 files changed

Lines changed: 11 additions & 9 deletions

File tree

packages/opencensus-core/src/metrics/export/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export interface Bucket {
238238
/**
239239
* If the distribution does not have a histogram, then omit this field.
240240
*/
241-
readonly exemplar: Exemplar;
241+
readonly exemplar?: Exemplar;
242242
}
243243

244244
/**

packages/opencensus-core/src/stats/view.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import * as defaultLogger from '../common/console-logger';
1818
import * as loggerTypes from '../common/types';
19-
import {LabelValue, Metric, MetricDescriptor, MetricDescriptorType, Point, TimeSeries, Timestamp} from '../metrics/export/types';
19+
import {DistributionValue, LabelValue, Metric, MetricDescriptor, MetricDescriptorType, Point, TimeSeries, Timestamp} from '../metrics/export/types';
2020

2121
import {BucketBoundaries} from './bucket-boundaries';
2222
import {MetricUtils} from './metric-utils';
@@ -254,10 +254,12 @@ export class BaseView implements View {
254254
sum,
255255
sumOfSquaredDeviation,
256256
bucketOptions: {explicit: {bounds: data.buckets}},
257-
buckets: data.bucketCounts
258-
};
257+
// Bucket without an Exemplar.
258+
buckets:
259+
data.bucketCounts.map(bucketCount => ({count: bucketCount}))
260+
} as DistributionValue;
259261
} else {
260-
value = data.value;
262+
value = data.value as number;
261263
}
262264
return {timestamp, value};
263265
}

packages/opencensus-core/test/test-metric-producer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe('Metric producer for stats', () => {
9898
points: [{
9999
value: {
100100
'bucketOptions': {'explicit': {'bounds': [2, 4, 6]}},
101-
'buckets': [1, 2, 2, 0],
101+
'buckets': [{count: 1}, {count: 2}, {count: 2}, {count: 0}],
102102
'count': 5,
103103
'sum': 16.099999999999998,
104104
'sumOfSquaredDeviation': 10.427999999999997

packages/opencensus-core/test/test-view.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ describe('BaseView', () => {
292292
assert.notEqual(typeof value, 'number');
293293
assert.deepStrictEqual((value as DistributionValue), {
294294
bucketOptions: {explicit: {bounds: buckets}},
295-
buckets: [1, 2, 2, 0],
295+
buckets: [{count: 1}, {count: 2}, {count: 2}, {count: 0}],
296296
count: 5,
297297
sum: total,
298298
sumOfSquaredDeviation: 10.427999999999997
@@ -334,7 +334,7 @@ describe('BaseView', () => {
334334
assert.notEqual(typeof value, 'number');
335335
assert.deepStrictEqual((value as DistributionValue), {
336336
bucketOptions: {explicit: {bounds: buckets}},
337-
buckets: [1, 2, 2, 0],
337+
buckets: [{count: 1}, {count: 2}, {count: 2}, {count: 0}],
338338
count: 5,
339339
sum: total,
340340
sumOfSquaredDeviation: 10.427999999999997
@@ -353,7 +353,7 @@ describe('BaseView', () => {
353353
assert.notEqual(typeof value, 'number');
354354
assert.deepStrictEqual((value as DistributionValue), {
355355
bucketOptions: {explicit: {bounds: buckets}},
356-
buckets: [1, 2, 2, 0],
356+
buckets: [{count: 1}, {count: 2}, {count: 2}, {count: 0}],
357357
count: 5,
358358
sum: total,
359359
sumOfSquaredDeviation: 10.427999999999997

0 commit comments

Comments
 (0)