@smarthivelabs-devs/observe-node
v26.3.0
Published
SmartObserve SDK for Node.js, Express, Fastify, and NestJS
Readme
@smarthivelabs-devs/observe-node
Errors, logs, and traces for Node.js. Failed ingest never throws into your app.
Requires Node 22+. Use a server DSN (sk_) from SmartObserve project settings. Public (pk_) keys are for browsers and Expo only.
https://<sk_server_key>@observe.smarthivelabs.dev/<project-slug>Install
pnpm add @smarthivelabs-devs/observe-nodeStart
Call init once at process boot, before the HTTP server listens.
import { SmartObserve } from "@smarthivelabs-devs/observe-node";
SmartObserve.init({
dsn: process.env.SMARTOBSERVE_DSN,
environment: process.env.SMARTOBSERVE_ENVIRONMENT ?? "production",
release: process.env.SMARTOBSERVE_RELEASE,
});If dsn is omitted, these env vars are used: SMARTOBSERVE_DSN, SMARTOBSERVE_ENVIRONMENT, SMARTOBSERVE_RELEASE. With no DSN, the client is disabled and every call is a no-op.
Unhandled exceptions and promise rejections are captured automatically. Call await SmartObserve.flush() (or close()) on shutdown so the last batch is sent.
What you send
SmartObserve.captureException(err);
SmartObserve.captureMessage("checkout failed", "error", { orderId });
SmartObserve.log.info("ready");
SmartObserve.log.warn("retrying", { attempt: 2 });
SmartObserve.log.error("queue stalled");
SmartObserve.setUser({ id: user.id, username: user.handle });
SmartObserve.setTag("tenant", org.slug);
SmartObserve.setContext("billing", { plan: "pro" });
SmartObserve.addBreadcrumb({ category: "auth", message: "session refreshed" });
await SmartObserve.startSpan({ name: "POST /orders", operation: "http.server" }, async (span) => {
span.setAttribute("http.route", "/orders");
return createOrder();
});setUser(null) / setTag(key, null) / setContext(key, null) clears that field. Pass beforeSend to drop or edit events (return null drops). Sensitive keys such as password, authorization, and cookie are redacted; add more with redactKeys.
Cron monitors
Wrap a scheduled job so SmartObserve knows when it ran, how long it took, and whether it failed. Missed runs (the job never started) and timeouts are detected on the server, even if your scheduler is down.
await SmartObserve.withMonitor("nightly-billing", async () => {
await runBilling();
}, { schedule: { type: "crontab", value: "0 2 * * *" }, timezone: "Africa/Accra", maxRuntime: 30 });The monitor is created (or updated) from that config on the first run. Without a config it only tracks reported failures. For manual control:
const checkInId = SmartObserve.captureCheckIn({ monitorSlug: "sync", status: "in_progress" });
// ...
SmartObserve.captureCheckIn({ monitorSlug: "sync", status: "ok", checkInId });Jobs triggered by the Smart Hive mailer scheduler (schedules.registerHttpJob) wrap their HTTP handler the same way, and the dashboard's Crons page can import mailer schedules directly. Shell scripts can POST /api/v1/monitors/<slug>/check-ins with a server key.
User feedback
const eventId = SmartObserve.captureException(error);
SmartObserve.captureFeedback({ message: "Export kept failing", email: "[email protected]", associatedEventId: eventId });Frameworks
Express — request spans plus next(err) capture. Mount the error handler last.
import { expressErrorHandler, expressMiddleware } from "@smarthivelabs-devs/observe-node/express";
const client = SmartObserve.init({ dsn: process.env.SMARTOBSERVE_DSN });
app.use(expressMiddleware(client));
app.use(expressErrorHandler(client));Fastify — captures onError hook failures.
import { fastifyPlugin } from "@smarthivelabs-devs/observe-node/fastify";
const client = SmartObserve.init({ dsn: process.env.SMARTOBSERVE_DSN });
await app.register(fastifyPlugin(client));NestJS — global exception filter.
import { SmartObserveExceptionFilter } from "@smarthivelabs-devs/observe-node/nest";
const client = SmartObserve.init({ dsn: process.env.SMARTOBSERVE_DSN });
app.useGlobalFilters(new SmartObserveExceptionFilter(client));Outgoing http / https requests are breadcrumbed when trackHttp is on (default). OpenTelemetry context is used for parent spans when present.
Options
| Option | Default | Notes |
| --- | --- | --- |
| dsn | SMARTOBSERVE_DSN | Server key DSN |
| environment | SMARTOBSERVE_ENVIRONMENT or NODE_ENV | Shown on issues |
| release / dist | SMARTOBSERVE_RELEASE | Tie events to a deploy |
| enabled | true if DSN is set | Set false in tests |
| debug | false | Logs transport failures |
| errorSampleRate | 1 | 0–1 |
| tracesSampleRate | 0.1 | Root transactions |
| sendDefaultPii | false | Include email / IP on user |
| trackHttp | true | Outbound Node HTTP |
API
SmartObserve.init · captureException · captureMessage · log · setUser · setTag · setContext · addBreadcrumb · startSpan · beforeSend · flush · close · SmartObserveClient
