Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## Project Structure & Module Organization

`cmd/pg_noty/` builds the CLI. Under `internal/`, `cli` composes commands, `config` validates YAML, `schema` owns storage, `source` manages triggers and queues, `reconcile` plans database changes, `delivery` sends webhooks, and `goartifact` supports source checks. Tests sit beside their packages; fixtures and golden files belong in each package's `testdata/`. Use `demo/` and `docker-compose.yml` for the runnable example. Design records live in `.specs/`.
`cmd/pg_noty/` builds the CLI. Under `internal/`, `cli` composes commands, `config` validates YAML, `schema` owns storage, `source` manages triggers and queues, `reconcile` plans database changes, and `delivery` sends webhooks. Tests sit beside their packages; fixtures and golden files belong in each package's `testdata/`. Use `demo/` and `docker-compose.yml` for the runnable example. Design records live in `.specs/`.

## Build, Test, and Development Commands

Expand Down
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ It starts PostgreSQL seeded from `demo/seed.sql`, an echo receiver, and `pg_noty
| `internal/source/` | Trigger DDL generation and the queue. Implements `EventSource`. |
| `internal/reconcile/` | Plans and applies database changes; ownership rules. |
| `internal/delivery/` | The webhook worker: signing, retries, HTTP boundary, SSRF guard. |
| `internal/goartifact/` | Shared source-measurement helpers used by tests. |
| `demo/` | Runnable example. |

Dependencies point inward, and each package's `doc.go` states its boundary. Only `internal/cli`
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ your configuration at that path is all it takes.
```sh
docker run --rm \
-e DATABASE_URL -e ORDER_WEBHOOK_URL -e WEBHOOK_SECRET \
-e SENTRY_DSN -e SENTRY_ENVIRONMENT -e SENTRY_RELEASE \
-v "$PWD/listeners.yaml:/etc/pg_noty/listeners.yaml:ro" \
-p 8080:8080 \
cpoul/pg_noty:latest
Expand All @@ -199,6 +200,24 @@ docker run --rm -e DATABASE_URL \
The image is `gcr.io/distroless/static-debian12:nonroot` with a statically linked binary and stays
under 10 MiB per platform. It contains no shell, package manager or compiler.

Set `SENTRY_DSN` in the process environment to report command failures, error-level logs
(including background worker errors), and panics on the command goroutine to Sentry. Leave it
unset or blank to disable Sentry. `SENTRY_ENVIRONMENT` and `SENTRY_RELEASE` optionally label events,
following the [Sentry Go SDK's environment configuration](https://github.com/getsentry/sentry-go/tree/v0.48.0#configuration).
Keep the DSN in your deployment's environment or secret store rather than tracked YAML.

```sh
SENTRY_DSN='https://public-key@sentry.example.com/1' \
SENTRY_ENVIRONMENT=production \
pg_noty run -f listeners.yaml
```

Error events include stack traces and the same redacted attributes used by local logs. Normal
logging continues unchanged; debug, info, and warning logs are not sent. Performance tracing,
Sentry's separate logs and metrics products are disabled. Shutdown allows up to two seconds to
flush pending events. An invalid DSN stops startup with `error: SENTRY_DSN is invalid` without
printing its value. Panic reporting preserves the crash; panics in other goroutines are not captured.

### Who changes your database

By default `pg_noty run` **changes nothing**. It creates no schema, applies no migration and
Expand Down
32 changes: 30 additions & 2 deletions cmd/pg_noty/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,43 @@ package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/cpouldev/pg_noty/internal/cli"
"github.com/getsentry/sentry-go"
)

func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
os.Exit(cli.Execute(ctx, os.Args[1:], cli.Streams{In: os.Stdin, Out: os.Stdout, Err: os.Stderr}))
status := run(
ctx,
os.Args[1:],
cli.Streams{In: os.Stdin, Out: os.Stdout, Err: os.Stderr},
os.Getenv("SENTRY_DSN"),
nil,
)
stop()
os.Exit(status)
}

func run(ctx context.Context, args []string, streams cli.Streams, dsn string, transport sentry.Transport) int {
monitoring, err := newSentryRuntime(dsn, transport)
if err != nil {
_, _ = fmt.Fprintln(streams.Err, "error: "+err.Error())
return 1
}
if monitoring == nil {
return cli.Execute(ctx, args, streams)
}
if ctx == nil {
ctx = context.Background()
}
ctx = monitoring.context(ctx)
// Defers unwind in reverse: capture a panic first, then flush it before os.Exit terminates.
defer monitoring.shutdown()
defer monitoring.recoverPanic(ctx)
return cli.Execute(ctx, args, streams)
}
66 changes: 66 additions & 0 deletions cmd/pg_noty/sentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"context"
"errors"
"strings"
"time"

"github.com/getsentry/sentry-go"
)

const sentryShutdownTimeout = 2 * time.Second

var errInvalidSentryDSN = errors.New("SENTRY_DSN is invalid")

type sentryRuntime struct {
client *sentry.Client
hub *sentry.Hub
}

func newSentryRuntime(dsn string, transport sentry.Transport) (*sentryRuntime, error) {
if strings.TrimSpace(dsn) == "" {
return nil, nil
}
if transport == nil {
// The SDK's default telemetry processor flushes again during Close.
// Use the asynchronous HTTP transport directly to keep shutdown bounded.
transport = sentry.NewHTTPTransport()
}
client, err := sentry.NewClient(sentry.ClientOptions{
Dsn: dsn,
AttachStacktrace: true,
DisableLogs: true,
DisableMetrics: true,
EnableTracing: false,
Transport: transport,
})
if err != nil {
return nil, errInvalidSentryDSN
}
scope := sentry.NewScope()
scope.SetTag("component", "cli")
return &sentryRuntime{client: client, hub: sentry.NewHub(client, scope)}, nil
}

func (runtime *sentryRuntime) context(ctx context.Context) context.Context {
return sentry.SetHubOnContext(ctx, runtime.hub)
}

func (runtime *sentryRuntime) recoverPanic(ctx context.Context) {
recovered := recover()
if recovered == nil {
return
}
runtime.hub.RecoverWithContext(ctx, recovered)
// Reporting must not turn a process crash into apparent success.
panic(recovered)
}

func (runtime *sentryRuntime) shutdown() {
// A signal normally cancels the command context, so flushing needs an independent bound.
ctx, cancel := context.WithTimeout(context.Background(), sentryShutdownTimeout)
defer cancel()
runtime.hub.FlushWithContext(ctx)
runtime.client.Close()
}
Loading
Loading