Skip to main content
All recipes

OpenTelemetry

CodeGenericOTLP

Record request duration and errors through an existing OpenTelemetry metrics provider. The beforeSend hook sees batches selected for delivery, so sampling and retries affect these measurements.

Mirror events to your OTel exporter

Configure and start your OpenTelemetry metrics provider before this code runs. The metrics API alone does not export data. Keep your existing provider and exporter shutdown handlers.

instrumentation.ts
import { metrics } from "@opentelemetry/api";
import { initCheckUpstream } from "@checkupstream/sdk/integrations/next";

const meter = metrics.getMeter("checkupstream");
const latency = meter.createHistogram("upstream.latency_ms", { unit: "ms" });
const errors = meter.createCounter("upstream.errors");

export function register() {
  const sdkKey = process.env.CHECKUPSTREAM_SDK_KEY;
  if (!sdkKey) throw new Error("Set CHECKUPSTREAM_SDK_KEY before starting the app");

  initCheckUpstream({
    sdkKey,
    beforeSend: (events) => {
      for (const e of events) {
        latency.record(e.latency_ms, { service: e.service, method: e.method });
        if (e.error_class) {
          errors.add(1, { service: e.service, error_class: e.error_class });
        }
      }
      // Return the events so CheckUpstream still ingests them.
      return events;
    },
  });
}

Continue your setup

Check runtime availability and credentials before deploying an integration.