instrumentation.go 1.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
package tabacco

import (
	"context"
	"time"

	"github.com/prometheus/client_golang/prometheus"

	"git.autistici.org/ale/tabacco/jobs"
)

var (
	backupSuccesses = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "backup_ok",
			Help: "Did the backup succeed.",
		},
		[]string{"dataset"},
	)
	backupElapsed = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "backup_elapsed",
			Help: "Time taken by the last backup.",
		},
		[]string{"dataset"},
	)
	backupLastRun = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "backup_last_run_timestamp",
			Help: "Timestamp of the last backup (succeeded or not).",
		},
		[]string{"dataset"},
	)
	backupLastOk = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "backup_last_ok_timestamp",
			Help: "Timestamp of the last successful backup.",
		},
		[]string{"dataset"},
	)
)

func init() {
	prometheus.MustRegister(backupSuccesses, backupElapsed, backupLastRun, backupLastOk)
}

// Wrap a job with prometheus success/lastRun instrumentation based on
// the exit status of the job.
func withInstrumentation(j jobs.Job, dataset string) jobs.Job {
	return jobs.JobFunc(func(ctx context.Context) error {
		start := time.Now()

		err := j.RunContext(ctx)

		l := prometheus.Labels{"dataset": dataset}
		elapsed := time.Since(start)
		backupElapsed.With(l).Set(elapsed.Seconds())
		backupLastRun.With(l).Set(float64(start.Unix()))
		var statusValue float64
		if err == nil {
			backupLastOk.With(l).Set(float64(start.Unix()))
			statusValue = 1
		}
		backupSuccesses.With(l).Set(statusValue)

		return err
	})
}