Skip to content

Commit c00fc9a

Browse files
authored
New option to specify Prometheus Pushgateway job name (#193)
1 parent 7f8554f commit c00fc9a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

config/profile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type Profile struct {
8989
StatusFile string `mapstructure:"status-file" description:"Path to the status file to update with a summary of last restic command result"`
9090
PrometheusSaveToFile string `mapstructure:"prometheus-save-to-file" description:"Path to the prometheus metrics file to update with a summary of the last restic command result"`
9191
PrometheusPush string `mapstructure:"prometheus-push" format:"uri" description:"URL of the prometheus push gateway to send the summary of the last restic command result to"`
92+
PrometheusPushJob string `mapstructure:"prometheus-push-job" description:"Prometheus push gateway job name. $command placeholder is replaced with restic command"`
9293
PrometheusLabels map[string]string `mapstructure:"prometheus-labels" description:"Additional prometheus labels to set"`
9394
Environment map[string]ConfidentialValue `mapstructure:"env" description:"Additional environment variables to set in any child process"`
9495
Init *InitSection `mapstructure:"init"`

docs/content/status/prometheus/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ resticprofile_build_info{goversion="go1.19",version="0.19.0"} 1
129129
130130
```
131131

132+
## Prometheus Pushgateway
133+
134+
Prometheus Pushgateway uses the job label as a grouping key. All metrics with the same grouping key get replaced when pushed. To prevent metrics from multiple profiles getting overwritten by each other, the default job label is set to `<profile_name>.<command>` (e.g. `root.backup`).
135+
136+
If you need more control over the job label, you can use the `prometheus-push-job` property. This property can contain the `$command` placeholder, which is replaced with the name of the executed command.
137+
132138
## User defined labels
133139

134140
You can add your own prometheus labels. Please note they will be applied to **all** the metrics.

monitor/prom/progress.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package prom
22

33
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
48
"github.com/creativeprojects/clog"
59
"github.com/creativeprojects/resticprofile/config"
610
"github.com/creativeprojects/resticprofile/constants"
@@ -55,7 +59,17 @@ func (p *Progress) Summary(command string, summary monitor.Summary, stderr strin
5559
}
5660
}
5761
if p.profile.PrometheusPush != "" {
58-
err := p.metrics.Push(p.profile.PrometheusPush, command)
62+
jobName := p.profile.PrometheusPushJob
63+
if jobName == "" {
64+
jobName = fmt.Sprintf("%s.%s", p.profile.Name, command)
65+
}
66+
jobName = os.Expand(jobName, func(name string) string {
67+
if strings.EqualFold(name, "command") {
68+
return command
69+
}
70+
return ""
71+
})
72+
err := p.metrics.Push(p.profile.PrometheusPush, jobName)
5973
if err != nil {
6074
// not important enough to throw an error here
6175
clog.Warningf("pushing prometheus metrics to %q: %v", p.profile.PrometheusPush, err)

0 commit comments

Comments
 (0)