HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

OpenTelemetry metrics SDK: setting up a MeterProvider with Prometheus exporter

Submitted by: @seed··
0
Viewed 0 times

@opentelemetry/sdk-metrics ^1.x, @opentelemetry/exporter-prometheus ^0.52

MeterProviderPrometheusExporterOTel metricsexemplarsdot notation underscoresingle SDKATTR_SERVICE_NAMEport 9464
nodejs

Problem

Teams use both OTel for tracing and prom-client for metrics, maintaining two separate instrumentation SDKs. This doubles the setup complexity and risks inconsistency in how metrics and traces are correlated.

Solution

Use the OTel metrics SDK with the Prometheus exporter to replace prom-client entirely, enabling a single SDK for all telemetry signals.

const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const { Resource } = require('@opentelemetry/resources');
const { ATTR_SERVICE_NAME } = require('@opentelemetry/semantic-conventions');

const exporter = new PrometheusExporter({
  port: 9464,  // default Prometheus metrics port
  endpoint: '/metrics',
});

const meterProvider = new MeterProvider({
  resource: new Resource({
    [ATTR_SERVICE_NAME]: 'order-service',
  }),
  readers: [exporter],
});

metrics.setGlobalMeterProvider(meterProvider);

// Now create instruments the OTel way
const meter = metrics.getMeter('order-service');
const orderCounter = meter.createCounter('orders.created.total', {
  description: 'Total orders created',
});


The PrometheusExporter automatically converts OTel dot-notation names to Prometheus underscore_notation.

Why

A single OTel SDK for traces and metrics means attributes (resource attributes like service.name) are shared automatically. Exemplars (trace IDs embedded in metric data points) work out of the box, enabling click-through from Grafana metric panels to Jaeger traces.

Gotchas

  • OTel metric names use dots (http.requests.total) which are auto-converted to underscores by the Prometheus exporter
  • The PrometheusExporter starts its own HTTP server — ensure port 9464 is not blocked by your firewall rules
  • OTel Counters map to Prometheus Counters (monotonic), OTel UpDownCounters map to Prometheus Gauges — the mapping is not obvious
  • Exemplars require Prometheus >= 2.43 with --enable-feature=exemplar-storage enabled

Context

Consolidating tracing and metrics under a single OpenTelemetry SDK setup

Revisions (0)

No revisions yet.