# Monitoring in MakerKit

> How monitoring works in this codebase.

*Canonical: https://makerkit.dev/docs/nextjs-prisma/monitoring/overview*

---

This repo ships a small monitoring abstraction with built-in Sentry support and a console fallback.

## What Exists

- Provider selection in `packages/monitoring/api/src/get-monitoring-provider.ts`
- Client provider wrapper in `packages/monitoring/api/src/components/provider.tsx`
- Server accessor in `packages/monitoring/api/src/server.ts`
- Client instrumentation entry in `packages/monitoring/api/src/instrumentation-client.ts` — owns `window` error handlers, buffers errors until the provider chunk loads
- Server instrumentation entry in `packages/monitoring/api/src/instrumentation.ts` — exposes `register()` and `onRequestError`
- Shared contract in `packages/monitoring/core/src/monitoring.service.ts`
- Console fallback in `packages/monitoring/core/src/console-monitoring.service.ts`
- Sentry implementation in `packages/monitoring/sentry`

## Supported Providers

Current runtime support:

- `sentry`
- empty value for console fallback

The docs for PostHog, Honeybadger, and SigNoz are informational only: those providers are not built into the repo.

## Client Usage

Use the client hooks from `@kit/monitoring/hooks`:

- `useMonitoring()`
- `useCaptureException(error)` — pass `null` to skip (e.g. when `error.digest` is set, meaning the error already fired server-side and was captured by `onRequestError`)

Unhandled `window.onerror` / `unhandledrejection` events are captured automatically by `packages/monitoring/api/src/instrumentation-client.ts`, wired in `apps/web/instrumentation-client.ts`. The handlers are provider-agnostic — they buffer errors and replay them once the provider chunk has finished loading.

For places where the React provider tree isn't available (e.g. a future `global-error.tsx`), use `captureClientException` from `@kit/monitoring/instrumentation-client`.

## Server Usage

Use `getServerMonitoringService()` from `@kit/monitoring/server` to capture exceptions or custom events from server code.

Request-lifecycle errors are forwarded automatically through `onRequestError` in `packages/monitoring/api/src/instrumentation.ts`, wired in `apps/web/instrumentation.ts`.

## Enable Sentry

Set:

```bash
NEXT_PUBLIC_MONITORING_PROVIDER=sentry
NEXT_PUBLIC_SENTRY_DSN=https://your-dsn@sentry.io/project-id
```

For the Sentry-specific setup in this repo, see [Sentry](./sentry).
