|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import { |
| 19 | + Aggregation, |
| 20 | + AggregationTemporality, |
| 21 | + MeterProvider, |
| 22 | + MetricReader, |
| 23 | + DataPoint, |
| 24 | + ExponentialHistogram, |
| 25 | + Histogram, |
| 26 | +} from '../../src'; |
| 27 | +import { TestMetricReader } from '../export/TestMetricReader'; |
| 28 | + |
| 29 | +describe('histogram-recording-nan', () => { |
| 30 | + it('exponential histogram should not count NaN', async () => { |
| 31 | + const reader = new TestMetricReader({ |
| 32 | + aggregationTemporalitySelector() { |
| 33 | + return AggregationTemporality.CUMULATIVE; |
| 34 | + }, |
| 35 | + aggregationSelector(type) { |
| 36 | + return Aggregation.ExponentialHistogram(); |
| 37 | + }, |
| 38 | + }); |
| 39 | + const meterProvider = new MeterProvider({ |
| 40 | + readers: [reader], |
| 41 | + }); |
| 42 | + |
| 43 | + const meter = meterProvider.getMeter('my-meter'); |
| 44 | + const hist = meter.createHistogram('testhist'); |
| 45 | + |
| 46 | + hist.record(1); |
| 47 | + hist.record(2); |
| 48 | + hist.record(4); |
| 49 | + hist.record(NaN); |
| 50 | + |
| 51 | + const resourceMetrics1 = await collectNoErrors(reader); |
| 52 | + const dataPoint1 = resourceMetrics1.scopeMetrics[0].metrics[0] |
| 53 | + .dataPoints[0] as DataPoint<ExponentialHistogram>; |
| 54 | + |
| 55 | + assert.deepStrictEqual( |
| 56 | + dataPoint1.value.count, |
| 57 | + 3, |
| 58 | + 'Sum of bucket count values should match overall count' |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('explicit bucket histogram should not count NaN', async () => { |
| 63 | + const reader = new TestMetricReader({ |
| 64 | + aggregationTemporalitySelector() { |
| 65 | + return AggregationTemporality.CUMULATIVE; |
| 66 | + }, |
| 67 | + aggregationSelector(type) { |
| 68 | + return Aggregation.Histogram(); |
| 69 | + }, |
| 70 | + }); |
| 71 | + const meterProvider = new MeterProvider({ |
| 72 | + readers: [reader], |
| 73 | + }); |
| 74 | + |
| 75 | + const meter = meterProvider.getMeter('my-meter'); |
| 76 | + const hist = meter.createHistogram('testhist'); |
| 77 | + |
| 78 | + hist.record(1); |
| 79 | + hist.record(2); |
| 80 | + hist.record(4); |
| 81 | + hist.record(NaN); |
| 82 | + |
| 83 | + const resourceMetrics1 = await collectNoErrors(reader); |
| 84 | + const dataPoint1 = resourceMetrics1.scopeMetrics[0].metrics[0] |
| 85 | + .dataPoints[0] as DataPoint<Histogram>; |
| 86 | + |
| 87 | + assert.deepStrictEqual( |
| 88 | + dataPoint1.value.count, |
| 89 | + 3, |
| 90 | + 'Sum of bucket count values should match overall count' |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + const collectNoErrors = async (reader: MetricReader) => { |
| 95 | + const { resourceMetrics, errors } = await reader.collect(); |
| 96 | + assert.strictEqual(errors.length, 0); |
| 97 | + return resourceMetrics; |
| 98 | + }; |
| 99 | +}); |
0 commit comments