Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(sdk-metrics): add example of exponential histogram metric #3855

8 changes: 4 additions & 4 deletions doc/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _Metrics API Reference: <https://open-telemetry.github.io/opentelemetry-js/class
- [Customizing the metric attributes of instrument](#customizing-the-metric-attributes-of-instrument)
- [Exporting measurements](#exporting-measurements)
- [Exporting measurements to Prometheus](#exporting-measurements-to-prometheus)
- [Exporting measurements to Opentelemetry Protocol](#exporting-measurements-to-opentelemetry-protocol)
- [Exporting measurements to OpenTelemetry Protocol](#exporting-measurements-to-opentelemetry-protocol)

## Getting Started

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

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

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

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

OpenTelemetry JavaScript comes with three different kinds of exporters that export
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).
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).

The example below shows how you can configure OpenTelemetry JavaScript to use
the OTLP exporter using http/protobuf.
Expand Down
44 changes: 37 additions & 7 deletions examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function for exponentialHistogram.
instrumentName: 'test_exponential_histogram',
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
});

// 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');

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

const exponentialHistogram = meter.createHistogram('test_exponential_histogram', {
description: 'Example of an ExponentialHistogram',
});

const attributes = { pid: process.pid, environment: 'staging' };

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);
4 changes: 4 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

### :rocket: (Enhancement)
Expand Down