|
| 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 * as sinon from 'sinon'; |
| 19 | +import { |
| 20 | + Aggregation, |
| 21 | + AggregationTemporality, |
| 22 | + InstrumentType, |
| 23 | + MeterProvider, |
| 24 | + MetricReader, |
| 25 | +} from '../../src'; |
| 26 | +import { TestMetricReader } from '../export/TestMetricReader'; |
| 27 | + |
| 28 | +describe('cumulative-exponential-histogram', () => { |
| 29 | + let clock: sinon.SinonFakeTimers; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + clock = sinon.useFakeTimers(); |
| 33 | + }); |
| 34 | + afterEach(() => { |
| 35 | + sinon.restore(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('Cumulative Histogram should have the same startTime every collection', async () => { |
| 39 | + // Works fine and passes |
| 40 | + await doTest(Aggregation.Histogram()); |
| 41 | + }); |
| 42 | + |
| 43 | + it('Cumulative ExponentialHistogram should have the same startTime every collection', async () => { |
| 44 | + // Fails |
| 45 | + await doTest(Aggregation.ExponentialHistogram()); |
| 46 | + }); |
| 47 | + |
| 48 | + const doTest = async (histogramAggregation: Aggregation) => { |
| 49 | + const meterProvider = new MeterProvider(); |
| 50 | + const reader = new TestMetricReader({ |
| 51 | + aggregationTemporalitySelector() { |
| 52 | + return AggregationTemporality.CUMULATIVE; |
| 53 | + }, |
| 54 | + aggregationSelector(type) { |
| 55 | + return type === InstrumentType.HISTOGRAM |
| 56 | + ? histogramAggregation |
| 57 | + : Aggregation.Default(); |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + meterProvider.addMetricReader(reader); |
| 62 | + const meter = meterProvider.getMeter('my-meter'); |
| 63 | + const hist = meter.createHistogram('testhist'); |
| 64 | + |
| 65 | + hist.record(1); |
| 66 | + |
| 67 | + const resourceMetrics1 = await collectNoErrors(reader); |
| 68 | + const dataPoint1 = |
| 69 | + resourceMetrics1.scopeMetrics[0].metrics[0].dataPoints[0]; |
| 70 | + |
| 71 | + clock.tick(1000); |
| 72 | + hist.record(2); |
| 73 | + |
| 74 | + const resourceMetrics2 = await collectNoErrors(reader); |
| 75 | + const dataPoint2 = |
| 76 | + resourceMetrics2.scopeMetrics[0].metrics[0].dataPoints[0]; |
| 77 | + |
| 78 | + assert.deepStrictEqual( |
| 79 | + dataPoint1.startTime, |
| 80 | + dataPoint2.startTime, |
| 81 | + 'The start time should be the same across cumulative collections' |
| 82 | + ); |
| 83 | + }; |
| 84 | + |
| 85 | + const collectNoErrors = async (reader: MetricReader) => { |
| 86 | + const { resourceMetrics, errors } = await reader.collect(); |
| 87 | + assert.strictEqual(errors.length, 0); |
| 88 | + return resourceMetrics; |
| 89 | + }; |
| 90 | +}); |
0 commit comments