From be8014b6d8be140fc376ba5b04ac02e2c9b7e9ab Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Fri, 2 Jun 2023 14:49:43 -0400 Subject: [PATCH 1/6] docs: add example of exponential histogram --- examples/otlp-exporter-node/metrics.js | 45 ++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/examples/otlp-exporter-node/metrics.js b/examples/otlp-exporter-node/metrics.js index f6c3efa55b5..ea019ee05c8 100644 --- a/examples/otlp-exporter-node/metrics.js +++ b/examples/otlp-exporter-node/metrics.js @@ -4,25 +4,50 @@ 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 { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); +// const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics'); +const { + ExponentialHistogramAggregation, + MeterProvider, + PeriodicExportingMetricReader, + View, +} = require('@opentelemetry/sdk-metrics'); const { Resource } = require('@opentelemetry/resources'); -const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); +const { + SemanticResourceAttributes, +} = require('@opentelemetry/semantic-conventions'); // Optional and only needed to see the internal diagnostic logging (during development) diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); -const metricExporter = new OTLPMetricExporter({}); +const metricExporter = new OTLPMetricExporter({ + // headers: { + // foo: 'bar' + // }, +}); + +// Define view for the exponential histogram metric +const expHistogramView = new View({ + aggregation: new ExponentialHistogramAggregation(), + instrumentName: 'test_exponential_histogram', +}); +// Note, the instrumentName is the same as the name that has been passed for +// the Meter#createHistogram function for exponentialHistogram. +// Create an instance of the metric provider const meterProvider = new MeterProvider({ resource: new Resource({ [SemanticResourceAttributes.SERVICE_NAME]: 'basic-metric-service', }), + views: [expHistogramView], }); -meterProvider.addMetricReader(new PeriodicExportingMetricReader({ - exporter: metricExporter, - exportIntervalMillis: 1000, -})); +meterProvider.addMetricReader( + new PeriodicExportingMetricReader({ + exporter: metricExporter, + // exporter: new ConsoleMetricExporter(), + exportIntervalMillis: 1000, + }) +); const meter = meterProvider.getMeter('example-exporter-collector'); @@ -36,6 +61,11 @@ const upDownCounter = meter.createUpDownCounter('test_up_down_counter', { const histogram = meter.createHistogram('test_histogram', { description: 'Example of a Histogram', + boundaries: [0, 10, 100, 1000], +}); + +const exponentialHistogram = meter.createHistogram('test_exp_histogram', { + description: 'Example of an ExponentialHistogram', }); const attributes = { pid: process.pid, environment: 'staging' }; @@ -44,4 +74,5 @@ setInterval(() => { requestCounter.add(1, attributes); upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes); histogram.record(Math.random(), attributes); + exponentialHistogram.record(Math.random(), attributes); }, 1000); From 16270153fd2c670b2c35f8ab213a3b4389b15cf4 Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Fri, 2 Jun 2023 14:52:46 -0400 Subject: [PATCH 2/6] docs: fix some typos in metrics doc --- doc/metrics.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/metrics.md b/doc/metrics.md index 7162f43638f..e2f2b5a1472 100644 --- a/doc/metrics.md +++ b/doc/metrics.md @@ -20,7 +20,7 @@ _Metrics API Reference: Date: Fri, 2 Jun 2023 15:10:48 -0400 Subject: [PATCH 3/6] update changelog --- experimental/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 4935d9b83ba..93f6df7828d 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to experimental packages in this project will be documented ## Unreleased +### :books: (Refine Doc) + +* docs(sdk-metrics): add example of exponential histogram metric [#3855](https://github.com/open-telemetry/opentelemetry-js/pull/3855) @JamieDanielson + ### :boom: Breaking Change * fix(exporter-logs-otlp-grpc): change OTLPLogsExporter to OTLPLogExporter [#3819](https://github.com/open-telemetry/opentelemetry-js/pull/3819) @fuaiyi From 971a55d7041a34dd64fd15e95ddf3ae1452f90fa Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Mon, 19 Jun 2023 17:35:24 -0400 Subject: [PATCH 4/6] Update examples/otlp-exporter-node/metrics.js Co-authored-by: Chengzhong Wu --- examples/otlp-exporter-node/metrics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/otlp-exporter-node/metrics.js b/examples/otlp-exporter-node/metrics.js index ea019ee05c8..e96555358e7 100644 --- a/examples/otlp-exporter-node/metrics.js +++ b/examples/otlp-exporter-node/metrics.js @@ -64,7 +64,7 @@ const histogram = meter.createHistogram('test_histogram', { boundaries: [0, 10, 100, 1000], }); -const exponentialHistogram = meter.createHistogram('test_exp_histogram', { +const exponentialHistogram = meter.createHistogram('test_exponential_histogram', { description: 'Example of an ExponentialHistogram', }); From 9feb8a018796e6721c160e91b5a6dc6fae237eca Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Wed, 5 Jul 2023 16:29:01 -0400 Subject: [PATCH 5/6] move comment into view section --- examples/otlp-exporter-node/metrics.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/otlp-exporter-node/metrics.js b/examples/otlp-exporter-node/metrics.js index e96555358e7..2d1ef5f670a 100644 --- a/examples/otlp-exporter-node/metrics.js +++ b/examples/otlp-exporter-node/metrics.js @@ -28,10 +28,10 @@ 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', }); -// Note, the instrumentName is the same as the name that has been passed for -// the Meter#createHistogram function for exponentialHistogram. // Create an instance of the metric provider const meterProvider = new MeterProvider({ From 97affc5874704aa5356036f3df2fb213cc7fd2fc Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Fri, 7 Jul 2023 08:53:43 -0400 Subject: [PATCH 6/6] Update examples/otlp-exporter-node/metrics.js Co-authored-by: Marc Pichler --- examples/otlp-exporter-node/metrics.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/otlp-exporter-node/metrics.js b/examples/otlp-exporter-node/metrics.js index 2d1ef5f670a..0594ba42e3f 100644 --- a/examples/otlp-exporter-node/metrics.js +++ b/examples/otlp-exporter-node/metrics.js @@ -61,7 +61,6 @@ const upDownCounter = meter.createUpDownCounter('test_up_down_counter', { const histogram = meter.createHistogram('test_histogram', { description: 'Example of a Histogram', - boundaries: [0, 10, 100, 1000], }); const exponentialHistogram = meter.createHistogram('test_exponential_histogram', {