diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index 064956eab05..dee3c789ab4 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -14,6 +14,7 @@ * refactor(resources)!: replace `ResourceAttributes` with `Attributes` [#5016](https://github.com/open-telemetry/opentelemetry-js/pull/5016) @david-luna * feat(sdk-metrics)!: drop `View` and `Aggregation` in favor of `ViewOptions` and `AggregationOption` [#4931](https://github.com/open-telemetry/opentelemetry-js/pull/4931) @pichlermarc * refactor(sdk-trace-base)!: remove `new Span` constructor in favor of `Tracer.startSpan` API [#5048](https://github.com/open-telemetry/opentelemetry-js/pull/5048) @david-luna +* feat(sdk-metrics)!: use factory functions over classes [#4932](https://github.com/open-telemetry/opentelemetry-js/pull/4932) @pichlermarc * refactor(sdk-trace-base)!: remove `BasicTracerProvider.addSpanProcessor` API in favor of constructor options. [#5134](https://github.com/open-telemetry/opentelemetry-js/pull/5134) @david-luna ### :rocket: (Enhancement) diff --git a/doc/metrics.md b/doc/metrics.md index 1eb873a11d6..ba4b4cf1632 100644 --- a/doc/metrics.md +++ b/doc/metrics.md @@ -397,17 +397,20 @@ Below an example is given how you can define explicit buckets for a histogram. ```typescript // Define view for the histogram metric -const histogramView = new View({ - aggregation: new ExplicitBucketHistogramAggregation([0, 1, 5, 10, 15, 20, 25, 30]), +const histogramView: ViewOptions = { + aggregation: { + type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, + boundaries: [0, 1, 5, 10, 15, 20, 25, 30] + }, instrumentName: 'http.server.duration', instrumentType: InstrumentType.HISTOGRAM, -}); +}; // Note, the instrumentName is the same as the name that has been passed for // the Meter#createHistogram function // Create an instance of the metric provider -const meterProvider = new MeterProvider({ +const meterProvider = createMeterProvider({ views: [ histogramView ] @@ -441,20 +444,20 @@ instruments with a specific name: The following view drops all instruments that are associated with a meter named `pubsub`: ```typescript -const dropView = new View({ - aggregation: new DropAggregation(), +const dropView: ViewOptions = { + aggregation: { type: AggregationType.DROP }, meterName: 'pubsub', -}); +}; ``` Alternatively, you can also drop instruments with a specific instrument name, for example, all instruments of which the name starts with `http`: ```typescript -const dropView = new View({ - aggregation: new DropAggregation(), +const dropView: ViewOptions = { + aggregation: { type: AggregationType.DROP }, instrumentName: 'http*', -}); +}; ``` ### Customizing the metric attributes of instrument @@ -467,12 +470,12 @@ In the example below will drop all attributes except attribute `environment` for all instruments. ```typescript -new View({ +const attributeLimitingView: ViewOptions = { // only export the attribute 'environment' - attributeKeys: ['environment'], + attributeProcesssors: [createAllowListAttributesProcessor(['environment'])], // apply the view to all instruments instrumentName: '*', -}) +}; ``` ## Exporting measurements @@ -497,22 +500,24 @@ to use the Prometheus exporter `PrometheusExporter` which is included in the ```typescript const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus'); -const { MeterProvider } = require('@opentelemetry/sdk-metrics'); +const { createMetricReader } = require('@opentelemetry/sdk-metrics'); // Add your port to the Prometheus options -const options = { port: 9464 }; +const options = {port: 9464}; const exporter = new PrometheusExporter(options); // Creates MeterProvider and installs the exporter as a MetricReader -const meterProvider = new MeterProvider(); -meterProvider.addMetricReader(exporter); +const meterProvider = createMeterProvider({ + readers: [exporter] +}); + const meter = meterProvider.getMeter('example-prometheus'); // Now, start recording data const counter = meter.createCounter('metric_name', { description: 'Example of a counter' }); -counter.add(10, { pid: process.pid }); +counter.add(10, {pid: process.pid}); ``` In the above example the instantiated `PrometheusExporter` is configured to expose @@ -534,19 +539,16 @@ The example below shows how you can configure OpenTelemetry JavaScript to use the OTLP exporter using http/protobuf. ```typescript -const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); +const { createMeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); const collectorOptions = { url: '', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics concurrencyLimit: 1, // an optional limit on pending requests }; const exporter = new OTLPMetricExporter(collectorOptions); -const meterProvider = new MeterProvider({}); - -meterProvider.addMetricReader(new PeriodicExportingMetricReader({ - exporter: metricExporter, - exportIntervalMillis: 1000, -})); +const meterProvider = createMeterProvider({ + readers: [createPeriodicExportingMetricReader({ exporter: metricExporter, exportIntervalMillis: 1000 })], +}); // Now, start recording data const meter = meterProvider.getMeter('example-meter'); diff --git a/examples/opentelemetry-web/examples/metrics/index.js b/examples/opentelemetry-web/examples/metrics/index.js index c3f86cfbe50..77bce266472 100644 --- a/examples/opentelemetry-web/examples/metrics/index.js +++ b/examples/opentelemetry-web/examples/metrics/index.js @@ -1,6 +1,6 @@ const { DiagConsoleLogger, DiagLogLevel, diag, metrics } = require('@opentelemetry/api'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); -const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); +const { createMeterProvider, createPeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); // Optional and only needed to see the internal diagnostic logging (during development) diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); @@ -18,13 +18,14 @@ function stopMetrics() { function startMetrics() { console.log('STARTING METRICS'); - const meterProvider = new MeterProvider(); - metrics.setGlobalMeterProvider(meterProvider); + const meterProvider = createMeterProvider({ + readers: [createPeriodicExportingMetricReader({ + exporter: new OTLPMetricExporter(), + exportIntervalMillis: 1000 + })] + }); - meterProvider.addMetricReader(new PeriodicExportingMetricReader({ - exporter: new OTLPMetricExporter(), - exportIntervalMillis: 1000 - })); + metrics.setGlobalMeterProvider(meterProvider); meter = meterProvider.getMeter('example-exporter-collector') diff --git a/examples/otlp-exporter-node/metrics.js b/examples/otlp-exporter-node/metrics.js index 22507ef528b..1a17704499f 100644 --- a/examples/otlp-exporter-node/metrics.js +++ b/examples/otlp-exporter-node/metrics.js @@ -4,12 +4,11 @@ const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); // const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc'); // const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); -// const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics'); +// const { createConsoleMetricExporter } = require('@opentelemetry/sdk-metrics'); const { - ExponentialHistogramAggregation, - MeterProvider, - PeriodicExportingMetricReader, - View, + createMeterProvider, + createPeriodicExportingMetricReader, + AggregationType, } = require('@opentelemetry/sdk-metrics'); const { Resource } = require('@opentelemetry/resources'); const { @@ -25,30 +24,26 @@ const metricExporter = new OTLPMetricExporter({ // }, }); -// Define view for the exponential histogram metric -const expHistogramView = new View({ - aggregation: new ExponentialHistogramAggregation(), - // Note, the instrumentName is the same as the name that has been passed for - // the Meter#createHistogram function for exponentialHistogram. - instrumentName: 'test_exponential_histogram', -}); // Create an instance of the metric provider -const meterProvider = new MeterProvider({ +const meterProvider = createMeterProvider({ + readers: [createPeriodicExportingMetricReader({ + exporter: metricExporter, + // exporter: createConsoleMetricExporter(), + exportIntervalMillis: 1000, + })], resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'basic-metric-service', }), - views: [expHistogramView], + // Define view for the exponential histogram metric + views: [{ + aggregation: { type: AggregationType.EXPONENTIAL_HISTOGRAM }, + // Note, the instrumentName is the same as the name that has been passed for + // the Meter#createHistogram function for exponentialHistogram. + instrumentName: 'test_exponential_histogram', + }], }); -meterProvider.addMetricReader( - new PeriodicExportingMetricReader({ - exporter: metricExporter, - // exporter: new ConsoleMetricExporter(), - exportIntervalMillis: 1000, - }) -); - const meter = meterProvider.getMeter('example-exporter-collector'); const requestCounter = meter.createCounter('requests', { diff --git a/experimental/examples/opencensus-shim/setup.js b/experimental/examples/opencensus-shim/setup.js index e3e189eae2d..25ec1b8aed1 100644 --- a/experimental/examples/opencensus-shim/setup.js +++ b/experimental/examples/opencensus-shim/setup.js @@ -20,7 +20,7 @@ const { NodeTracerProvider, BatchSpanProcessor, } = require('@opentelemetry/sdk-trace-node'); -const { MeterProvider } = require('@opentelemetry/sdk-metrics'); +const { createMeterProvider } = require('@opentelemetry/sdk-metrics'); const { OTLPTraceExporter, } = require('@opentelemetry/exporter-trace-otlp-grpc'); @@ -58,17 +58,20 @@ module.exports = function setup(serviceName) { }); tracerProvider.register(); - const meterProvider = new MeterProvider({ resource }); - meterProvider.addMetricReader( - new PrometheusExporter({ - metricProducers: [ - new OpenCensusMetricProducer({ - openCensusMetricProducerManager: - oc.Metrics.getMetricProducerManager(), - }), - ], - }) - ); + const meterProvider = createMeterProvider({ + readers: [ + new PrometheusExporter({ + metricProducers: [ + new OpenCensusMetricProducer({ + openCensusMetricProducerManager: + oc.Metrics.getMetricProducerManager(), + }), + ], + }) + ], + resource + }); + metrics.setGlobalMeterProvider(meterProvider); // Start OpenCensus tracing diff --git a/experimental/examples/prometheus/index.js b/experimental/examples/prometheus/index.js index 8f8dd96836d..e1557737d8d 100644 --- a/experimental/examples/prometheus/index.js +++ b/experimental/examples/prometheus/index.js @@ -1,7 +1,7 @@ 'use strict'; const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api'); -const { MeterProvider } = require('@opentelemetry/sdk-metrics'); +const { MeterProvider, createMeterProvider} = require('@opentelemetry/sdk-metrics'); const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus'); // Optional and only needed to see the internal diagnostic logging (during development) @@ -16,8 +16,9 @@ const exporter = new PrometheusExporter({}, () => { }); // Creates MeterProvider and installs the exporter as a MetricReader -const meterProvider = new MeterProvider(); -meterProvider.addMetricReader(exporter); +const meterProvider = createMeterProvider({ + readers: [exporter] +}); const meter = meterProvider.getMeter('example-prometheus'); // Creates metric instruments diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md index 5c87ab2527a..2f7f9af7c49 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md @@ -27,7 +27,7 @@ The OTLPMetricsExporter in Node expects the URL to only be the hostname. It will options that work with trace also work with metrics. ```js -const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); +const { createPeriodicExportingMetricReader, createMeterProvider } = require('@opentelemetry/sdk-metrics'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc'); const collectorOptions = { // url is optional and can be omitted - default is http://localhost:4317 @@ -36,12 +36,14 @@ const collectorOptions = { }; const metricExporter = new OTLPMetricExporter(collectorOptions); -const meterProvider = new MeterProvider({}); - -meterProvider.addMetricReader(new PeriodicExportingMetricReader({ - exporter: metricExporter, - exportIntervalMillis: 1000, -})); +const meterProvider = createMeterProvider({ + readers: [ + createPeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, + }) + ] +}); ['SIGINT', 'SIGTERM'].forEach(signal => { process.on(signal, () => meterProvider.shutdown().catch(console.error)); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts index 352169212a7..1c6127388b2 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts @@ -28,7 +28,7 @@ import * as grpc from '@grpc/grpc-js'; import { VERSION } from '@opentelemetry/core'; import { AggregationType, - MeterProvider, + createMeterProvider, MetricReader, } from '@opentelemetry/sdk-metrics'; import { @@ -55,7 +55,7 @@ const testResource = new Resource({ }); let reader = new TestMetricReader(); -let meterProvider = new MeterProvider({ +let meterProvider = createMeterProvider({ resource: testResource, readers: [reader], }); @@ -68,7 +68,7 @@ export async function collect() { export function setUp() { reader = new TestMetricReader(); - meterProvider = new MeterProvider({ + meterProvider = createMeterProvider({ resource: testResource, views: [ { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md index 6458aaab672..c2404e828ca 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md @@ -26,7 +26,7 @@ the [Collector Trace Exporter for web and node][trace-exporter-url]. The OTLPMetricExporter in Web expects the endpoint to end in `/v1/metrics`. ```js -import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'; +import { createMeterProvider, createPeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'; import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'; const collectorOptions = { @@ -35,12 +35,14 @@ const collectorOptions = { concurrencyLimit: 1, // an optional limit on pending requests }; const metricExporter = new OTLPMetricExporter(collectorOptions); -const meterProvider = new MeterProvider({}); - -meterProvider.addMetricReader(new PeriodicExportingMetricReader({ - exporter: metricExporter, - exportIntervalMillis: 1000, -})); +const meterProvider = createMeterProvider({ + readers: [ + createPeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, + }) + ] +}); // Now, start recording data const meter = meterProvider.getMeter('example-meter'); @@ -51,19 +53,21 @@ counter.add(10, { 'key': 'value' }); ## Metrics in Node ```js -const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); +const { createMeterProvider, createPeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); const collectorOptions = { url: '', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics concurrencyLimit: 1, // an optional limit on pending requests }; const metricExporter = new OTLPMetricExporter(collectorOptions); -const meterProvider = new MeterProvider({}); - -meterProvider.addMetricReader(new PeriodicExportingMetricReader({ - exporter: metricExporter, - exportIntervalMillis: 1000, -})); +const meterProvider = createMeterProvider({ + readers: [ + createPeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, + }) + ] +}); // Now, start recording data const meter = meterProvider.getMeter('example-meter'); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index 62f749f1afe..b8e02f21a7b 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -29,7 +29,7 @@ import * as assert from 'assert'; import { InstrumentationScope, VERSION } from '@opentelemetry/core'; import { AggregationType, - MeterProvider, + createMeterProvider, MetricReader, ViewOptions, } from '@opentelemetry/sdk-metrics'; @@ -78,7 +78,7 @@ const defaultResource = Resource.default().merge( ); let reader = new TestMetricReader(); -let meterProvider = new MeterProvider({ +let meterProvider = createMeterProvider({ resource: defaultResource, readers: [reader], }); @@ -90,7 +90,7 @@ export async function collect() { export function setUp(views?: ViewOptions[]) { reader = new TestMetricReader(); - meterProvider = new MeterProvider({ + meterProvider = createMeterProvider({ resource: defaultResource, views, readers: [reader], diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md index e1b3c9941a5..de948a46b1c 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md @@ -22,18 +22,20 @@ To see sample code and documentation for the traces exporter, visit the [Collect ## Metrics in Node - PROTO over http ```js -const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); +const { createPeriodicExportingMetricReader, createMeterProvider } = require('@opentelemetry/sdk-metrics'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); const collectorOptions = { url: '', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics }; const metricExporter = new OTLPMetricExporter(collectorOptions); -const meterProvider = new MeterProvider({}); - -meterProvider.addMetricReader(new PeriodicExportingMetricReader({ - exporter: metricExporter, - exportIntervalMillis: 1000, -})); +const meterProvider = createMeterProvider({ + readers: [ + createPeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, + }) + ] +}); // Now, start recording data const meter = meterProvider.getMeter('example-meter'); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index 756d470bc97..e6f03774700 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -26,7 +26,7 @@ import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; import { AggregationType, - MeterProvider, + createMeterProvider, MetricReader, } from '@opentelemetry/sdk-metrics'; import { @@ -54,7 +54,7 @@ const testResource = new Resource({ }); let reader = new TestMetricReader(); -let meterProvider = new MeterProvider({ +let meterProvider = createMeterProvider({ resource: testResource, readers: [reader], }); @@ -67,7 +67,7 @@ export async function collect() { export function setUp() { reader = new TestMetricReader(); - meterProvider = new MeterProvider({ + meterProvider = createMeterProvider({ resource: testResource, views: [ { diff --git a/experimental/packages/opentelemetry-exporter-prometheus/README.md b/experimental/packages/opentelemetry-exporter-prometheus/README.md index 83c6ac14d5b..aa004f728af 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/README.md +++ b/experimental/packages/opentelemetry-exporter-prometheus/README.md @@ -22,15 +22,17 @@ Create & register the exporter on your application. ```js const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus'); -const { MeterProvider } = require('@opentelemetry/sdk-metrics'); +const { createMeterProvider } = require('@opentelemetry/sdk-metrics'); // Add your port and startServer to the Prometheus options const options = {port: 9464}; const exporter = new PrometheusExporter(options); // Creates MeterProvider and installs the exporter as a MetricReader -const meterProvider = new MeterProvider(); -meterProvider.addMetricReader(exporter); +const meterProvider = createMeterProvider({ + readers: [exporter] +}); + const meter = meterProvider.getMeter('example-prometheus'); // Now, start recording data diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index 66bab19ef8f..b2c28c58af9 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -14,8 +14,13 @@ * limitations under the License. */ -import { Counter, Meter, ObservableResult } from '@opentelemetry/api'; -import { MeterProvider } from '@opentelemetry/sdk-metrics'; +import { + Counter, + Meter, + MeterProvider, + ObservableResult, +} from '@opentelemetry/api'; +import { createMeterProvider } from '@opentelemetry/sdk-metrics'; import * as assert from 'assert'; import * as sinon from 'sinon'; import * as http from 'http'; @@ -200,12 +205,12 @@ describe('PrometheusExporter', () => { describe('export', () => { let exporter: PrometheusExporter; - let meterProvider: MeterProvider; + let meterProvider: ReturnType; let meter: Meter; beforeEach(async () => { exporter = new PrometheusExporter(); - meterProvider = new MeterProvider({ + meterProvider = createMeterProvider({ readers: [exporter], }); meter = meterProvider.getMeter('test-prometheus', '1'); @@ -471,7 +476,7 @@ describe('PrometheusExporter', () => { let exporter: PrometheusExporter; function setup(reader: PrometheusExporter) { - meterProvider = new MeterProvider({ + meterProvider = createMeterProvider({ readers: [exporter], }); diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index d671a9d7f6d..4d121fb823e 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -18,10 +18,11 @@ import * as assert from 'assert'; import { Attributes, UpDownCounter } from '@opentelemetry/api'; import { AggregationTemporality, + AggregationType, + createMeterProvider, DataPoint, DataPointType, Histogram, - MeterProvider, MetricReader, } from '@opentelemetry/sdk-metrics'; import * as sinon from 'sinon'; @@ -35,7 +36,6 @@ import { serviceName, } from './util'; import { Resource } from '@opentelemetry/resources'; -import { AggregationType } from '@opentelemetry/sdk-metrics'; const attributes = { foo1: 'bar1', @@ -82,7 +82,7 @@ describe('PrometheusSerializer', () => { describe('Singular', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { type: AggregationType.SUM }, @@ -132,7 +132,7 @@ describe('PrometheusSerializer', () => { describe('Histogram', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { @@ -202,7 +202,7 @@ describe('PrometheusSerializer', () => { describe('monotonic Sum', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { type: AggregationType.SUM }, @@ -257,7 +257,7 @@ describe('PrometheusSerializer', () => { describe('non-monotonic Sum', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { type: AggregationType.SUM }, @@ -311,7 +311,7 @@ describe('PrometheusSerializer', () => { describe('Gauge', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { @@ -367,7 +367,7 @@ describe('PrometheusSerializer', () => { describe('with ExplicitBucketHistogramAggregation', () => { async function testSerializer(serializer: PrometheusSerializer) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { @@ -425,7 +425,7 @@ describe('PrometheusSerializer', () => { it('serialize cumulative metric record on instrument that allows negative values', async () => { const serializer = new PrometheusSerializer(); const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { @@ -481,7 +481,7 @@ describe('PrometheusSerializer', () => { options: Partial<{ unit: string; exportAll: boolean }> = {} ) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { type: AggregationType.SUM }, @@ -573,7 +573,7 @@ describe('PrometheusSerializer', () => { fn: (counter: UpDownCounter) => void ) { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ views: [ { aggregation: { type: AggregationType.SUM }, 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 907a67b10b0..098a8d20b6d 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 @@ -15,9 +15,9 @@ */ import { AggregationTemporality, + createInMemoryMetricExporter, + createMeterProvider, DataPointType, - InMemoryMetricExporter, - MeterProvider, } from '@opentelemetry/sdk-metrics'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { @@ -58,11 +58,11 @@ const protocol = 'http'; const hostname = 'localhost'; const pathname = '/test'; const tracerProvider = new NodeTracerProvider(); -const metricsMemoryExporter = new InMemoryMetricExporter( - AggregationTemporality.DELTA -); +const metricsMemoryExporter = createInMemoryMetricExporter({ + aggregationTemporality: AggregationTemporality.DELTA, +}); const metricReader = new TestMetricReader(metricsMemoryExporter); -const meterProvider = new MeterProvider({ readers: [metricReader] }); +const meterProvider = createMeterProvider({ readers: [metricReader] }); instrumentation.setTracerProvider(tracerProvider); instrumentation.setMeterProvider(meterProvider); diff --git a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts index 363e196fb88..96b0473be9b 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts @@ -23,8 +23,9 @@ import { SpanCustomizationHook, } from '../../src'; -import { MeterProvider } from '@opentelemetry/sdk-metrics'; +import { createMeterProvider } from '@opentelemetry/sdk-metrics'; import { LoggerProvider } from '@opentelemetry/sdk-logs'; +import { MeterProvider } from '@opentelemetry/api'; interface TestInstrumentationConfig extends InstrumentationConfig { isActive?: boolean; @@ -84,7 +85,7 @@ describe('BaseInstrumentation', () => { describe('setMeterProvider', () => { let otelTestingMeterProvider: MeterProvider; beforeEach(() => { - otelTestingMeterProvider = new MeterProvider(); + otelTestingMeterProvider = createMeterProvider(); }); it('should call _updateMetricInstruments', () => { let called = true; diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index ba4c8b48568..24ad9b3c496 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -37,6 +37,11 @@ import { Resource, ResourceDetectionConfig, } from '@opentelemetry/resources'; +import { + createMeterProvider, + MetricReader, + ViewOptions, +} from '@opentelemetry/sdk-metrics'; import { LogRecordProcessor, LoggerProvider, @@ -48,11 +53,6 @@ import { import { OTLPLogExporter as OTLPHttpLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; import { OTLPLogExporter as OTLPGrpcLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc'; import { OTLPLogExporter as OTLPProtoLogExporter } from '@opentelemetry/exporter-logs-otlp-proto'; -import { - MeterProvider, - MetricReader, - ViewOptions, -} from '@opentelemetry/sdk-metrics'; import { BatchSpanProcessor, SpanProcessor, @@ -109,7 +109,7 @@ export class NodeSDK { private _tracerProvider?: NodeTracerProvider; private _loggerProvider?: LoggerProvider; - private _meterProvider?: MeterProvider; + private _meterProvider?: ReturnType; private _serviceName?: string; private _configuration?: Partial; @@ -299,7 +299,7 @@ export class NodeSDK { if (this._meterProviderConfig.reader) { readers.push(this._meterProviderConfig.reader); } - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: this._resource, views: this._meterProviderConfig?.views ?? [], readers: readers, diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index efda2a509fb..b52c2a5bdb5 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -23,6 +23,7 @@ import { DiagLogLevel, metrics, DiagConsoleLogger, + MeterProvider, } from '@opentelemetry/api'; import { AsyncHooksContextManager, @@ -32,11 +33,10 @@ import { CompositePropagator } from '@opentelemetry/core'; import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; import { AggregationTemporality, - ConsoleMetricExporter, - InMemoryMetricExporter, + createConsoleMetricExporter, InstrumentType, - MeterProvider, - PeriodicExportingMetricReader, + createPeriodicExportingMetricReader, + createInMemoryMetricExporter, } from '@opentelemetry/sdk-metrics'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { @@ -88,6 +88,10 @@ const DefaultContextManager = semver.gte(process.version, '14.8.0') ? AsyncLocalStorageContextManager : AsyncHooksContextManager; +function isSdkMeterProvider(obj: any) { + return typeof obj.shutdown === 'function'; +} + describe('Node SDK', () => { let ctxManager: any; let propagator: any; @@ -141,7 +145,7 @@ describe('Node SDK', () => { delegate, 'tracer provider should not have changed' ); - assert.ok(!(metrics.getMeterProvider() instanceof MeterProvider)); + assert.ok(!isSdkMeterProvider(metrics.getMeterProvider())); assert.strictEqual( (logs.getLoggerProvider() as ProxyLoggerProvider).getDelegate(), logsDelegate, @@ -193,7 +197,7 @@ describe('Node SDK', () => { sdk.start(); - assert.ok(!(metrics.getMeterProvider() instanceof MeterProvider)); + assert.ok(!isSdkMeterProvider(metrics.getMeterProvider())); assert.ok( context['_getContextManager']().constructor.name === @@ -216,7 +220,7 @@ describe('Node SDK', () => { sdk.start(); - assert.ok(!(metrics.getMeterProvider() instanceof MeterProvider)); + assert.ok(!isSdkMeterProvider(metrics.getMeterProvider())); assert.ok( context['_getContextManager']().constructor.name === @@ -246,7 +250,7 @@ describe('Node SDK', () => { sdk.start(); - assert.ok(!(metrics.getMeterProvider() instanceof MeterProvider)); + assert.ok(!isSdkMeterProvider(metrics.getMeterProvider())); assert.ok( context['_getContextManager']().constructor.name === @@ -274,8 +278,8 @@ describe('Node SDK', () => { // need to set OTEL_TRACES_EXPORTER to none since default value is otlp // which sets up an exporter and affects the context manager env.OTEL_TRACES_EXPORTER = 'none'; - const exporter = new ConsoleMetricExporter(); - const metricReader = new PeriodicExportingMetricReader({ + const exporter = createConsoleMetricExporter(); + const metricReader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 100, exportTimeoutMillis: 100, @@ -304,7 +308,7 @@ describe('Node SDK', () => { 'tracer provider should not have changed' ); - assert.ok(metrics.getMeterProvider() instanceof MeterProvider); + assert.ok(isSdkMeterProvider(metrics.getMeterProvider())); await sdk.shutdown(); delete env.OTEL_TRACES_EXPORTER; @@ -403,7 +407,7 @@ describe('Node SDK', () => { }); async function waitForNumberOfMetrics( - exporter: InMemoryMetricExporter, + exporter: ReturnType, numberOfMetrics: number ): Promise { if (numberOfMetrics <= 0) { @@ -422,10 +426,10 @@ describe('Node SDK', () => { // need to set OTEL_TRACES_EXPORTER to none since default value is otlp // which sets up an exporter and affects the context manager env.OTEL_TRACES_EXPORTER = 'none'; - const exporter = new InMemoryMetricExporter( - AggregationTemporality.CUMULATIVE - ); - const metricReader = new PeriodicExportingMetricReader({ + const exporter = createInMemoryMetricExporter({ + aggregationTemporality: AggregationTemporality.CUMULATIVE, + }); + const metricReader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 100, exportTimeoutMillis: 100, @@ -877,8 +881,8 @@ describe('Node SDK', () => { }); it('should not register a meter provider if a reader is provided', async () => { - const exporter = new ConsoleMetricExporter(); - const metricReader = new PeriodicExportingMetricReader({ + const exporter = createConsoleMetricExporter(); + const metricReader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 100, exportTimeoutMillis: 100, @@ -890,7 +894,7 @@ describe('Node SDK', () => { }); sdk.start(); - assert.ok(!(metrics.getMeterProvider() instanceof MeterProvider)); + assert.ok(!isSdkMeterProvider(metrics.getMeterProvider())); await sdk.shutdown(); }); diff --git a/experimental/packages/shim-opencensus/src/OpenCensusMetricProducer.ts b/experimental/packages/shim-opencensus/src/OpenCensusMetricProducer.ts index 04d4a067384..c2b58c620c6 100644 --- a/experimental/packages/shim-opencensus/src/OpenCensusMetricProducer.ts +++ b/experimental/packages/shim-opencensus/src/OpenCensusMetricProducer.ts @@ -46,8 +46,8 @@ interface OpenCensusMetricProducerOptions { * * @example * ``` - * const meterProvider = new MeterProvider(); - * const reader = new PeriodicExportingMetricReader({ + * const meterProvider = createMeterProvider(); + * const reader = createPeriodicExportingMetricReader({ * metricProducers: [new OpenCensusMetricProducer()], * exporter: exporter, * }); diff --git a/packages/sdk-metrics/README.md b/packages/sdk-metrics/README.md index af8f7eb0d56..8a3fa00a852 100644 --- a/packages/sdk-metrics/README.md +++ b/packages/sdk-metrics/README.md @@ -22,12 +22,12 @@ The basic setup of the SDK can be seen as followings: ```js const opentelemetry = require('@opentelemetry/api'); -const { MeterProvider } = require('@opentelemetry/sdk-metrics'); +const { createMeterProvider } = require('@opentelemetry/sdk-metrics'); // To create an instrument, you first need to initialize the Meter provider. // NOTE: The default OpenTelemetry meter provider does not record any metric instruments. // Registering a working meter provider allows the API methods to record instruments. -opentelemetry.metrics.setGlobalMeterProvider(new MeterProvider()); +opentelemetry.metrics.setGlobalMeterProvider(createMeterProvider()); // To record a metric event, we used the global singleton meter to create an instrument. const counter = opentelemetry.metrics.getMeter('default').createCounter('foo'); @@ -61,12 +61,12 @@ async function batchObservableCallback(batchObservableResult) { Views can be registered when instantiating a `MeterProvider`: ```js -const meterProvider = new MeterProvider({ +const meterProvider = new createMeterProvider({ views: [ // override the bucket boundaries on `my.histogram` to [0, 50, 100] - new View({ aggregation: new ExplicitBucketHistogramAggregation([0, 50, 100]), instrumentName: 'my.histogram'}), + { aggregation: { type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM, boundaries: [0, 50, 100] }, instrumentName: 'my.histogram'}, // rename 'my.counter' to 'my.renamed.counter' - new View({ name: 'my.renamed.counter', instrumentName: 'my.counter'}) + { name: 'my.renamed.counter', instrumentName: 'my.counter'}, ] }) ``` diff --git a/packages/sdk-metrics/src/MeterProvider.ts b/packages/sdk-metrics/src/MeterProvider.ts index e852519f698..6948a99d50d 100644 --- a/packages/sdk-metrics/src/MeterProvider.ts +++ b/packages/sdk-metrics/src/MeterProvider.ts @@ -16,7 +16,7 @@ import { diag, - MeterProvider as IMeterProvider, + MeterProvider, Meter as IMeter, MeterOptions, createNoopMeter, @@ -62,7 +62,7 @@ function prepareResource( /** * This class implements the {@link MeterProvider} interface. */ -export class MeterProvider implements IMeterProvider { +class MeterProviderImpl implements MeterProvider { private _sharedState: MeterProviderSharedState; private _shutdown = false; @@ -145,3 +145,12 @@ export class MeterProvider implements IMeterProvider { ); } } + +export function createMeterProvider( + options?: MeterProviderOptions +): MeterProvider & { + shutdown(options?: ShutdownOptions): Promise; + forceFlush(options?: ForceFlushOptions): Promise; +} { + return new MeterProviderImpl(options); +} diff --git a/packages/sdk-metrics/src/export/ConsoleMetricExporter.ts b/packages/sdk-metrics/src/export/ConsoleMetricExporter.ts index 56254836b30..adb157dbbd0 100644 --- a/packages/sdk-metrics/src/export/ConsoleMetricExporter.ts +++ b/packages/sdk-metrics/src/export/ConsoleMetricExporter.ts @@ -35,7 +35,7 @@ interface ConsoleMetricExporterOptions { */ /* eslint-disable no-console */ -export class ConsoleMetricExporter implements PushMetricExporter { +class ConsoleMetricExporter implements PushMetricExporter { protected _shutdown = false; protected _temporalitySelector: AggregationTemporalitySelector; @@ -92,3 +92,9 @@ export class ConsoleMetricExporter implements PushMetricExporter { done({ code: ExportResultCode.SUCCESS }); } } + +export function createConsoleMetricExporter( + options?: ConsoleMetricExporterOptions +): PushMetricExporter { + return new ConsoleMetricExporter(options); +} diff --git a/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts b/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts index 2a80973bb95..6bfda24ce55 100644 --- a/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts +++ b/packages/sdk-metrics/src/export/InMemoryMetricExporter.ts @@ -26,7 +26,7 @@ import { PushMetricExporter } from './MetricExporter'; * which accumulates metrics data in the local memory and * allows to inspect it (useful for e.g. unit tests). */ -export class InMemoryMetricExporter implements PushMetricExporter { +class InMemoryMetricExporter implements PushMetricExporter { protected _shutdown = false; protected _aggregationTemporality: AggregationTemporality; private _metrics: ResourceMetrics[] = []; @@ -79,3 +79,17 @@ export class InMemoryMetricExporter implements PushMetricExporter { return Promise.resolve(); } } + +/** + * Creates an In-memory Metrics Exporter. A Push Metric Exporter + * which accumulates metrics data in the local memory and + * allows to inspect it (useful for e.g. unit tests). + */ +export function createInMemoryMetricExporter(options: { + aggregationTemporality: AggregationTemporality; +}): PushMetricExporter & { + reset(): void; + getMetrics(): ResourceMetrics[]; +} { + return new InMemoryMetricExporter(options.aggregationTemporality); +} diff --git a/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts index 646c832aa41..6ef0bf1f0de 100644 --- a/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/packages/sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -53,7 +53,7 @@ export type PeriodicExportingMetricReaderOptions = { * {@link MetricReader} which collects metrics based on a user-configurable time interval, and passes the metrics to * the configured {@link PushMetricExporter} */ -export class PeriodicExportingMetricReader extends MetricReader { +class PeriodicExportingMetricReader extends MetricReader { private _interval?: ReturnType; private _exporter: PushMetricExporter; private readonly _exportInterval: number; @@ -165,3 +165,13 @@ export class PeriodicExportingMetricReader extends MetricReader { await this._exporter.shutdown(); } } + +/** + * Creates a {@link MetricReader} which collects metrics based on a user-configurable time interval, and passes the + * metrics to the configured {@link PushMetricExporter} + */ +export function createPeriodicExportingMetricReader( + options: PeriodicExportingMetricReaderOptions +): MetricReader { + return new PeriodicExportingMetricReader(options); +} diff --git a/packages/sdk-metrics/src/index.ts b/packages/sdk-metrics/src/index.ts index 4f359214bc8..e3cbd752d58 100644 --- a/packages/sdk-metrics/src/index.ts +++ b/packages/sdk-metrics/src/index.ts @@ -49,13 +49,13 @@ export { PushMetricExporter } from './export/MetricExporter'; export { MetricReader, MetricReaderOptions } from './export/MetricReader'; export { - PeriodicExportingMetricReader, + createPeriodicExportingMetricReader, PeriodicExportingMetricReaderOptions, } from './export/PeriodicExportingMetricReader'; -export { InMemoryMetricExporter } from './export/InMemoryMetricExporter'; +export { createInMemoryMetricExporter } from './export/InMemoryMetricExporter'; -export { ConsoleMetricExporter } from './export/ConsoleMetricExporter'; +export { createConsoleMetricExporter } from './export/ConsoleMetricExporter'; export { MetricCollectOptions, MetricProducer } from './export/MetricProducer'; @@ -65,7 +65,7 @@ export { InstrumentType } from './InstrumentDescriptor'; */ export type InstrumentDescriptor = MetricDescriptor; -export { MeterProvider, MeterProviderOptions } from './MeterProvider'; +export { createMeterProvider, MeterProviderOptions } from './MeterProvider'; export { AggregationOption, AggregationType } from './view/AggregationOption'; diff --git a/packages/sdk-metrics/src/view/View.ts b/packages/sdk-metrics/src/view/View.ts index 00227e918ba..8ee0c30ab80 100644 --- a/packages/sdk-metrics/src/view/View.ts +++ b/packages/sdk-metrics/src/view/View.ts @@ -158,7 +158,7 @@ function validateViewOptions(viewOptions: ViewOptions) { } /** - * Can be passed to a {@link MeterProvider} to select instruments and alter their metric stream. + * Internal class to select instruments and alter their metric stream. */ export class View { readonly name?: string; diff --git a/packages/sdk-metrics/test/Instruments.test.ts b/packages/sdk-metrics/test/Instruments.test.ts index a3348a2f889..3a21911f230 100644 --- a/packages/sdk-metrics/test/Instruments.test.ts +++ b/packages/sdk-metrics/test/Instruments.test.ts @@ -23,7 +23,7 @@ import { DataPointType, Histogram, InstrumentType, - MeterProvider, + createMeterProvider, MetricDescriptor, MetricReader, } from '../src'; @@ -408,7 +408,7 @@ describe('Instruments', () => { }); it('should allow metric advice with empty explicit boundaries', function () { - const meter = new MeterProvider({ + const meter = createMeterProvider({ readers: [new TestMetricReader()], }).getMeter('meter'); @@ -804,7 +804,7 @@ describe('Instruments', () => { function setup() { const deltaReader = new TestDeltaMetricReader(); const cumulativeReader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, readers: [deltaReader, cumulativeReader], }); diff --git a/packages/sdk-metrics/test/MeterProvider.test.ts b/packages/sdk-metrics/test/MeterProvider.test.ts index d7ab799fa96..4790026b218 100644 --- a/packages/sdk-metrics/test/MeterProvider.test.ts +++ b/packages/sdk-metrics/test/MeterProvider.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import { - MeterProvider, + createMeterProvider, InstrumentType, DataPointType, HistogramMetricData, @@ -41,13 +41,11 @@ describe('MeterProvider', () => { describe('constructor', () => { it('should construct without exceptions', () => { - const meterProvider = new MeterProvider(); - assert(meterProvider instanceof MeterProvider); + createMeterProvider(); }); it('construct with resource', () => { - const meterProvider = new MeterProvider({ resource: defaultResource }); - assert(meterProvider instanceof MeterProvider); + createMeterProvider({ resource: defaultResource }); }); it('should use default resource when no resource is passed', async function () { @@ -113,20 +111,20 @@ describe('MeterProvider', () => { describe('getMeter', () => { it('should get a meter', () => { - const meterProvider = new MeterProvider(); + const meterProvider = createMeterProvider(); const meter = meterProvider.getMeter('meter1', '1.0.0'); assert(meter instanceof Meter); }); it('should get an identical meter on duplicated calls', () => { - const meterProvider = new MeterProvider(); + const meterProvider = createMeterProvider(); const meter1 = meterProvider.getMeter('meter1', '1.0.0'); const meter2 = meterProvider.getMeter('meter1', '1.0.0'); assert.strictEqual(meter1, meter2); }); it('get a noop meter on shutdown', () => { - const meterProvider = new MeterProvider(); + const meterProvider = createMeterProvider(); meterProvider.shutdown(); const meter = meterProvider.getMeter('meter1', '1.0.0'); // returned tracer should be no-op, not instance of Meter (from SDK) @@ -135,7 +133,7 @@ describe('MeterProvider', () => { it('get meter with same identity', async () => { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, readers: [reader], }); @@ -195,7 +193,7 @@ describe('MeterProvider', () => { describe('addView', () => { it('with existing instrument should rename', async () => { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, // Add view to rename 'non-renamed-instrument' to 'renamed-instrument' views: [ @@ -265,7 +263,7 @@ describe('MeterProvider', () => { const reader = new TestMetricReader(); // Add view to drop all attributes except 'attrib1' - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, views: [ { @@ -333,7 +331,7 @@ describe('MeterProvider', () => { const reader = new TestMetricReader(); // Add view that renames 'test-counter' to 'renamed-instrument' - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, views: [ { @@ -404,7 +402,7 @@ describe('MeterProvider', () => { it('with meter name should apply view to only the selected meter', async () => { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, views: [ // Add view that renames 'test-counter' to 'renamed-instrument' on 'meter1' @@ -477,7 +475,7 @@ describe('MeterProvider', () => { it('with different instrument types does not throw', async () => { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, // Add Views to rename both instruments (of different types) to the same name. views: [ @@ -546,7 +544,7 @@ describe('MeterProvider', () => { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, views: [ { @@ -617,7 +615,7 @@ describe('MeterProvider', () => { const reader2 = new TestMetricReader(); const reader1ShutdownSpy = sinon.spy(reader1, 'shutdown'); const reader2ShutdownSpy = sinon.spy(reader2, 'shutdown'); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, readers: [reader1, reader2], }); @@ -643,7 +641,7 @@ describe('MeterProvider', () => { const reader2 = new TestMetricReader(); const reader1ForceFlushSpy = sinon.spy(reader1, 'forceFlush'); const reader2ForceFlushSpy = sinon.spy(reader2, 'forceFlush'); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, readers: [reader1, reader2], }); diff --git a/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts b/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts index 258f0df00f5..ae1033f3338 100644 --- a/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts +++ b/packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts @@ -15,16 +15,22 @@ */ import * as metrics from '@opentelemetry/api'; import { ExportResult } from '@opentelemetry/core'; -import { ConsoleMetricExporter } from '../../src/export/ConsoleMetricExporter'; -import { PeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; +import { createConsoleMetricExporter } from '../../src/export/ConsoleMetricExporter'; +import { createPeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; import { ResourceMetrics } from '../../src/export/MetricData'; -import { MeterProvider } from '../../src/MeterProvider'; +import { createMeterProvider } from '../../src/MeterProvider'; import { defaultResource } from '../util'; import * as assert from 'assert'; import * as sinon from 'sinon'; import { assertAggregationTemporalitySelector } from './utils'; import { DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR } from '../../src/export/AggregationSelector'; -import { AggregationTemporality, InstrumentType } from '../../src'; +import { + AggregationTemporality, + InstrumentType, + MetricReader, + PushMetricExporter, +} from '../../src'; +import { MeterProvider } from '@opentelemetry/api'; async function waitForNumberOfExports( exporter: sinon.SinonSpy< @@ -48,22 +54,22 @@ async function waitForNumberOfExports( describe('ConsoleMetricExporter', () => { describe('export', () => { let previousConsoleDir: any; - let exporter: ConsoleMetricExporter; + let exporter: PushMetricExporter; let meterProvider: MeterProvider; - let metricReader: PeriodicExportingMetricReader; + let metricReader: MetricReader; let meter: metrics.Meter; beforeEach(() => { previousConsoleDir = console.dir; console.dir = () => {}; - exporter = new ConsoleMetricExporter(); - metricReader = new PeriodicExportingMetricReader({ + exporter = createConsoleMetricExporter(); + metricReader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 100, exportTimeoutMillis: 100, }); - meterProvider = new MeterProvider({ + meterProvider = createMeterProvider({ resource: defaultResource, readers: [metricReader], }); @@ -129,7 +135,7 @@ describe('ConsoleMetricExporter', () => { describe('constructor', () => { it('with no arguments should select cumulative temporality', () => { - const exporter = new ConsoleMetricExporter(); + const exporter = createConsoleMetricExporter(); assertAggregationTemporalitySelector( exporter, DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR @@ -137,7 +143,7 @@ describe('ConsoleMetricExporter', () => { }); it('with empty options should select cumulative temporality', () => { - const exporter = new ConsoleMetricExporter({}); + const exporter = createConsoleMetricExporter({}); assertAggregationTemporalitySelector( exporter, DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR @@ -145,7 +151,7 @@ describe('ConsoleMetricExporter', () => { }); it('with cumulative preference should select cumulative temporality', () => { - const exporter = new ConsoleMetricExporter({ + const exporter = createConsoleMetricExporter({ temporalitySelector: _ => AggregationTemporality.CUMULATIVE, }); assertAggregationTemporalitySelector( @@ -169,7 +175,7 @@ describe('ConsoleMetricExporter', () => { return AggregationTemporality.CUMULATIVE; } }; - const exporter = new ConsoleMetricExporter({ + const exporter = createConsoleMetricExporter({ temporalitySelector: selector, }); assertAggregationTemporalitySelector(exporter, selector); diff --git a/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts b/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts index 1e14a8ed5e1..25761fede79 100644 --- a/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts +++ b/packages/sdk-metrics/test/export/InMemoryMetricExporter.test.ts @@ -18,14 +18,16 @@ import { Resource } from '@opentelemetry/resources'; import * as metrics from '@opentelemetry/api'; import assert = require('assert'); import { AggregationTemporality } from '../../src/export/AggregationTemporality'; -import { InMemoryMetricExporter } from '../../src/export/InMemoryMetricExporter'; +import { createInMemoryMetricExporter } from '../../src/export/InMemoryMetricExporter'; import { ResourceMetrics } from '../../src/export/MetricData'; -import { PeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; -import { MeterProvider } from '../../src/MeterProvider'; +import { createPeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; +import { createMeterProvider } from '../../src/MeterProvider'; import { defaultResource } from '../util'; +import { MetricReader } from '../../src'; +import { MeterProvider } from '@opentelemetry/api'; async function waitForNumberOfExports( - exporter: InMemoryMetricExporter, + exporter: ReturnType, numberOfExports: number ): Promise { if (numberOfExports <= 0) { @@ -43,19 +45,21 @@ async function waitForNumberOfExports( } describe('InMemoryMetricExporter', () => { - let exporter: InMemoryMetricExporter; + let exporter: ReturnType; let meterProvider: MeterProvider; - let metricReader: PeriodicExportingMetricReader; + let metricReader: MetricReader; let meter: metrics.Meter; beforeEach(() => { - exporter = new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE); - metricReader = new PeriodicExportingMetricReader({ + exporter = createInMemoryMetricExporter({ + aggregationTemporality: AggregationTemporality.CUMULATIVE, + }); + metricReader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 100, exportTimeoutMillis: 100, }); - meterProvider = new MeterProvider({ + meterProvider = createMeterProvider({ resource: defaultResource, readers: [metricReader], }); diff --git a/packages/sdk-metrics/test/export/MetricReader.test.ts b/packages/sdk-metrics/test/export/MetricReader.test.ts index cc091c97977..0596e8344b7 100644 --- a/packages/sdk-metrics/test/export/MetricReader.test.ts +++ b/packages/sdk-metrics/test/export/MetricReader.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; -import { MeterProvider } from '../../src/MeterProvider'; +import { createMeterProvider } from '../../src/MeterProvider'; import { assertRejects } from '../test-utils'; import { emptyResourceMetrics, TestMetricProducer } from './TestMetricProducer'; import { TestMetricReader } from './TestMetricReader'; @@ -75,14 +75,14 @@ describe('MetricReader', () => { const reader = new TestMetricReader(); assert.throws( () => - new MeterProvider({ + createMeterProvider({ readers: [reader, reader], }), /MetricReader can not be bound to a MeterProvider again/ ); assert.throws( () => - new MeterProvider({ + createMeterProvider({ readers: [reader], }), /MetricReader can not be bound to a MeterProvider again/ @@ -151,7 +151,7 @@ describe('MetricReader', () => { const reader = new TestMetricReader({ metricProducers: [additionalProducer], }); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, readers: [reader], }); @@ -192,7 +192,7 @@ describe('MetricReader', () => { new TestMetricProducer({ errors: ['err2'] }), ], }); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ readers: [reader], }); diff --git a/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts b/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts index d4bc6e7d05f..60bd624ba46 100644 --- a/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts +++ b/packages/sdk-metrics/test/export/PeriodicExportingMetricReader.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { PeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; +import { createPeriodicExportingMetricReader } from '../../src/export/PeriodicExportingMetricReader'; import { AggregationTemporality } from '../../src/export/AggregationTemporality'; import { AggregationOption, @@ -129,15 +129,14 @@ describe('PeriodicExportingMetricReader', () => { }); describe('constructor', () => { - it('should construct PeriodicExportingMetricReader without exceptions', () => { + it('should createPeriodicExportingMetricReader without exceptions', () => { const exporter = new TestDeltaMetricExporter(); - assert.doesNotThrow( - () => - new PeriodicExportingMetricReader({ - exporter, - exportIntervalMillis: 4000, - exportTimeoutMillis: 3000, - }) + assert.doesNotThrow(() => + createPeriodicExportingMetricReader({ + exporter, + exportIntervalMillis: 4000, + exportTimeoutMillis: 3000, + }) ); }); @@ -145,7 +144,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestDeltaMetricExporter(); assert.throws( () => - new PeriodicExportingMetricReader({ + createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 0, exportTimeoutMillis: 0, @@ -158,7 +157,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestDeltaMetricExporter(); assert.throws( () => - new PeriodicExportingMetricReader({ + createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 1, exportTimeoutMillis: 0, @@ -171,7 +170,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestDeltaMetricExporter(); assert.throws( () => - new PeriodicExportingMetricReader({ + createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 100, exportTimeoutMillis: 200, @@ -185,7 +184,7 @@ describe('PeriodicExportingMetricReader', () => { const exporterMock = sinon.mock(exporter); exporterMock.expects('export').never(); - new PeriodicExportingMetricReader({ + createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 1, exportTimeoutMillis: 1, @@ -199,7 +198,7 @@ describe('PeriodicExportingMetricReader', () => { describe('setMetricProducer', () => { it('should start exporting periodically', async () => { const exporter = new TestMetricExporter(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 30, exportTimeoutMillis: 20, @@ -220,7 +219,7 @@ describe('PeriodicExportingMetricReader', () => { it('should keep running on export errors', async () => { const exporter = new TestMetricExporter(); exporter.throwExport = true; - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 30, exportTimeoutMillis: 20, @@ -241,7 +240,7 @@ describe('PeriodicExportingMetricReader', () => { it('should keep running on export failure', async () => { const exporter = new TestMetricExporter(); exporter.rejectExport = true; - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 30, exportTimeoutMillis: 20, @@ -263,7 +262,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestMetricExporter(); // set time longer than timeout. exporter.exportTime = 40; - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: 30, exportTimeoutMillis: 20, @@ -291,7 +290,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestMetricExporter(); const exporterMock = sinon.mock(exporter); exporterMock.expects('forceFlush').calledOnceWithExactly(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, @@ -403,7 +402,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestMetricExporter(); exporter.forceFlushTime = 60; - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, @@ -420,7 +419,7 @@ describe('PeriodicExportingMetricReader', () => { it('should throw when exporter throws', async () => { const exporter = new TestMetricExporter(); exporter.throwFlush = true; - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, @@ -435,7 +434,7 @@ describe('PeriodicExportingMetricReader', () => { const exporterMock = sinon.mock(exporter); // expect once on shutdown. exporterMock.expects('forceFlush').once(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, @@ -453,7 +452,7 @@ describe('PeriodicExportingMetricReader', () => { it('should default to Cumulative with no exporter preference', () => { // Adding exporter without preference. const exporter = new TestMetricExporter(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, }); @@ -468,7 +467,7 @@ describe('PeriodicExportingMetricReader', () => { it('should default to exporter preference', () => { // Adding exporter with DELTA preference. const exporter = new TestDeltaMetricExporter(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, }); @@ -485,7 +484,7 @@ describe('PeriodicExportingMetricReader', () => { it('should use default aggregation with no exporter preference', () => { // Adding exporter without preference. const exporter = new TestMetricExporter(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, }); @@ -498,7 +497,7 @@ describe('PeriodicExportingMetricReader', () => { it('should default to exporter preference', () => { // Adding exporter with Drop Aggregation preference. const exporter = new TestDropMetricExporter(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, }); @@ -518,7 +517,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestMetricExporter(); const exporterMock = sinon.mock(exporter); exporterMock.expects('forceFlush').calledOnceWithExactly(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, @@ -533,7 +532,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestMetricExporter(); exporter.forceFlushTime = 1000; - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, @@ -550,7 +549,7 @@ describe('PeriodicExportingMetricReader', () => { const exporter = new TestMetricExporter(); const exporterMock = sinon.mock(exporter); exporterMock.expects('shutdown').calledOnceWithExactly(); - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, @@ -568,7 +567,7 @@ describe('PeriodicExportingMetricReader', () => { it('should throw on non-initialized instance.', async () => { const exporter = new TestMetricExporter(); exporter.throwFlush = true; - const reader = new PeriodicExportingMetricReader({ + const reader = createPeriodicExportingMetricReader({ exporter: exporter, exportIntervalMillis: MAX_32_BIT_INT, exportTimeoutMillis: 80, diff --git a/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts b/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts index 3787308d9ec..8ba78ff1104 100644 --- a/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts +++ b/packages/sdk-metrics/test/regression/cumulative-exponential-histogram.test.ts @@ -21,7 +21,7 @@ import { AggregationTemporality, AggregationType, InstrumentType, - MeterProvider, + createMeterProvider, MetricReader, } from '../../src'; import { TestMetricReader } from '../export/TestMetricReader'; @@ -57,7 +57,7 @@ describe('cumulative-exponential-histogram', () => { : { type: AggregationType.DEFAULT }; }, }); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ readers: [reader], }); diff --git a/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts b/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts index 4cb73ff4984..f34956ac9cb 100644 --- a/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts +++ b/packages/sdk-metrics/test/regression/histogram-recording-nan.test.ts @@ -17,7 +17,7 @@ import * as assert from 'assert'; import { AggregationTemporality, - MeterProvider, + createMeterProvider, MetricReader, DataPoint, ExponentialHistogram, @@ -36,7 +36,7 @@ describe('histogram-recording-nan', () => { return { type: AggregationType.EXPONENTIAL_HISTOGRAM }; }, }); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ readers: [reader], }); @@ -68,7 +68,7 @@ describe('histogram-recording-nan', () => { return { type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM }; }, }); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ readers: [reader], }); diff --git a/packages/sdk-metrics/test/regression/two-metric-readers-async-instrument.test.ts b/packages/sdk-metrics/test/regression/two-metric-readers-async-instrument.test.ts index 74913f9476b..b83e41a2b58 100644 --- a/packages/sdk-metrics/test/regression/two-metric-readers-async-instrument.test.ts +++ b/packages/sdk-metrics/test/regression/two-metric-readers-async-instrument.test.ts @@ -15,7 +15,7 @@ */ import * as assert from 'assert'; -import { DataPointType, MeterProvider, MetricReader } from '../../src'; +import { DataPointType, createMeterProvider, MetricReader } from '../../src'; import { TestDeltaMetricReader } from '../export/TestMetricReader'; import { assertDataPoint, assertMetricData } from '../util'; @@ -25,7 +25,7 @@ describe('two-metric-readers-async-instrument', () => { it('both metric readers should collect metrics', async () => { const reader1 = new TestDeltaMetricReader(); const reader2 = new TestDeltaMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ readers: [reader1, reader2], }); diff --git a/packages/sdk-metrics/test/state/MeterSharedState.test.ts b/packages/sdk-metrics/test/state/MeterSharedState.test.ts index 74b4ff9a273..c379fdf7e29 100644 --- a/packages/sdk-metrics/test/state/MeterSharedState.test.ts +++ b/packages/sdk-metrics/test/state/MeterSharedState.test.ts @@ -17,12 +17,12 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { - MeterProvider, DataPointType, MetricReader, InstrumentType, AggregationType, ViewOptions, + createMeterProvider, } from '../../src'; import { assertMetricData, @@ -45,7 +45,7 @@ describe('MeterSharedState', () => { describe('registerMetricStorage', () => { function setupMeter(views?: ViewOptions[], readers?: MetricReader[]) { - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, views, readers: readers, @@ -55,10 +55,14 @@ describe('MeterSharedState', () => { return { meter, - meterSharedState: meterProvider['_sharedState'].getMeterSharedState({ + meterSharedState: (meterProvider as any)[ + '_sharedState' + ].getMeterSharedState({ name: 'test-meter', }), - collectors: Array.from(meterProvider['_sharedState'].metricCollectors), + collectors: Array.from( + (meterProvider as any)['_sharedState'].metricCollectors + ), }; } @@ -188,7 +192,7 @@ describe('MeterSharedState', () => { const cumulativeReader = new TestMetricReader(); const deltaReader = new TestDeltaMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, views: views, readers: [cumulativeReader, deltaReader], diff --git a/packages/sdk-metrics/test/state/MetricCollector.test.ts b/packages/sdk-metrics/test/state/MetricCollector.test.ts index f173a48446d..531a6844a61 100644 --- a/packages/sdk-metrics/test/state/MetricCollector.test.ts +++ b/packages/sdk-metrics/test/state/MetricCollector.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; -import { MeterProvider } from '../../src'; +import { createMeterProvider } from '../../src'; import { TimeoutError } from '../../src/utils'; import { DataPointType } from '../../src/export/MetricData'; import { MeterProviderSharedState } from '../../src/state/MeterProviderSharedState'; @@ -56,7 +56,7 @@ describe('MetricCollector', () => { describe('collect', () => { function setupInstruments() { const reader = new TestMetricReader(); - const meterProvider = new MeterProvider({ + const meterProvider = createMeterProvider({ resource: defaultResource, readers: [reader], });