Skip to content

Commit

Permalink
ci(instrumentation-http): improve metrics test stability (open-teleme…
Browse files Browse the repository at this point in the history
…try#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
  • Loading branch information
pichlermarc authored Sep 13, 2022
1 parent bd0a77f commit fade3c9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
return Promise.resolve(undefined);
}

protected onShutdown(): Promise<void> {
return Promise.resolve(undefined);
}

public async collectAndExport(): Promise<void> {
const result = await this.collect();
await new Promise<void>((resolve, reject) => {
this._exporter.export(result.resourceMetrics, result => {
if (result.error != null) {
reject(result.error);
} else {
resolve();
}
});
});
}
}

0 comments on commit fade3c9

Please sign in to comment.