@opentelemetry/exporter-metrics-otlp-http

OpenTelemetry Collector Metrics Exporter allows user to send collected metrics to the OpenTelemetry Collector

Usage no npm install needed!

<script type="module">
  import opentelemetryExporterMetricsOtlpHttp from 'https://cdn.skypack.dev/@opentelemetry/exporter-metrics-otlp-http';
</script>

README

OpenTelemetry Collector Metrics Exporter for web and node

NPM Published Version dependencies devDependencies Apache License

This module provides exporter for web and node to be used with opentelemetry-collector - last tested with version 0.25.0.

Installation

npm install --save @opentelemetry/exporter-metrics-otlp-http

Service Name

The OpenTelemetry Collector Metrics Exporter does not have a service name configuration. In order to set the service name, use the service.name resource attribute as prescribed in the OpenTelemetry Resource Semantic Conventions. To see sample code and documentation for the traces exporter, visit the Collector Trace Exporter for web and node.

Metrics in Web

The OTLPMetricExporter in Web expects the endpoint to end in /v1/metrics.

import { MeterProvider } from '@opentelemetry/sdk-metrics-base';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
const collectorOptions = {
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:55681/v1/metrics
  headers: {}, // an optional object containing custom headers to be sent with each request
  concurrencyLimit: 1, // an optional limit on pending requests
};
const exporter = new OTLPMetricExporter(collectorOptions);

// Register the exporter
const meter = new MeterProvider({
  exporter,
  interval: 60000,
}).getMeter('example-meter');

// Now, start recording data
const counter = meter.createCounter('metric_name');
counter.add(10, { 'key': 'value' });

Metrics in Node

const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const { OTLPMetricExporter } =  require('@opentelemetry/exporter-metrics-otlp-http');
const collectorOptions = {
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:55681/v1/metrics
  concurrencyLimit: 1, // an optional limit on pending requests
};
const exporter = new OTLPMetricExporter(collectorOptions);

// Register the exporter
const meter = new MeterProvider({
  exporter,
  interval: 60000,
}).getMeter('example-meter');

// Now, start recording data
const counter = meter.createCounter('metric_name');
counter.add(10, { 'key': 'value' });

GRPC

For exporting metrics with GRPC please check exporter-metrics-otlp-grpc

PROTOBUF

For exporting metrics with PROTOBUF please check exporter-metrics-otlp-proto

Configuration options as environment variables

Instead of providing options to OTLPMetricExporter and OTLPTraceExporter explicitly, environment variables may be provided instead.

OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4317
# this will automatically append the version and signal path
# e.g. https://localhost:4317/v1/traces for `OTLPTraceExporter` and https://localhost:4317/v1/metrics for `OTLPMetricExporter`

If the trace and metric exporter endpoints have different providers, the env var for per-signal endpoints are available to use

OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://trace-service:4317/v1/traces
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://metric-service:4317/v1/metrics
# version and signal needs to be explicit

The per-signal endpoints take precedence and overrides OTEL_EXPORTER_OTLP_ENDPOINT

For more details, see OpenTelemetry Specification on Protocol Exporter.

Running opentelemetry-collector locally to see the metrics

  1. Go to examples/otlp-exporter-node
  2. run npm run docker:start
  3. Open page at http://localhost:9411/zipkin/ to observe the metrics

Useful links

License

Apache 2.0 - See LICENSE for more information.