diff --git a/doc/metrics.md b/doc/metrics.md index 9e1c642b47d..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(createPeriodicExportingMetricReader({ - 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 543c1d22030..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(createPeriodicExportingMetricReader({ - 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 b5ed0af74f3..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( - createPeriodicExportingMetricReader({ - 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 1bb277eddb4..6d7ef63a164 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'); @@ -56,17 +56,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 1a9de77c2f1..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(createPeriodicExportingMetricReader({ - 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-http/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md index 417f4b74f70..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(createPeriodicExportingMetricReader({ - 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(createPeriodicExportingMetricReader({ - 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/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md index c08fdb93212..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(createPeriodicExportingMetricReader({ - 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-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/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'}, ] }) ```