Skip to content

Commit

Permalink
feat: add support for Distributions (#125)
Browse files Browse the repository at this point in the history
* initial update

* Update after api testing

* Adding optional param to Distribution type

* fix lint attempt

* fix lint and tests

* fix lint

* fix tests

* throw error for incompatible distributionValue aggregated

* update test

* fix lint

* fix lint

* update comment
  • Loading branch information
TigerHe7 authored Sep 1, 2020
1 parent 2db3776 commit 0c92cde
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
17 changes: 13 additions & 4 deletions packages/opentelemetry-cloud-monitoring-exporter/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,21 @@ function transformValue(
value: number | OTDistribution | OTHistogram
) {
if (isDistributionValue(value)) {
// TODO: Add support for Distribution
throw new Error('Distributions are not yet supported');
throw Error('unsupported distribution value type');
// no buckets aggregated, which is a required param in `distributionValue` for Cloud Monitoring v3
}
if (isHistogramValue(value)) {
// TODO: Add support for Histogram
throw new Error('Histograms are not yet supported');
return {
distributionValue: {
// sumOfSquaredDeviation param not aggregated
count: value.count,
mean: value.sum / value.count,
bucketOptions: {
explicitBuckets: { bounds: value.buckets.boundaries },
},
bucketCounts: value.buckets.counts,
},
};
}

if (valueType === OTValueType.INT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface Point {
export interface Distribution {
count: number;
mean: number;
sumOfSquaredDeviation: number;
sumOfSquaredDeviation?: number;
bucketOptions: { explicitBuckets: { bounds: Bucket[] } };
bucketCounts: number[];
exemplars?: Exemplar[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe('transform', () => {
assert(!result.interval.startTime);
});

it('should throw an error when given a distribution value', () => {
it('should export a distribution value', () => {
const metricDescriptor: OTMetricDescriptor = {
name: METRIC_NAME,
description: METRIC_DESCRIPTION,
Expand All @@ -359,19 +359,16 @@ describe('transform', () => {
timestamp: process.hrtime(),
};

try {
TEST_ONLY.transformPoint(
assert.throws(() => {
return TEST_ONLY.transformPoint(
point,
metricDescriptor,
new Date().toISOString()
);
assert.fail('should have thrown an error');
} catch (err) {
assert(err.message.toLowerCase().includes('distributions'));
}
}, /unsupported distribution value type/);
});

it('should thrown an error when given a histogram value', () => {
it('should export a histogram value', () => {
const metricDescriptor: OTMetricDescriptor = {
name: METRIC_NAME,
description: METRIC_DESCRIPTION,
Expand All @@ -391,16 +388,26 @@ describe('transform', () => {
timestamp: process.hrtime(),
};

try {
TEST_ONLY.transformPoint(
point,
metricDescriptor,
new Date().toISOString()
);
assert.fail('should have thrown an error');
} catch (err) {
assert(err.message.toLowerCase().includes('histograms'));
}
const result = TEST_ONLY.transformPoint(
point,
metricDescriptor,
new Date().toISOString()
);

assert.deepStrictEqual(result.value, {
distributionValue: {
bucketCounts: [1, 2],
bucketOptions: {
explicitBuckets: {
bounds: [10, 30],
},
},
count: 3,
mean: 23.333333333333332,
},
});
assert(result.interval.endTime);
assert(result.interval.startTime);
});
});
});

0 comments on commit 0c92cde

Please sign in to comment.