Skip to content

Commit fc28665

Browse files
JamieDanielsonlegendecaspichlermarc
authored
docs(sdk-metrics): add example of exponential histogram metric (#3855)
Co-authored-by: Chengzhong Wu <[email protected]> Co-authored-by: Marc Pichler <[email protected]>
1 parent 6d13eb4 commit fc28665

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

doc/metrics.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ _Metrics API Reference: <https://open-telemetry.github.io/opentelemetry-js/class
2020
- [Customizing the metric attributes of instrument](#customizing-the-metric-attributes-of-instrument)
2121
- [Exporting measurements](#exporting-measurements)
2222
- [Exporting measurements to Prometheus](#exporting-measurements-to-prometheus)
23-
- [Exporting measurements to Opentelemetry Protocol](#exporting-measurements-to-opentelemetry-protocol)
23+
- [Exporting measurements to OpenTelemetry Protocol](#exporting-measurements-to-opentelemetry-protocol)
2424

2525
## Getting Started
2626

@@ -266,7 +266,7 @@ Most of the time, instruments will be used to measure operations in your applica
266266

267267
```typescript
268268
async function myTask() {
269-
const histogram = meter.createHistogram("taks.duration");
269+
const histogram = meter.createHistogram("task.duration");
270270
const startTime = new Date().getTime()
271271
try {
272272
// Wait for five seconds before continuing code execution
@@ -479,7 +479,7 @@ new View({
479479

480480
After you have instrumented your application with metrics, you also need to make
481481
sure that the metrics get collected by your metrics backend. The most common formats
482-
that are used are Prometheus and OLTP.
482+
that are used are Prometheus and OTLP.
483483

484484
The latter is the [OpenTelemetry protocol format](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md)
485485
which is supported by the OpenTelemetry Collector. The former is based on the [OpenMetrics
@@ -528,7 +528,7 @@ at: <https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/e
528528
## Exporting measurements to OpenTelemetry Protocol
529529

530530
OpenTelemetry JavaScript comes with three different kinds of exporters that export
531-
the OTLP protocol, a) [over HTTP](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-exporter-metrics-otlp-http), b) [over GRPC](https://www.npmjs.com/package/@opentelemetry/exporter-metrics-otlp-grpc), c) [over Protofbuf](https://www.npmjs.com/package/@opentelemetry/exporter-metrics-otlp-proto).
531+
the OTLP protocol, a) [over HTTP](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-exporter-metrics-otlp-http), b) [over GRPC](https://www.npmjs.com/package/@opentelemetry/exporter-metrics-otlp-grpc), c) [over Protobuf](https://www.npmjs.com/package/@opentelemetry/exporter-metrics-otlp-proto).
532532

533533
The example below shows how you can configure OpenTelemetry JavaScript to use
534534
the OTLP exporter using http/protobuf.

examples/otlp-exporter-node/metrics.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,50 @@ const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api');
44
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
55
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
66
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
7-
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
7+
// const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
8+
const {
9+
ExponentialHistogramAggregation,
10+
MeterProvider,
11+
PeriodicExportingMetricReader,
12+
View,
13+
} = require('@opentelemetry/sdk-metrics');
814
const { Resource } = require('@opentelemetry/resources');
9-
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
15+
const {
16+
SemanticResourceAttributes,
17+
} = require('@opentelemetry/semantic-conventions');
1018

1119
// Optional and only needed to see the internal diagnostic logging (during development)
1220
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
1321

14-
const metricExporter = new OTLPMetricExporter({});
22+
const metricExporter = new OTLPMetricExporter({
23+
// headers: {
24+
// foo: 'bar'
25+
// },
26+
});
27+
28+
// Define view for the exponential histogram metric
29+
const expHistogramView = new View({
30+
aggregation: new ExponentialHistogramAggregation(),
31+
// Note, the instrumentName is the same as the name that has been passed for
32+
// the Meter#createHistogram function for exponentialHistogram.
33+
instrumentName: 'test_exponential_histogram',
34+
});
1535

36+
// Create an instance of the metric provider
1637
const meterProvider = new MeterProvider({
1738
resource: new Resource({
1839
[SemanticResourceAttributes.SERVICE_NAME]: 'basic-metric-service',
1940
}),
41+
views: [expHistogramView],
2042
});
2143

22-
meterProvider.addMetricReader(new PeriodicExportingMetricReader({
23-
exporter: metricExporter,
24-
exportIntervalMillis: 1000,
25-
}));
44+
meterProvider.addMetricReader(
45+
new PeriodicExportingMetricReader({
46+
exporter: metricExporter,
47+
// exporter: new ConsoleMetricExporter(),
48+
exportIntervalMillis: 1000,
49+
})
50+
);
2651

2752
const meter = meterProvider.getMeter('example-exporter-collector');
2853

@@ -38,10 +63,15 @@ const histogram = meter.createHistogram('test_histogram', {
3863
description: 'Example of a Histogram',
3964
});
4065

66+
const exponentialHistogram = meter.createHistogram('test_exponential_histogram', {
67+
description: 'Example of an ExponentialHistogram',
68+
});
69+
4170
const attributes = { pid: process.pid, environment: 'staging' };
4271

4372
setInterval(() => {
4473
requestCounter.add(1, attributes);
4574
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);
4675
histogram.record(Math.random(), attributes);
76+
exponentialHistogram.record(Math.random(), attributes);
4777
}, 1000);

experimental/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to experimental packages in this project will be documented
44

55
## Unreleased
66

7+
### :books: (Refine Doc)
8+
9+
* docs(sdk-metrics): add example of exponential histogram metric [#3855](https://github.com/open-telemetry/opentelemetry-js/pull/3855) @JamieDanielson
10+
711
### :boom: Breaking Change
812

913
### :rocket: (Enhancement)

0 commit comments

Comments
 (0)