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
34 changes: 0 additions & 34 deletions .github/workflows/grafana-alertcheck-release.yml

This file was deleted.

33 changes: 0 additions & 33 deletions grafana-alertcheck/.goreleaser.yaml

This file was deleted.

46 changes: 43 additions & 3 deletions grafana-alertcheck/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
# grafana-alertcheck

A CD quality gate for Grafana alerts: bookend a release with `watch` (record) and `check` (classify) to
answer whether any watched alert was in a bad state during the release window.
A CD quality gate for Grafana alerts. It bookends a release with two commands — `watch` (record) and
`check` (classify) — and answers whether any watched alert was in a bad state during the release window.

Under construction.
```
watch → your work → check
```

`watch` starts a background recorder that polls each named alert into a JSONL log. After the work emits a
`from`/`to` pair, `check` proves continuous coverage of that window, classifies each alert's state
timeline, and exits `0`, `1`, or `2`.

It **fails closed**: if it cannot get an answer, it stops the release — never a pass on an unproven window.

## Quickstart

```bash
export GRAFANA_URL=https://grafana.example.com
export GRAFANA_TOKEN=…

grafana-alertcheck watch --out /tmp/run.jsonl --alerts alerts.txt
./deploy.sh # emits deployed_at=<RFC3339>
./verify.sh # emits finished_at=<RFC3339>
grafana-alertcheck check --in /tmp/run.jsonl --from "$deployed_at" --to "$finished_at"
```

Requires Grafana >= 13.0.0 and < 14.0.0. Connection details come from the environment only — the token is
never a flag.

## Documentation

| Doc | Covers |
| --- | ------ |
| [`docs/index.md`](./docs/index.md) | Overview, quickstarts, exit codes, common surprises |
| [`docs/how-alerts-are-evaluated.md`](./docs/how-alerts-are-evaluated.md) | Verdict model, coverage proof, health/liveness |
| [`docs/advanced.md`](./docs/advanced.md) | Check budget, scheduling, why history isn't queried |
| [`docs/architecture.md`](./docs/architecture.md) | Design invariants, the pure-function seam, recorder lifecycle |
| [`docs/reference/cli.md`](./docs/reference/cli.md) | Full CLI reference — subcommands, flags, naming |
| [`docs/reference/log-format.md`](./docs/reference/log-format.md) | The JSONL log schema, for debugging artifacts |

## Build

```bash
go build ./... && go test ./...
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -40,6 +41,13 @@ func runCheck(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
output := fs.String("output", "", `"json" writes the machine-readable Result to stdout in addition to the table; default is the table alone`)

if err := fs.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 2
}
if fs.NArg() != 0 {
fmt.Fprintf(stderr, "check: unexpected arguments %v\n", fs.Args())
return 2
}
if *output != "" && *output != "json" {
Expand Down Expand Up @@ -82,7 +90,7 @@ func runCheck(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
PidFile: *pidfile,
Concurrency: *common.concurrency,
Clock: gate.SystemClock{},
Notes: stderr,
Notes: newNoteStyler(stderr),
}
if *to == "" {
fmt.Fprintln(stderr, "check: --to is required")
Expand Down Expand Up @@ -113,11 +121,10 @@ func runCheck(args []string, stdin io.Reader, stdout, stderr io.Writer) int {

result, checkErr := gate.Check(ctx, cfg)

if err := renderTable(stderr, result); err != nil {
fmt.Fprintln(stderr, err)
}
if checkErr != nil {
fmt.Fprintln(stderr, checkErr)
} else if err := renderTable(stderr, result); err != nil {
fmt.Fprintln(stderr, err)
}
if *output == "json" {
enc := json.NewEncoder(stdout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"errors"
"os"
"strings"
"testing"

"github.com/smartcontractkit/chainlink-testing-framework/grafana-alertcheck/internal/gate"
"github.com/stretchr/testify/require"
)

// The exit-code mapping, pinned directly against exitCode with no network
Expand All @@ -27,19 +27,15 @@ func TestExitCode(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := exitCode(tt.res, tt.err); got != tt.want {
t.Fatalf("exitCode(...) = %d, want %d", got, tt.want)
}
require.Equal(t, tt.want, exitCode(tt.res, tt.err))
})
}
}

func writeTempAlerts(t *testing.T) string {
t.Helper()
path := t.TempDir() + "/alerts.txt"
if err := os.WriteFile(path, []byte("Some Alert\n"), 0o644); err != nil {
t.Fatal(err)
}
require.NoError(t, os.WriteFile(path, []byte("Some Alert\n"), 0o644))
return path
}

Expand Down Expand Up @@ -100,12 +96,8 @@ func TestRunCheck_FlagValidation(t *testing.T) {
var stdout, stderr bytes.Buffer
args := append([]string{"check"}, tt.args(t)...)
code := run(args, &stdout, &stderr)
if code != 2 {
t.Fatalf("code = %d, want 2; stderr = %q", code, stderr.String())
}
if !strings.Contains(stderr.String(), tt.wantErr) {
t.Fatalf("stderr = %q, want it to contain %q", stderr.String(), tt.wantErr)
}
require.Equal(t, 2, code)
require.Contains(t, stderr.String(), tt.wantErr)
})
}
}
Expand All @@ -121,12 +113,8 @@ func TestRunCheck_ToInPastNoLog(t *testing.T) {
"--from", "1999-01-01T00:00:00Z", "--to", "2000-01-01T00:00:00Z",
"--alerts", writeTempAlerts(t),
}, &stdout, &stderr)
if code != 2 {
t.Fatalf("code = %d, want 2; stderr = %q", code, stderr.String())
}
if !strings.Contains(stderr.String(), "already passed") {
t.Fatalf("stderr = %q, want the past-`to` refusal", stderr.String())
}
require.Equal(t, 2, code)
require.Contains(t, stderr.String(), "already passed")
}

// --output json never writes to stdout when Check was never reached, because
Expand All @@ -137,10 +125,6 @@ func TestRunCheck_NoResultOnConfigError(t *testing.T) {
t.Setenv("GRAFANA_TOKEN", "")
var stdout, stderr bytes.Buffer
code := run([]string{"check", "--to", "2026-01-01T00:00:00Z", "--output", "json"}, &stdout, &stderr)
if code != 2 {
t.Fatalf("code = %d, want 2", code)
}
if stdout.Len() != 0 {
t.Fatalf("stdout = %q, want empty", stdout.String())
}
require.Equal(t, 2, code)
require.Empty(t, stdout.String())
}
60 changes: 0 additions & 60 deletions grafana-alertcheck/cmd/grafana-alertcheck/main_test.go

This file was deleted.

29 changes: 0 additions & 29 deletions grafana-alertcheck/cmd/grafana-alertcheck/version.go

This file was deleted.

32 changes: 0 additions & 32 deletions grafana-alertcheck/cmd/grafana-alertcheck/version_test.go

This file was deleted.

Loading
Loading