From 47aa7e468e2e6d3cf7e235869d9a50d16b37ee34 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 4 May 2022 13:28:36 +0200 Subject: [PATCH] fix(otlp-exporters): remove 'if' for undefined checks from tests. --- .../test/OTLPTraceExporter.test.ts | 47 +++++----- .../test/traceHelper.ts | 10 +- .../test/node/CollectorTraceExporter.test.ts | 8 +- .../test/OTLPTraceExporter.test.ts | 8 +- .../test/OTLPMetricExporter.test.ts | 72 +++++++-------- .../test/metricsHelper.ts | 10 +- .../browser/CollectorMetricExporter.test.ts | 92 +++++++++---------- 7 files changed, 114 insertions(+), 133 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index 207ac017d17..ebdb6bd682e 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -176,29 +176,26 @@ const testCollectorExporter = (params: TestParams) => typeof exportedData !== 'undefined', 'resource' + " doesn't exist" ); - let spans; - let resource; - if (exportedData) { - spans = exportedData.scopeSpans[0].spans; - resource = exportedData.resource; - assert.ok( - typeof spans !== 'undefined', - 'spans do not exist' - ); + const spans = exportedData.scopeSpans[0].spans; + const resource = exportedData.resource; - ensureExportedSpanIsCorrect(spans[0]); + assert.ok( + typeof spans !== 'undefined', + 'spans do not exist' + ); - assert.ok( - typeof resource !== 'undefined', - "resource doesn't exist" - ); + ensureExportedSpanIsCorrect(spans[0]); + + assert.ok( + typeof resource !== 'undefined', + "resource doesn't exist" + ); + + ensureResourceIsCorrect(resource); + + ensureMetadataIsCorrect(reqMetadata, params?.metadata); - ensureResourceIsCorrect(resource); - } - if (params.metadata && reqMetadata) { - ensureMetadataIsCorrect(reqMetadata, params.metadata); - } done(); }, 200); }); @@ -247,13 +244,13 @@ const testCollectorExporter = (params: TestParams) => typeof resource !== 'undefined', "resource doesn't exist" ); - if (resource) { - ensureResourceIsCorrect(resource); - } - } - if (params.metadata && reqMetadata) { - ensureMetadataIsCorrect(reqMetadata, params.metadata); + + ensureResourceIsCorrect(resource); + } + + ensureMetadataIsCorrect(reqMetadata, params.metadata); + done(); }, 500); }); diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts index b7476f45f05..5b0d0444a19 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts @@ -320,11 +320,11 @@ export function ensureResourceIsCorrect( } export function ensureMetadataIsCorrect( - actual: grpc.Metadata, - expected: grpc.Metadata + actual?: grpc.Metadata, + expected?: grpc.Metadata ) { //ignore user agent - expected.remove('user-agent'); - actual.remove('user-agent'); - assert.deepStrictEqual(actual.getMap(), expected.getMap()); + expected?.remove('user-agent'); + actual?.remove('user-agent'); + assert.deepStrictEqual(actual?.getMap(), expected?.getMap() ?? {}); } diff --git a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts index 2c14e406a18..bfe9d4ccb4a 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts @@ -230,9 +230,7 @@ describe('OTLPTraceExporter - node with json over http', () => { const json = JSON.parse(responseBody) as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureSpanIsCorrect(span1); - } + ensureSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); @@ -323,9 +321,7 @@ describe('OTLPTraceExporter - node with json over http', () => { const json = JSON.parse(responseBody) as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureSpanIsCorrect(span1); - } + ensureSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); assert.ok(spySetHeader.calledWith('Content-Encoding', 'gzip')); diff --git a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts index 87c4a091a44..56f6aba69ce 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts @@ -156,9 +156,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureProtoSpanIsCorrect(span1); - } + ensureProtoSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); @@ -242,9 +240,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureProtoSpanIsCorrect(span1); - } + ensureProtoSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); assert.ok(spySetHeader.calledWith('Content-Encoding', 'gzip')); 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 1fcdd28b689..76dd71708dd 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 @@ -207,43 +207,43 @@ const testOTLPMetricExporter = (params: TestParams) => setTimeout(() => { assert.ok( typeof exportedData !== 'undefined', - 'resource' + " doesn't exist" + 'resource does not exist' ); - let resource; - if (exportedData) { - resource = exportedData[0].resource; - const counter = - exportedData[0].scopeMetrics[0].metrics[0]; - const observableGauge = - exportedData[0].scopeMetrics[0].metrics[1]; - const histogram = - exportedData[0].scopeMetrics[0].metrics[2]; - ensureExportedCounterIsCorrect( - counter, - counter.sum?.dataPoints[0].timeUnixNano, - counter.sum?.dataPoints[0].startTimeUnixNano - ); - ensureExportedObservableGaugeIsCorrect( - observableGauge, - observableGauge.gauge?.dataPoints[0].timeUnixNano, - observableGauge.gauge?.dataPoints[0].startTimeUnixNano - ); - ensureExportedHistogramIsCorrect( - histogram, - histogram.histogram?.dataPoints[0].timeUnixNano, - histogram.histogram?.dataPoints[0].startTimeUnixNano, - [0, 100], - ['0', '2', '0'] - ); - assert.ok( - typeof resource !== 'undefined', - "resource doesn't exist" - ); - ensureResourceIsCorrect(resource); - } - if (params.metadata && reqMetadata) { - ensureMetadataIsCorrect(reqMetadata, params.metadata); - } + + assert.ok(exportedData, 'exportedData does not exist'); + + const resource = exportedData[0].resource; + const counter = + exportedData[0].scopeMetrics[0].metrics[0]; + const observableGauge = + exportedData[0].scopeMetrics[0].metrics[1]; + const histogram = + exportedData[0].scopeMetrics[0].metrics[2]; + ensureExportedCounterIsCorrect( + counter, + counter.sum?.dataPoints[0].timeUnixNano, + counter.sum?.dataPoints[0].startTimeUnixNano + ); + ensureExportedObservableGaugeIsCorrect( + observableGauge, + observableGauge.gauge?.dataPoints[0].timeUnixNano, + observableGauge.gauge?.dataPoints[0].startTimeUnixNano + ); + ensureExportedHistogramIsCorrect( + histogram, + histogram.histogram?.dataPoints[0].timeUnixNano, + histogram.histogram?.dataPoints[0].startTimeUnixNano, + [0, 100], + ['0', '2', '0'] + ); + assert.ok( + typeof resource !== 'undefined', + "resource doesn't exist" + ); + ensureResourceIsCorrect(resource); + + ensureMetadataIsCorrect(reqMetadata, params.metadata); + done(); }, 500); }); 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 7c73fc01058..7f33ceb9efa 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts @@ -258,11 +258,11 @@ export function ensureResourceIsCorrect( } export function ensureMetadataIsCorrect( - actual: grpc.Metadata, - expected: grpc.Metadata + actual?: grpc.Metadata, + expected?: grpc.Metadata ) { //ignore user agent - expected.remove('user-agent'); - actual.remove('user-agent'); - assert.deepStrictEqual(actual.getMap(), expected.getMap()); + expected?.remove('user-agent'); + actual?.remove('user-agent'); + assert.deepStrictEqual(actual?.getMap(), expected?.getMap() ?? {}); } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts index 7bcb9a669a6..21cec954f9f 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts @@ -112,41 +112,37 @@ describe('OTLPMetricExporter - web', () => { const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); - if (metric1) { - ensureCounterIsCorrect( - metric1, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) - ); - } + + ensureCounterIsCorrect( + metric1, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) + ); + assert.ok( typeof metric2 !== 'undefined', "second metric doesn't exist" ); - if (metric2) { - ensureObservableGaugeIsCorrect( - metric2, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), - 6, - 'double-observable-gauge2' - ); - } + ensureObservableGaugeIsCorrect( + metric2, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), + 6, + 'double-observable-gauge2' + ); assert.ok( typeof metric3 !== 'undefined', "third metric doesn't exist" ); - if (metric3) { - ensureHistogramIsCorrect( - metric3, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), - [0, 100], - [0, 2, 0] - ); - } + ensureHistogramIsCorrect( + metric3, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), + [0, 100], + [0, 2, 0] + ); const resource = json.resourceMetrics[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); @@ -221,41 +217,37 @@ describe('OTLPMetricExporter - web', () => { const metric1 = json.resourceMetrics[0].scopeMetrics[0].metrics[0]; const metric2 = json.resourceMetrics[0].scopeMetrics[0].metrics[1]; const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; + assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); - if (metric1) { - ensureCounterIsCorrect( - metric1, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) - ); - } + ensureCounterIsCorrect( + metric1, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) + ); + assert.ok( typeof metric2 !== 'undefined', "second metric doesn't exist" ); - if (metric2) { - ensureObservableGaugeIsCorrect( - metric2, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), - 6, - 'double-observable-gauge2' - ); - } + ensureObservableGaugeIsCorrect( + metric2, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), + 6, + 'double-observable-gauge2' + ); assert.ok( typeof metric3 !== 'undefined', "third metric doesn't exist" ); - if (metric3) { - ensureHistogramIsCorrect( - metric3, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), - [0, 100], - [0, 2, 0] - ); - } + ensureHistogramIsCorrect( + metric3, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), + [0, 100], + [0, 2, 0] + ); const resource = json.resourceMetrics[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist");