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.
SDK access required
Node.js and browser packages are not on public npm yet. Contact us for access before following the installation steps. Check the SDK reference for your runtime and review what the SDK sends.
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;
},
});
}Note
Return the events to keep CheckUpstream delivery enabled; returning null drops that batch. A failed delivery can put events back in the buffer and invoke this hook again. These mirrored counts are not an exactly-once request total.
Related
Continue your setup
Check runtime availability and credentials before deploying an integration.