Skip to content

Commit

Permalink
prometheus: simplify snapshot visualization
Browse files Browse the repository at this point in the history
Make it *really easy* to look at `prometheus-snapshot.tgz` by putting a
shell script next to it that spins it up via docker, so you can just
run:

```
$ ./prometheus-docker-run.sh
[...]
ts=2022-02-09T09:07:06.254Z caller=main.go:896 level=info msg="Server is ready to receive web requests."
```

I got motivated to do this while working on the roachtest in cockroachdb#76147 but
I imagine it'll come in handy in general as well.

Release note: None
  • Loading branch information
tbg committed Feb 9, 2022
1 parent 03c50b0 commit 66ac06f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
35 changes: 30 additions & 5 deletions pkg/cmd/roachtest/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"time"

"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/option"
Expand Down Expand Up @@ -118,10 +119,9 @@ sudo systemd-run --unit prometheus --same-dir \
return &Prometheus{Config: cfg}, nil
}

// Snapshot takes a snapshot of prometheus and stores the snapshot in the given localPath
func (pm *Prometheus) Snapshot(
ctx context.Context, c Cluster, l *logger.Logger, localPath string,
) error {
// Snapshot takes a snapshot of prometheus and stores the snapshot and a script to spin up
// a docker instance for it to the given directory.
func (pm *Prometheus) Snapshot(ctx context.Context, c Cluster, l *logger.Logger, dir string) error {
if err := c.RunE(
ctx,
pm.PrometheusNode,
Expand All @@ -130,12 +130,37 @@ func (pm *Prometheus) Snapshot(
); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(dir, "prometheus-docker-run.sh"), []byte(`#!/bin/sh
set -eu
tar xf prometheus-snapshot.tar.gz
snapdir=$(find data/snapshots -mindepth 1 -maxdepth 1 -type d)
pyml=$(mktemp)
chmod -R o+rw "${snapdir}" "${pyml}"
cat <<EOF > "${pyml}"
global:
scrape_interval: 10s
scrape_timeout: 5s
EOF
set -x
# Worked as of v2.33.1 so you can hard-code that if necessary.
docker run --privileged -p 9090:9090 \
-v "${pyml}:/etc/prometheus/prometheus.yml" -v "${PWD}/${snapdir}:/prometheus" \
prom/prometheus:latest \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus \
--web.enable-admin-api
`), 0755); err != nil {
return err
}

return c.Get(
ctx,
l,
"/tmp/prometheus/prometheus-snapshot.tar.gz",
localPath,
dir,
pm.PrometheusNode,
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/roachtest/tests/tpcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ func setupPrometheus(
snapshotCtx,
c,
t.L(),
filepath.Join(t.ArtifactsDir(), "prometheus-snapshot.tar.gz"),
t.ArtifactsDir(),
); err != nil {
t.L().Printf("failed to get prometheus snapshot: %v", err)
}
Expand Down

0 comments on commit 66ac06f

Please sign in to comment.