|
| 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 | +import { ExportResultCode } from '@opentelemetry/core'; |
| 17 | +import { Resource } from '@opentelemetry/resources'; |
| 18 | +import * as metrics from '@opentelemetry/api-metrics'; |
| 19 | +import assert = require('assert'); |
| 20 | +import { AggregationTemporality } from '../../src/export/AggregationTemporality'; |
| 21 | +import { InMemoryMetricExporter } from '../../src/export/InMemoryMetricExporter'; |
| 22 | +import { ResourceMetrics } from '../../src/export/MetricData'; |
| 23 | +import { PeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; |
| 24 | +import { MeterProvider } from '../../src/MeterProvider'; |
| 25 | +import { defaultResource } from '../util'; |
| 26 | + |
| 27 | +async function waitForNumberOfExports(exporter: InMemoryMetricExporter , numberOfExports: number): Promise<ResourceMetrics[]> { |
| 28 | + if (numberOfExports <= 0) { |
| 29 | + throw new Error('numberOfExports must be greater than or equal to 0'); |
| 30 | + } |
| 31 | + |
| 32 | + let totalExports = 0; |
| 33 | + while (totalExports < numberOfExports) { |
| 34 | + await new Promise(resolve => setTimeout(resolve, 20)); |
| 35 | + const exportedMetrics = exporter.getMetrics(); |
| 36 | + totalExports = exportedMetrics.length; |
| 37 | + } |
| 38 | + |
| 39 | + return exporter.getMetrics(); |
| 40 | +} |
| 41 | + |
| 42 | +describe('InMemoryMetricExporter', () => { |
| 43 | + let exporter: InMemoryMetricExporter; |
| 44 | + let meterProvider: MeterProvider; |
| 45 | + let meterReader: PeriodicExportingMetricReader; |
| 46 | + let meter: metrics.Meter; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + exporter = new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE); |
| 50 | + meterProvider = new MeterProvider({ resource: defaultResource }); |
| 51 | + meter = meterProvider.getMeter('InMemoryMetricExporter', '1.0.0'); |
| 52 | + meterReader = new PeriodicExportingMetricReader({ |
| 53 | + exporter: exporter, |
| 54 | + exportIntervalMillis: 100, |
| 55 | + exportTimeoutMillis: 100 |
| 56 | + }); |
| 57 | + meterProvider.addMetricReader(meterReader); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(async () => { |
| 61 | + await exporter.shutdown(); |
| 62 | + await meterReader.shutdown(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return failed result code', done => { |
| 66 | + exporter.shutdown().then(() => { |
| 67 | + const resource = new Resource({ |
| 68 | + 'resource-attribute': 'resource attribute value', |
| 69 | + }); |
| 70 | + const resourceMetrics: ResourceMetrics = { |
| 71 | + resource: resource, |
| 72 | + scopeMetrics: |
| 73 | + [ |
| 74 | + { |
| 75 | + scope: { |
| 76 | + name: 'mylib', |
| 77 | + version: '0.1.0', |
| 78 | + schemaUrl: 'http://url.to.schema' |
| 79 | + }, |
| 80 | + metrics: [], |
| 81 | + } |
| 82 | + ] |
| 83 | + }; |
| 84 | + exporter.export(resourceMetrics, result => { |
| 85 | + assert.ok(result.code === ExportResultCode.FAILED); |
| 86 | + meterReader.shutdown().then(() => { |
| 87 | + done(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should reset metrics when reset is called', async () => { |
| 94 | + const counter = meter.createCounter('counter_total', { |
| 95 | + description: 'a test description', |
| 96 | + }); |
| 97 | + const counterAttribute = { key1: 'attributeValue1' }; |
| 98 | + counter.add(10, counterAttribute); |
| 99 | + |
| 100 | + const exportedMetrics = await waitForNumberOfExports(exporter, 1); |
| 101 | + assert.ok(exportedMetrics.length > 0); |
| 102 | + |
| 103 | + exporter.reset(); |
| 104 | + |
| 105 | + const otherMetrics = exporter.getMetrics(); |
| 106 | + assert.ok(otherMetrics.length === 0); |
| 107 | + |
| 108 | + await exporter.shutdown(); |
| 109 | + await meterReader.shutdown(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should be able to access metric', async () => { |
| 113 | + const counter = meter.createCounter('counter_total', { |
| 114 | + description: 'a test description', |
| 115 | + }); |
| 116 | + const counterAttribute = { key1: 'attributeValue1' }; |
| 117 | + counter.add(10, counterAttribute); |
| 118 | + counter.add(10, counterAttribute); |
| 119 | + |
| 120 | + const histogram = meter.createHistogram('histogram', { description: 'a histogram' }); |
| 121 | + histogram.record(10); |
| 122 | + histogram.record(100); |
| 123 | + histogram.record(1000); |
| 124 | + |
| 125 | + const exportedMetrics = await waitForNumberOfExports(exporter, 1); |
| 126 | + assert.ok(exportedMetrics.length > 0); |
| 127 | + |
| 128 | + const resourceMetrics = exportedMetrics.shift(); |
| 129 | + assert.ok(resourceMetrics); |
| 130 | + const firstScopeMetric = resourceMetrics?.scopeMetrics.shift(); |
| 131 | + assert.ok(firstScopeMetric); |
| 132 | + assert.ok(firstScopeMetric.metrics.length > 0); |
| 133 | + const [counterMetric, histogramMetric] = firstScopeMetric.metrics; |
| 134 | + assert.ok(counterMetric.descriptor.name, 'counter_total'); |
| 135 | + assert.ok(counterMetric.dataPoints.length > 0); |
| 136 | + const counterDataPoint = counterMetric.dataPoints.shift(); |
| 137 | + assert.ok(counterDataPoint); |
| 138 | + assert.strictEqual(counterDataPoint.attributes, counterAttribute); |
| 139 | + |
| 140 | + assert.ok(histogramMetric.descriptor.name, 'histogram'); |
| 141 | + assert.ok(histogramMetric.dataPoints.length > 0); |
| 142 | + const histogramDataPoint = histogramMetric.dataPoints.shift(); |
| 143 | + assert.ok(histogramDataPoint); |
| 144 | + |
| 145 | + await meterReader.shutdown(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments