Skip to content

Commit

Permalink
fix(otlp-exporters): remove 'if' for undefined checks from tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed May 4, 2022
1 parent 35d9762 commit 47aa7e4
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?? {});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?? {});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 47aa7e4

Please sign in to comment.