# Monitor your application with Nightwatch

> Add lightweight monitoring and observability to your Laravel or Statamic application.

## Monitor your application with Nightwatch

Nightwatch adds lightweight monitoring and observability to your Laravel or
Statamic application. It watches your application as it runs and reports the
details back to your Nightwatch account, so you can see slow queries, errors,
requests and queue jobs in one place.

It is a **Custom services** entry you add alongside your application. As a sidecar
it shares your application's lifecycle: it starts and stops with it, and there is
nothing separate to manage or restart.

### Adding Nightwatch

1. Go to your application and add a new service.
2. Pick **Nightwatch**.
3. Enter your **NIGHTWATCH_TOKEN**. This is the authentication token for the
   Nightwatch API, and you get it from your Nightwatch account for the project
   you want to send data to. It is required, so you will need it before you can
   create the service.
4. Create the service.

That is all it takes. During the next deployment the sidecar starts, the required
environment variables are injected into your main application, and Nightwatch
begins reporting.

### What it monitors

Nightwatch captures the usual sources of trouble in a running application:

- **Database queries** and how long they take
- **Exceptions** as they are thrown
- **HTTP requests**, so you can spot slow endpoints and high traffic
- **Queue jobs**, including failures and durations

The token you entered is used to authenticate with the Nightwatch API, and the
data lands in your Nightwatch project.

### Configuring it

The token is shown directly on the create form. The rest of the settings live
under **Advanced settings** in either the create modal or, after the service
exists, **Edit resources** > **Service configuration**. None of them are
required, and each has a sensible default.

- **NIGHTWATCH_SAMPLE_RATE** is the percentage of requests to sample. Default
  is `100`; anywhere from `1` to `100`. Lower it on busy applications if you
  want to cut the volume of data reported.
- **NIGHTWATCH_LOG_LEVEL** is the log level Nightwatch reports at, one of
  `debug`, `info`, `warning` or `error`. Default is `info`.
- **NIGHTWATCH_CAPTURE_QUERIES** captures database queries. Default is `true`.
- **NIGHTWATCH_CAPTURE_EXCEPTIONS** captures and reports exceptions. Default is
  `true`.
- **NIGHTWATCH_CAPTURE_REQUESTS** captures HTTP request details. Default is
  `true`.
- **NIGHTWATCH_CAPTURE_JOBS** monitors queue jobs. Default is `true`.

Set any of the capture flags to `false` to stop Nightwatch collecting that
kind of data.

### About resources and lifecycle

Because Nightwatch is a sidecar, its resource usage is fixed and modest, so the
memory and instance controls are not shown. It runs the Nightwatch agent image, a
lightweight companion to your application, so there is nothing to restart and no
separate debug access. Its logs are written to the same place as your
application's, so look there if you need them.

### Environment variables

On deployment, Nightwatch injects the following variables into your main
application so the agent picks up your configuration:

- **NIGHTWATCH_ENABLED** is set to `true`.
- **NIGHTWATCH_TOKEN** is your token (from the primary setting).
- **NIGHTWATCH_SAMPLE_RATE**, **NIGHTWATCH_LOG_LEVEL** and the four
  **NIGHTWATCH_CAPTURE_*** flags reflect whatever you set in the panel, using
  their defaults when you left them alone.

You only see this service for Laravel and Statamic applications, which is where
the agent works.
