# Go

> Add pulse-app.si/go and one line to main. Pulse then monitors the clients you pass it, your scheduled jobs, and your logs, and the module has no dependencies.

- Section: Packages
- Availability: From the Free plan, logs on Pro and Team. Go 1.22 or later, with no dependency.
- URL: https://pulse-app.si/en/docs/packages/go

## Install

**zsh**

```bash
go get pulse-app.si/go@latest
```

The module path really is `pulse-app.si/go`. Put the key in `PULSE_KEY`, then start the reporter at the top of `main`, with your clients in `Watch`. Go has no registry to search, so what you don’t pass isn’t checked.

**main.go**

```go
import pulse "pulse-app.si/go"

reporter := pulse.Start(pulse.Options{Watch: []any{db, cache}})
defer reporter.Stop()
```

The same line works for `net/http`, Gin, Echo, Chi, and Fiber. The framework and its version are read from the build info. The first report goes out 2 seconds later, then at the pace set by the API. Without a key, `Start` returns `nil` and warns once, and every method accepts `nil`. `Stop` waits for the current send to finish. There’s one reporter per process, and `pulse.Default()` retrieves it.

## What Pulse recognizes in Watch

| Client | What Pulse does |
| --- | --- |
| `*sql.DB` (`pq`, `pgx`, `go-sql-driver/mysql`, `sqlite3`, `modernc` sqlite, `mssql`, `go-ora`), `*gorm.DB`, `*sqlx.DB` | `PingContext`, then `SELECT 1` |
| `*pgxpool.Pool`, `*pgx.Conn` | `Ping` |
| go-redis (`*redis.Client`, `*redis.ClusterClient`, `*redis.Ring`) | `PING` |
| `*mongo.Client` | `Ping` |
| `*asynq.Inspector` | `GetQueueInfo`, shown as `File emails` |
| robfig/cron v3 `*cron.Cron`, gocron `Scheduler` | Schedule, last and next run |

Several clients of the same type are numbered: `PostgreSQL`, `PostgreSQL (2)`. Each check has 5 seconds to respond and only reads. A failing dependency, a job that crashed, or one more than 5 minutes past its scheduled run turns the app **Degraded**.

## Your own checks

`HTTPProbe` makes a `GET` without following redirects and fails at 500 and above. `Kind` is `database`, `redis`, `cache`, `queue`, `http` or `custom`. `reporter.AddProbe` adds a probe after startup.

**main.go**

```go
reporter := pulse.Start(pulse.Options{
	Probes: []pulse.Probe{
		pulse.HTTPProbe("Route /health", "http://localhost:8080/health"),
		{Name: "File invoices", Kind: "queue", Engine: "nats", Run: func(ctx context.Context) error {
			return nats.Ping(ctx)
		}},
	},
})
```

## Scheduled jobs

With robfig/cron, wrap the job with `pulse.Job` and pass the scheduler in `Watch`. Elsewhere (gocron, your own loop, a route called by a system cron), use `pulse.Track`. The error or panic is recorded, then comes back to you unchanged. `reporter.RecordCronRun(name, ok, err)` records a run by hand.

**jobs.go**

```go
scheduler.AddJob("0 * * * *", pulse.Job("renew-subscriptions", renewSubscriptions))

err := pulse.Track("renew-subscriptions", renewSubscriptions)
err = pulse.TrackContext(ctx, "export-invoices", exportInvoices)
```

## Choose what is sent

The heartbeat is always sent. The rest is set by [variables](https://pulse-app.si/en/docs/packages/variables), or by `pulse.Options`, which takes priority over them.

**main.go**

```go
pulse.Start(pulse.Options{
	Send:     []string{"checks", "crons", "logs"},
	Exclude:  []string{"File emails"},
	LogLevel: "WARN",
	Watch:    []any{db, cache},
})
```

## Logs

On Pro and Team, plug in `slog` or `log`, because Go has no clean way to do it for you. The original output keeps receiving every line.

**main.go**

```go
slog.SetDefault(slog.New(pulse.NewSlogHandler(slog.NewJSONHandler(os.Stdout, nil))))

log.SetOutput(reporter.Writer(os.Stderr))
```

With `slog`, the `component` or `logger` attribute becomes the context, the others are kept in the message, and secrets masked. The package’s own messages go through `slog.Default()` or `Options.Logger`, and aren’t sent.

**stdout**

```text
level=ERROR msg="Payment declined token=abc123" component=payments order=42
ERROR  payments  Payment declined token=*** order=42
```

## If nothing arrives

| Symptom | What to do |
| --- | --- |
| `PULSE_KEY is missing` | The variable isn’t in the binary’s environment: check the service or the container. |
| `Pulse key refused` | The key was replaced in the app: copy the new one, then restart. |
| `Pulse unreachable` | Network or firewall: your app keeps running and sending resumes automatically. |
| A dependency doesn’t appear | Pass the client in `Watch`, or add a probe. |
| No logs | Plug in `slog` or `log`. Nothing is copied without it. |

- [All variables](https://pulse-app.si/en/docs/packages/variables): Every setting and its default.
- [Exactly what is sent](https://pulse-app.si/en/docs/packages/payload): The precise format of reports and logs.
- [Live logs](https://pulse-app.si/en/docs/apps/logs): Read, filter, and follow them in the app.
