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.
Resource Attributes
Section titled “Resource Attributes”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", ... }Emitted Metric Records
Section titled “Emitted Metric Records”| Metric name | Unit | Source |
|---|---|---|
health.status | state | Aggregate report status (1, 0.5, 0) |
health.run.duration_ms | milliseconds | report.durationMs |
health.check.status | state | Status of each check (1, 0.5, 0) |
health.check.duration_ms | milliseconds | Duration of each check |
| collector metrics | metric.unit | Every entry in report.metrics |
Troubleshooting & Common Pitfalls
Section titled “Troubleshooting & Common Pitfalls”1. Nothing Reaches the Collector
Section titled “1. Nothing Reaches the Collector”The package does not send data. If no metric arrives, check the OpenTelemetry SDK and exporter in your application.
2. Collector Metrics Are Missing
Section titled “2. Collector Metrics Are Missing”Collector metrics come from report.metrics. Register collectors on the manager, and run a scope and profile that include them.