Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Distributions #125

Merged
merged 13 commits into from
Sep 1, 2020
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 },
aabmass marked this conversation as resolved.
Show resolved Hide resolved
},
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);
});
});
});