From fade3c989bd698948deefd0f67707f6d91aa7260 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 13 Sep 2022 18:25:51 +0200 Subject: [PATCH] ci(instrumentation-http): improve metrics test stability (#3242) * fix(instrumentation-http): improve metrics test stability * fix(changelog): add changelog entry. * fix(instrumentation-http): clean up test reader. * fix(changlog): move entry to internal * fix(instrumentation-http): fix TestMetricReader --- experimental/CHANGELOG.md | 2 + .../test/functionals/http-metrics.test.ts | 7 +-- .../test/utils/TestMetricReader.ts | 49 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 experimental/packages/opentelemetry-instrumentation-http/test/utils/TestMetricReader.ts diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index f3d709a045..0c77075056 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -54,6 +54,8 @@ All notable changes to experimental packages in this project will be documented ### :house: (Internal) +* ci(instrumentation-http): improve metrics test stability [#3242](https://github.com/open-telemetry/opentelemetry-js/pull/3242) @pichlermarc + ## 0.32.0 ### :boom: Breaking Change diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts index 5ab046b45f..3d80bb950d 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-metrics.test.ts @@ -18,13 +18,13 @@ import { DataPointType, InMemoryMetricExporter, MeterProvider, - PeriodicExportingMetricReader, } from '@opentelemetry/sdk-metrics'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import { HttpInstrumentation } from '../../src/http'; import { httpRequest } from '../utils/httpRequest'; +import { TestMetricReader } from '../utils/TestMetricReader'; const instrumentation = new HttpInstrumentation(); instrumentation.enable(); @@ -40,7 +40,8 @@ const pathname = '/test'; const tracerProvider = new NodeTracerProvider(); const meterProvider = new MeterProvider(); const metricsMemoryExporter = new InMemoryMetricExporter(AggregationTemporality.DELTA); -const metricReader = new PeriodicExportingMetricReader({ exporter: metricsMemoryExporter, exportIntervalMillis: 100 }); +const metricReader = new TestMetricReader(metricsMemoryExporter); + meterProvider.addMetricReader(metricReader); instrumentation.setTracerProvider(tracerProvider); instrumentation.setMeterProvider(meterProvider); @@ -69,7 +70,7 @@ describe('metrics', () => { for (let i = 0; i < requestCount; i++) { await httpRequest.get(`${protocol}://${hostname}:${serverPort}${pathname}`); } - await new Promise(resolve => setTimeout(resolve, 300)); + await metricReader.collectAndExport(); const resourceMetrics = metricsMemoryExporter.getMetrics(); const scopeMetrics = resourceMetrics[0].scopeMetrics; assert.strictEqual(scopeMetrics.length, 1, 'scopeMetrics count'); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/utils/TestMetricReader.ts b/experimental/packages/opentelemetry-instrumentation-http/test/utils/TestMetricReader.ts new file mode 100644 index 0000000000..53ea687d64 --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation-http/test/utils/TestMetricReader.ts @@ -0,0 +1,49 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + MetricReader, + PushMetricExporter +} from '@opentelemetry/sdk-metrics'; + +export class TestMetricReader extends MetricReader { + constructor(private _exporter: PushMetricExporter) { + super({ + aggregationTemporalitySelector: _exporter.selectAggregationTemporality?.bind(_exporter), + }); + } + + protected onForceFlush(): Promise { + return Promise.resolve(undefined); + } + + protected onShutdown(): Promise { + return Promise.resolve(undefined); + } + + public async collectAndExport(): Promise { + const result = await this.collect(); + await new Promise((resolve, reject) => { + this._exporter.export(result.resourceMetrics, result => { + if (result.error != null) { + reject(result.error); + } else { + resolve(); + } + }); + }); + } +}