Skip to content

Commit

Permalink
Retries, timeout, version configurable per job
Browse files Browse the repository at this point in the history
This makes retries, timeout and SNMP version configurable per job. It
overrides the module's equivalent WalkParams if they're present as query
attributes.

Partially addresses: prometheus#762
  • Loading branch information
daenney committed Nov 23, 2022
1 parent 0d8c352 commit a74068d
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -84,8 +85,68 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) {
if moduleName == "" {
moduleName = "if_mib"
}

timeout := query.Get("timeout")
if len(query["timeout"]) > 1 {
http.Error(w, "'timeout' parameter must only be specified once", 400)
snmpRequestErrors.Inc()
return
}
var ptimeout *time.Duration
if timeout != "" {
p, err := time.ParseDuration(timeout)
if err != nil {
http.Error(w, "'timeout' parameter must be a valid Go duration string", 400)
snmpRequestErrors.Inc()
return
}
ptimeout = &p
}
retries := query.Get("retries")
if len(query["retries"]) > 1 {
http.Error(w, "'retries' parameter must only be specified once", 400)
snmpRequestErrors.Inc()
return
}
var pretries *int
if retries != "" {
i, err := strconv.ParseInt(retries, 0, 64)
if err != nil {
http.Error(w, "'retries' parameter must be an integer", 400)
snmpRequestErrors.Inc()
return
}
ii := int(i)
pretries = &ii
}
version := query.Get("version")
if len(query["version"]) > 1 {
http.Error(w, "'version' parameter must only be specified once", 400)
snmpRequestErrors.Inc()
return
}
var pversion *int
if version != "" {
i, err := strconv.ParseInt(version, 0, 64)
if err != nil {
http.Error(w, "'version' parameter must be an integer", 400)
snmpRequestErrors.Inc()
return
}
ii := int(i)
pversion = &ii
}
sc.RLock()
module, ok := (*(sc.C))[moduleName]
if ptimeout != nil {
module.WalkParams.Timeout = *ptimeout
}
if pretries != nil {
module.WalkParams.Retries = pretries
}
if pversion != nil {
module.WalkParams.Version = *pversion
}
sc.RUnlock()
if !ok {
http.Error(w, fmt.Sprintf("Unknown module '%s'", moduleName), 400)
Expand Down

0 comments on commit a74068d

Please sign in to comment.