@@ -6,20 +6,25 @@ import (
66 "net/http"
77 "os"
88 "os/exec"
9+ "path/filepath"
910 "runtime/debug"
11+ "strconv"
1012 "strings"
1113 "syscall"
1214 "time"
15+
16+ "github.com/meysam81/prometheus-command-timer/counter"
1317)
1418
1519type Config struct {
16- PushgatewayURL string
17- JobName string
18- InstanceName string
19- Labels string
20- Debug bool
21- Version bool
22- Info bool
20+ PushgatewayURL string
21+ JobName string
22+ InstanceName string
23+ Labels string
24+ ExecCountTransientStore string
25+ Debug bool
26+ Version bool
27+ Info bool
2328}
2429
2530func main () {
@@ -32,6 +37,7 @@ func main() {
3237 flag .StringVar (& config .JobName , "job-name" , "" , "Job name for metrics (required)" )
3338 flag .StringVar (& config .InstanceName , "instance-name" , config .InstanceName , "Instance name for metrics" )
3439 flag .StringVar (& config .Labels , "labels" , "" , "Additional labels in key=value format, comma-separated (e.g., env=prod,team=infra)" )
40+ flag .StringVar (& config .ExecCountTransientStore , "execution-count-store" , filepath .Join (os .TempDir (), "prometheus-command-time.json" ), "Override the default transient store filename (<tmp>/prometheus-command-time.json)" )
3541 flag .BoolVar (& config .Version , "version" , false , "Output version" )
3642 showHelp := flag .Bool ("help" , false , "Show help message" )
3743 flag .BoolVar (showHelp , "h" , false , "Show help message (shorthand)" )
@@ -133,11 +139,27 @@ func executeCommand(config *Config, cmdArgs []string) int {
133139 sendMetric (config , "job_duration_seconds" , fmt .Sprintf ("%.6f" , duration ), "gauge" , "Total time taken for job execution in seconds" )
134140 sendMetric (config , "job_exit_status" , fmt .Sprintf ("%d" , exitStatus ), "gauge" , "Exit status code of the last job execution (0=success)" )
135141 sendMetric (config , "job_last_execution_timestamp" , fmt .Sprintf ("%d" , endTime ), "gauge" , "Timestamp of the last job execution" )
136- sendMetric (config , "job_executions_total" , "1" , "counter" , "Total number of job executions" )
142+ sendMetric (config , "job_executions_total" , strconv . Itoa ( incrementExecutionCounter ( config )) , "counter" , "Total number of job executions" )
137143
138144 return exitStatus
139145}
140146
147+ // incrementExecutionCounter will return the next value of a counter which
148+ // is named using the push gateway URL.
149+ func incrementExecutionCounter (config * Config ) int {
150+ counterVal := 1
151+ counterName , err := buildPushgatewayURL (config )
152+ if err != nil {
153+ logStdout (config , "error building counter name: %v" , err )
154+ } else {
155+ counterVal , err = counter .IncrementNamedCounter (counterName , 1 , config .ExecCountTransientStore )
156+ if err != nil {
157+ logStdout (config , "error loading counter: %v" , err )
158+ }
159+ }
160+ return counterVal
161+ }
162+
141163func buildPushgatewayURL (config * Config ) (string , error ) {
142164 url := fmt .Sprintf ("%s/metrics/job/%s/instance/%s" ,
143165 strings .TrimSuffix (config .PushgatewayURL , "/" ),
0 commit comments