Skip to content

Production Guides & Recipes

Record Health Metrics with Your OpenTelemetry SDK

Section titled “Record Health Metrics with Your OpenTelemetry SDK”

The package returns data. Your application owns the OpenTelemetry SDK, the meter and the exporter. This example assumes @opentelemetry/api is already set up in your service:

import { toOtelMetricRecords } from "@nexload-sdk/healthcheck-otel";
import { metrics } from "@opentelemetry/api";
import { health } from "@/lib/health";
const meter = metrics.getMeter("health");
const gauges = new Map<string, ReturnType<typeof meter.createGauge>>();
export async function recordHealth() {
const report = await health.run("diagnostics");
for (const record of toOtelMetricRecords(report)) {
let gauge = gauges.get(record.name);
if (!gauge) {
gauge = meter.createGauge(record.name, { unit: record.unit });
gauges.set(record.name, gauge);
}
gauge.record(record.value, record.attributes);
}
}

Each OtelMetricRecord has name, value, attributes, observedAt, and optional unit and type.


Use toOtelResourceAttributes(report) when you build the SDK resource or when you want to tag a log record:

import { toOtelResourceAttributes } from "@nexload-sdk/healthcheck-otel";
const attributes = toOtelResourceAttributes(report);
// { "service.name": "api", "service.version": "1.0.0", "process.runtime.name": "node", ... }

Metric nameUnitSource
health.statusstateAggregate report status (1, 0.5, 0)
health.run.duration_msmillisecondsreport.durationMs
health.check.statusstateStatus of each check (1, 0.5, 0)
health.check.duration_msmillisecondsDuration of each check
collector metricsmetric.unitEvery entry in report.metrics

The package does not send data. If no metric arrives, check the OpenTelemetry SDK and exporter in your application.

Collector metrics come from report.metrics. Register collectors on the manager, and run a scope and profile that include them.