Skip to content

Commit 7898d31

Browse files
committed
fix(sdk-metrics): ignore in accumulation instead
1 parent 185515b commit 7898d31

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

packages/sdk-metrics/src/Instruments.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ export class SyncInstrument {
5454
);
5555
return;
5656
}
57-
if (Number.isNaN(value)) {
58-
diag.warn(
59-
`NaN provided to metric ${this._descriptor.name}, ignoring to preserve the integrity of the metrics stream`
60-
);
61-
return;
62-
}
6357
if (
6458
this._descriptor.valueType === ValueType.INT &&
6559
!Number.isInteger(value)

packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ export class ExponentialHistogramAccumulation implements Accumulation {
193193
* @param increment
194194
*/
195195
updateByIncrement(value: number, increment: number) {
196+
// NaN does not fall into any bucket, is not zero and should not be counted,
197+
// NaN is never greater than max nor less than min, therefore return as there's nothing for us to do.
198+
if (Number.isNaN(value)) {
199+
return;
200+
}
201+
196202
if (value > this._max) {
197203
this._max = value;
198204
}

packages/sdk-metrics/src/aggregator/Histogram.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export class HistogramAccumulation implements Accumulation {
7272
) {}
7373

7474
record(value: number): void {
75+
// NaN does not fall into any bucket, is not zero and should not be counted,
76+
// NaN is never greater than max nor less than min, therefore return as there's nothing for us to do.
77+
if (Number.isNaN(value)) {
78+
return;
79+
}
80+
7581
this._current.count += 1;
7682
this._current.sum += value;
7783

packages/sdk-metrics/test/Instruments.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ describe('Instruments', () => {
135135
counter.add(1.2, { foo: 'bar' });
136136
// non-number values should be ignored.
137137
counter.add('1' as any);
138-
counter.add(NaN);
139138

140139
await validateExport(cumulativeReader, {
141140
dataPointType: DataPointType.SUM,
@@ -248,7 +247,6 @@ describe('Instruments', () => {
248247
upDownCounter.add(1.1, { foo: 'bar' });
249248
// non-number values should be ignored.
250249
upDownCounter.add('1' as any);
251-
upDownCounter.add(NaN);
252250

253251
await validateExport(deltaReader, {
254252
dataPointType: DataPointType.SUM,

packages/sdk-metrics/test/aggregator/ExponentialHistogram.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,20 @@ describe('ExponentialHistogramAccumulation', () => {
215215
}
216216
});
217217
});
218+
219+
it('ignores NaN', () => {
220+
const accumulation = new ExponentialHistogramAccumulation([0, 0], 1);
221+
222+
accumulation.record(NaN);
223+
224+
assert.strictEqual(accumulation.scale, 0);
225+
assert.strictEqual(accumulation.max, -Infinity);
226+
assert.strictEqual(accumulation.min, Infinity);
227+
assert.strictEqual(accumulation.sum, 0);
228+
assert.strictEqual(accumulation.count, 0);
229+
assert.deepStrictEqual(getCounts(accumulation.positive), []);
230+
assert.deepStrictEqual(getCounts(accumulation.negative), []);
231+
});
218232
});
219233
describe('merge', () => {
220234
it('handles simple (even) case', () => {

packages/sdk-metrics/test/aggregator/Histogram.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,19 @@ describe('HistogramAccumulation', () => {
257257
accumulation.record(value);
258258
}
259259
});
260+
261+
it('ignores NaN', () => {
262+
const accumulation = new HistogramAccumulation([0, 0], [1, 10, 100]);
263+
264+
accumulation.record(NaN);
265+
266+
const pointValue = accumulation.toPointValue();
267+
assert.strictEqual(pointValue.max, -Infinity);
268+
assert.strictEqual(pointValue.min, Infinity);
269+
assert.strictEqual(pointValue.sum, 0);
270+
assert.strictEqual(pointValue.count, 0);
271+
assert.deepStrictEqual(pointValue.buckets.counts, [0, 0, 0, 0]);
272+
});
260273
});
261274

262275
describe('setStartTime', () => {

0 commit comments

Comments
 (0)