diff --git a/packages/opentelemetry-cloud-monitoring-exporter/src/transform.ts b/packages/opentelemetry-cloud-monitoring-exporter/src/transform.ts index 89132464..b3d5c76f 100644 --- a/packages/opentelemetry-cloud-monitoring-exporter/src/transform.ts +++ b/packages/opentelemetry-cloud-monitoring-exporter/src/transform.ts @@ -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) { diff --git a/packages/opentelemetry-cloud-monitoring-exporter/src/types.ts b/packages/opentelemetry-cloud-monitoring-exporter/src/types.ts index 38b21e6f..1ddeba53 100644 --- a/packages/opentelemetry-cloud-monitoring-exporter/src/types.ts +++ b/packages/opentelemetry-cloud-monitoring-exporter/src/types.ts @@ -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[]; diff --git a/packages/opentelemetry-cloud-monitoring-exporter/test/transform.test.ts b/packages/opentelemetry-cloud-monitoring-exporter/test/transform.test.ts index 7d178f01..e6d851a7 100644 --- a/packages/opentelemetry-cloud-monitoring-exporter/test/transform.test.ts +++ b/packages/opentelemetry-cloud-monitoring-exporter/test/transform.test.ts @@ -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, @@ -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, @@ -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); }); }); });