From 53bea7e97da705a95dd10ff5595ebe1397cb3934 Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 26 Oct 2021 14:30:48 +0800 Subject: [PATCH] feat(sdk-metrics-base): remove per-meter config on `MeterProvider.getMeter` Makes it compliant with api definition. --- .../test/PrometheusExporter.test.ts | 25 +++++---------- .../src/MeterProvider.ts | 4 +-- .../test/Meter.test.ts | 23 -------------- .../test/MeterProvider.test.ts | 31 ++++++++++++++++++- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index 3b5f044be3c..d3dbd6c0389 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -216,10 +216,9 @@ describe('PrometheusExporter', () => { exporter = new PrometheusExporter({}, () => { meterProvider = new MeterProvider({ interval: Math.pow(2, 31) - 1, - }); - meter = meterProvider.getMeter('test-prometheus', '1', { exporter, }); + meter = meterProvider.getMeter('test-prometheus', '1'); done(); }); }); @@ -352,25 +351,15 @@ describe('PrometheusExporter', () => { counter.bind({ counterKey1: 'labelValue2' }).add(20); counter.bind({ counterKey1: 'labelValue3' }).add(30); meterProvider.shutdown().then(() => { + // exporter has been shut down along with meter provider. http .get('http://localhost:9464/metrics', res => { - res.on('data', chunk => { - const body = chunk.toString(); - const lines = body.split('\n'); - - assert.deepStrictEqual(lines, [ - '# HELP counter_total a test description', - '# TYPE counter_total counter', - `counter_total{counterKey1="labelValue1"} 10 ${mockedHrTimeMs}`, - `counter_total{counterKey1="labelValue2"} 20 ${mockedHrTimeMs}`, - `counter_total{counterKey1="labelValue3"} 30 ${mockedHrTimeMs}`, - '', - ]); - - done(); - }); + errorHandler(done)(new Error('unreachable')); }) - .on('error', errorHandler(done)); + .on('error', err => { + assert(`${err}`.match('ECONNREFUSED')); + done(); + }); }); }); diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts index c7edefd0d7f..cc4ecc82a1e 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts @@ -48,12 +48,12 @@ export class MeterProvider implements api.MeterProvider { * * @returns Meter A Meter with the given name and version */ - getMeter(name: string, version?: string, config?: MeterConfig): Meter { + getMeter(name: string, version?: string): Meter { const key = `${name}@${version || ''}`; if (!this._meters.has(key)) { this._meters.set( key, - new Meter({ name, version }, config || this._config) + new Meter({ name, version }, this._config) ); } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/Meter.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/Meter.test.ts index e89545f7adf..3dae4e62302 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/Meter.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/Meter.test.ts @@ -21,7 +21,6 @@ import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; import * as sinon from 'sinon'; import { - Aggregator, CounterMetric, Histogram, LastValue, @@ -29,7 +28,6 @@ import { Meter, MeterProvider, Metric, - MetricDescriptor, MetricKind, MetricRecord, Sum, @@ -40,7 +38,6 @@ import { import { BatchObserver } from '../src/BatchObserver'; import { BatchObserverResult } from '../src/BatchObserverResult'; import { SumAggregator } from '../src/export/aggregators'; -import { Processor } from '../src/export/Processor'; import { ObservableCounterMetric } from '../src/ObservableCounterMetric'; import { ObservableUpDownCounterMetric } from '../src/ObservableUpDownCounterMetric'; import { hashLabels } from '../src/Utils'; @@ -1364,28 +1361,8 @@ describe('Meter', () => { assert.strictEqual(value, 10); }); }); - - it('should allow custom processor', () => { - const customMeter = new MeterProvider().getMeter('custom-processor', '*', { - processor: new CustomProcessor(), - }); - assert.throws(() => { - const histogram = customMeter.createHistogram('myHistogram'); - histogram.bind({}).record(1); - }, /aggregatorFor method not implemented/); - }); }); -class CustomProcessor extends Processor { - process(record: MetricRecord): void { - throw new Error('process method not implemented.'); - } - - aggregatorFor(metricKind: MetricDescriptor): Aggregator { - throw new Error('aggregatorFor method not implemented.'); - } -} - function ensureMetric(metric: MetricRecord, name?: string, value?: LastValue) { assert.ok(metric.aggregator instanceof LastValueAggregator); const lastValue = metric.aggregator.toPoint().value; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts index b7bb7bd42f6..5411f63b8f5 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/MeterProvider.test.ts @@ -16,7 +16,15 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; -import { MeterProvider, Meter, CounterMetric } from '../src'; +import { + MeterProvider, + Meter, + CounterMetric, + MetricRecord, + MetricDescriptor, + Aggregator, + Processor, +} from '../src'; describe('MeterProvider', () => { afterEach(() => { @@ -74,6 +82,27 @@ describe('MeterProvider', () => { const meter4 = provider.getMeter('meter3', 'ver2'); assert.notEqual(meter3, meter4); }); + + it('should allow custom processor', () => { + class CustomProcessor extends Processor { + process(record: MetricRecord): void { + throw new Error('process method not implemented.'); + } + + aggregatorFor(metricKind: MetricDescriptor): Aggregator { + throw new Error('aggregatorFor method not implemented.'); + } + } + + const meter = new MeterProvider({ + processor: new CustomProcessor(), + }).getMeter('custom-processor', '*'); + + assert.throws(() => { + const histogram = meter.createHistogram('myHistogram'); + histogram.bind({}).record(1); + }, /aggregatorFor method not implemented/); + }); }); describe('shutdown()', () => {