From 1f9a214885c3b8d5af57ae730099a171e7236b4d Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 30 Mar 2022 14:07:26 +0200 Subject: [PATCH] feat(otlp-exporter): cleanup. --- .../test/OTLPMetricExporter.test.ts | 4 - .../test/metricsHelper.ts | 17 ++-- .../src/transformMetrics.ts | 78 +++++++++---------- .../test/metricsHelper.ts | 38 ++++----- .../test/metricsHelper.ts | 19 +++-- 5 files changed, 76 insertions(+), 80 deletions(-) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts index ebe9925f744..e90b06327cd 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts @@ -130,10 +130,6 @@ const testOTLPMetricExporter = (params: TestParams) => metadata: params.metadata, aggregationTemporality: AggregationTemporality.CUMULATIVE }); - // Overwrites the start time to make tests consistent - Object.defineProperty(collectorExporter, '_startTime', { - value: 1592602232694000000, - }); setUp(); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts index b208dabf392..a64cd408c56 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts @@ -32,25 +32,25 @@ export class TestMetricReader extends MetricReader { } } -const defaultResource = Resource.default().merge(new Resource({ +const testResource = Resource.default().merge(new Resource({ service: 'ui', version: 1, cost: 112.12, })); -let meterProvider = new MeterProvider({ resource: defaultResource }); +let meterProvider = new MeterProvider({ resource: testResource }); + let reader = new TestMetricReader(); -meterProvider.addMetricReader( - reader -); +meterProvider.addMetricReader(reader); + let meter = meterProvider.getMeter('default', '0.0.1'); -export async function collect(){ +export async function collect() { return (await reader.collect())!; } export function setUp() { - meterProvider = new MeterProvider({ resource: defaultResource }); + meterProvider = new MeterProvider({ resource: testResource }); reader = new TestMetricReader(); meterProvider.addMetricReader( reader @@ -62,7 +62,6 @@ export async function shutdown() { await meterProvider.shutdown(); } - export function mockCounter(): Counter { const name = 'int-counter'; return meter.createCounter(name, { @@ -87,7 +86,7 @@ export function mockObservableGauge( export function mockHistogram(): Histogram { const name = 'int-histogram'; - meterProvider.addView({aggregation: new HistogramAggregation([0,100])}); + meterProvider.addView({ aggregation: new HistogramAggregation([0, 100]) }); return meter.createHistogram(name, { description: 'sample histogram description', diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/transformMetrics.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/transformMetrics.ts index c7803ec22e6..52cb8f6e1b7 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/transformMetrics.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/transformMetrics.ts @@ -28,7 +28,7 @@ import { import { Attributes, ValueType } from '@opentelemetry/api-metrics'; /** - * Converts labels + * Converts {@link Attributes} to a collector-compatible format. * @param attributes */ export function toCollectorAttributes( @@ -40,7 +40,7 @@ export function toCollectorAttributes( } /** - * Given a {@link AggregationTemporality}, return its temporality in a compatible format with the collector + * Convert {@link AggregationTemporality} to a collector-compatible format. * @param aggregationTemporality */ export function toAggregationTemporality( @@ -60,44 +60,44 @@ export function toAggregationTemporality( } /** - * Returns an array of DataPoints which can have integers or double values - * @param metric + * Convert {@link MetricData} of {@link DataPointType.SINGULAR} to a collector-compatible format. + * @param metricData */ -export function toDataPoints( - metric: MetricData +export function toSingularDataPoints( + metricData: MetricData ): otlpTypes.opentelemetryProto.metrics.v1.DataPoint[] { - return Array.from(metric.dataPoints.map(pointData => { + return Array.from(metricData.dataPoints.map(dataPoint => { return { - labels: toCollectorAttributes(pointData.attributes), - value: pointData.value as number, + labels: toCollectorAttributes(dataPoint.attributes), + value: dataPoint.value as number, startTimeUnixNano: core.hrTimeToNanoseconds( - pointData.startTime + dataPoint.startTime ), timeUnixNano: core.hrTimeToNanoseconds( - pointData.endTime + dataPoint.endTime ), }; })); } /** - * Returns an array of HistogramPoints to the collector - * @param metric + * Convert {@link MetricData} of {@link DataPointType.HISTOGRAM} to a collector-compatible format. + * @param metricData */ -export function toHistogramPoints( - metric: MetricData +export function toHistogramDataPoints( + metricData: MetricData ): otlpTypes.opentelemetryProto.metrics.v1.HistogramDataPoint[] { - return Array.from(metric.dataPoints.map(pointData => { - const histogram = pointData.value as Histogram; + return Array.from(metricData.dataPoints.map(dataPoints => { + const histogram = dataPoints.value as Histogram; return { - labels: toCollectorAttributes(pointData.attributes), + labels: toCollectorAttributes(dataPoints.attributes), sum: histogram.sum, count: histogram.count, startTimeUnixNano: core.hrTimeToNanoseconds( - pointData.startTime + dataPoints.startTime ), timeUnixNano: core.hrTimeToNanoseconds( - pointData.endTime + dataPoints.endTime ), bucketCounts: histogram.buckets.counts, explicitBounds: histogram.buckets.boundaries, @@ -106,54 +106,54 @@ export function toHistogramPoints( } /** - * Converts a metric to be compatible with the collector - * @param metric + * Converts {@link MetricData} to a collector-compatible format. + * @param metricData * @param aggregationTemporality */ export function toCollectorMetric( - metric: MetricData, + metricData: MetricData, aggregationTemporality: AggregationTemporality ): otlpTypes.opentelemetryProto.metrics.v1.Metric { const metricCollector: otlpTypes.opentelemetryProto.metrics.v1.Metric = { - name: metric.descriptor.name, - description: metric.descriptor.description, - unit: metric.descriptor.unit, + name: metricData.descriptor.name, + description: metricData.descriptor.description, + unit: metricData.descriptor.unit, }; - if (metric.dataPointType === DataPointType.SINGULAR) { + if (metricData.dataPointType === DataPointType.SINGULAR) { const result = { - dataPoints: toDataPoints(metric), + dataPoints: toSingularDataPoints(metricData), isMonotonic: - metric.descriptor.type === InstrumentType.COUNTER || - metric.descriptor.type === InstrumentType.OBSERVABLE_COUNTER, + metricData.descriptor.type === InstrumentType.COUNTER || + metricData.descriptor.type === InstrumentType.OBSERVABLE_COUNTER, aggregationTemporality: toAggregationTemporality(aggregationTemporality), }; if ( - metric.descriptor.type === InstrumentType.COUNTER || - metric.descriptor.type === InstrumentType.OBSERVABLE_COUNTER || - metric.descriptor.type === InstrumentType.UP_DOWN_COUNTER || - metric.descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER + metricData.descriptor.type === InstrumentType.COUNTER || + metricData.descriptor.type === InstrumentType.OBSERVABLE_COUNTER || + metricData.descriptor.type === InstrumentType.UP_DOWN_COUNTER || + metricData.descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER ) { - if (metric.descriptor.valueType === ValueType.INT) { + if (metricData.descriptor.valueType === ValueType.INT) { metricCollector.intSum = result; } else { metricCollector.doubleSum = result; } } else{ // Instrument is a gauge. - if (metric.descriptor.valueType === ValueType.INT) { + if (metricData.descriptor.valueType === ValueType.INT) { metricCollector.intGauge = result; } else { metricCollector.doubleGauge = result; } } - } else if (metric.dataPointType === DataPointType.HISTOGRAM) { + } else if (metricData.dataPointType === DataPointType.HISTOGRAM) { const result = { - dataPoints: toHistogramPoints(metric), + dataPoints: toHistogramDataPoints(metricData), aggregationTemporality: toAggregationTemporality(aggregationTemporality) }; - if (metric.descriptor.valueType === ValueType.INT) { + if (metricData.descriptor.valueType === ValueType.INT) { metricCollector.intHistogram = result; } else { metricCollector.doubleHistogram = result; diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index c64d43aefaf..78b29999eef 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -21,6 +21,14 @@ import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; +if (typeof Buffer === 'undefined') { + (window as any).Buffer = { + from: function (arr: []) { + return new Uint8Array(arr); + }, + }; +} + export class TestMetricReader extends MetricReader { protected onForceFlush(): Promise { return Promise.resolve(undefined); @@ -44,12 +52,8 @@ meterProvider.addMetricReader( ); let meter = meterProvider.getMeter('default', '0.0.1'); -if (typeof Buffer === 'undefined') { - (window as any).Buffer = { - from: function (arr: []) { - return new Uint8Array(arr); - }, - }; +export async function collect() { + return (await reader.collect())!; } export function setUp() { @@ -65,10 +69,6 @@ export async function shutdown() { await meterProvider.shutdown(); } -export async function collect() { - return (await reader.collect())!; -} - export function mockCounter(): Counter { const name = 'int-counter'; return meter.createCounter(name, { @@ -77,14 +77,6 @@ export function mockCounter(): Counter { }); } -export function mockDoubleCounter(): Counter { - const name = 'double-counter'; - return meter.createCounter(name, { - description: 'sample counter description', - valueType: ValueType.DOUBLE, - }); -} - export function mockObservableGauge( callback: (observableResult: ObservableResult) => void, name = 'double-observable-gauge' @@ -99,6 +91,16 @@ export function mockObservableGauge( ); } +export function mockDoubleCounter(): Counter { + const name = 'double-counter'; + return meter.createCounter(name, { + description: 'sample counter description', + valueType: ValueType.DOUBLE, + }); +} + + + export function mockObservableCounter( callback: (observableResult: ObservableResult) => void, name = 'double-observable-counter' diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index f49448d3483..d2280982b58 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -36,26 +36,25 @@ export class TestMetricReader extends MetricReader { } } -const defaultResource = new Resource({ +const testResource = Resource.default().merge(new Resource({ service: 'ui', version: 1, cost: 112.12, -}); +})); + +let meterProvider = new MeterProvider({ resource: testResource }); -let meterProvider = new MeterProvider({ resource: defaultResource }); let reader = new TestMetricReader(); -meterProvider.addMetricReader( - reader -); +meterProvider.addMetricReader(reader); + let meter = meterProvider.getMeter('default', '0.0.1'); -export async function collect(){ +export async function collect() { return (await reader.collect())!; } - export function setUp() { - meterProvider = new MeterProvider({ resource: defaultResource }); + meterProvider = new MeterProvider({ resource: testResource }); reader = new TestMetricReader(); meterProvider.addMetricReader( reader @@ -91,7 +90,7 @@ export function mockObservableGauge( export function mockHistogram(): Histogram { const name = 'int-histogram'; - meterProvider.addView({aggregation: new HistogramAggregation([0,100])}); + meterProvider.addView({ aggregation: new HistogramAggregation([0, 100]) }); return meter.createHistogram(name, { description: 'sample histogram description',