Production Guides & Recipes
Exposing the /metrics Endpoint
Section titled “Exposing the /metrics Endpoint”In Next.js, use createNextMetricsRoute from @nexload-sdk/healthcheck-next. In any other framework, serialize the report yourself:
import { toPrometheusText } from "@nexload-sdk/healthcheck-prometheus";
import { health } from "@/lib/health";
export async function handleMetrics() { const report = await health.run("all", { profile: "monitoring" }); const body = toPrometheusText(report, { prefix: "shop_api", defaultLabels: { version: "1.2.0" }, includeDescriptions: true, });
return new Response(body, { status: 200, headers: { "Content-Type": "text/plain; version=0.0.4; charset=utf-8" }, });}For OpenMetrics, call toOpenMetricsText with the same options and answer with Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8.
Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
prefix | string | "nexload" | Metric name prefix. |
defaultLabels | Record<string, string> | {} | Labels added to every series. Labels outside the allowlist are dropped. |
includeDescriptions | boolean | false | Emits # HELP lines. |
Emitted Series
Section titled “Emitted Series”| Series | Labels | Value |
|---|---|---|
<prefix>_health_status | scope, service, status | 1 for the active status, 0 for the other two. |
<prefix>_health_check_status | check, component, scope, service, status | Same encoding, per check. |
<prefix>_health_check_duration_milliseconds | check, component, scope, service | Duration of each check. |
<prefix>_health_run_duration_milliseconds | scope, service | Duration of the whole run. |
<prefix>_<collector_metric_name> | from the collector | Every metric in report.metrics. |
Troubleshooting & Common Pitfalls
Section titled “Troubleshooting & Common Pitfalls”1. Prometheus Scraper Reports text format parse error
Section titled “1. Prometheus Scraper Reports text format parse error”Send the content type that matches the function you called: text/plain; version=0.0.4 for toPrometheusText, application/openmetrics-text; version=1.0.0 for toOpenMetricsText.
2. A Label Does Not Appear
Section titled “2. A Label Does Not Appear”Only the allowlisted labels are emitted. Do not use user IDs or request IDs as labels; put static service data in defaultLabels.