Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 56ef7b2

Browse files
committedAug 17, 2023
fixed review
1 parent 232ed18 commit 56ef7b2

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed
 

‎collector/collector.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var (
4444
wrapCounters = kingpin.Flag("snmp.wrap-large-counters", "Wrap 64-bit counters to avoid floating point rounding.").Default("true").Bool()
4545
srcAddress = kingpin.Flag("snmp.source-address", "Source address to send snmp from in the format 'address:port' to use when connecting targets. If the port parameter is empty or '0', as in '127.0.0.1:' or '[::1]:0', a source port number is automatically (random) chosen.").Default("").String()
4646

47-
SnmpDuration = promauto.NewHistogramVec(
47+
snmpDuration = promauto.NewHistogramVec(
4848
prometheus.HistogramOpts{
4949
Name: "snmp_collection_duration_seconds",
5050
Help: "Duration of collections by the SNMP exporter",
@@ -89,6 +89,10 @@ func listToOid(l []int) string {
8989
return strings.Join(result, ".")
9090
}
9191

92+
func InitModuleMetrics(auth, module string) {
93+
snmpDuration.WithLabelValues(auth, module)
94+
}
95+
9296
type ScrapeResults struct {
9397
pdus []gosnmp.SnmpPDU
9498
packets uint64
@@ -514,7 +518,7 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
514518
c.collect(ch, m)
515519
duration := time.Since(start).Seconds()
516520
level.Debug(logger).Log("msg", "Finished scrape", "duration_seconds", duration)
517-
SnmpDuration.WithLabelValues(c.authName, m.name).Observe(duration)
521+
snmpDuration.WithLabelValues(c.authName, m.name).Observe(duration)
518522
}
519523
}()
520524
}

‎main.go

+24-17
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) {
9292
if len(queryModule) == 0 {
9393
queryModule = append(queryModule, "if_mib")
9494
}
95+
uniqueM := make(map[string]bool)
96+
var modules []string
97+
for _, qm := range queryModule {
98+
for _, m := range strings.Split(qm, ",") {
99+
if m == "" {
100+
continue
101+
}
102+
if _, ok := uniqueM[m]; !ok {
103+
uniqueM[m] = true
104+
modules = append(modules, m)
105+
}
106+
}
107+
}
95108
sc.RLock()
96109
auth, authOk := sc.C.Auths[authName]
97110
if !authOk {
@@ -100,27 +113,21 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) {
100113
snmpRequestErrors.Inc()
101114
return
102115
}
103-
uniqueM := make(map[string]bool)
104-
var modules []*collector.NamedModule
105-
for _, qm := range queryModule {
106-
for _, m := range strings.Split(qm, ",") {
107-
module, moduleOk := sc.C.Modules[m]
108-
if !moduleOk {
109-
sc.RUnlock()
110-
http.Error(w, fmt.Sprintf("Unknown module '%s'", m), http.StatusBadRequest)
111-
snmpRequestErrors.Inc()
112-
return
113-
}
114-
if _, ok := uniqueM[m]; !ok {
115-
uniqueM[m] = true
116-
modules = append(modules, collector.NewNamedModule(m, module))
117-
}
116+
var nmodules []*collector.NamedModule
117+
for _, m := range modules {
118+
module, moduleOk := sc.C.Modules[m]
119+
if !moduleOk {
120+
sc.RUnlock()
121+
http.Error(w, fmt.Sprintf("Unknown module '%s'", m), http.StatusBadRequest)
122+
snmpRequestErrors.Inc()
123+
return
118124
}
125+
nmodules = append(nmodules, collector.NewNamedModule(m, module))
119126
}
120127
sc.RUnlock()
121128
logger = log.With(logger, "auth", authName, "target", target)
122129
registry := prometheus.NewRegistry()
123-
c := collector.New(r.Context(), target, authName, auth, modules, logger, registry, *concurrency)
130+
c := collector.New(r.Context(), target, authName, auth, nmodules, logger, registry, *concurrency)
124131
registry.MustRegister(c)
125132
// Delegate http serving to Prometheus client library, which will call collector.Collect.
126133
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
@@ -155,7 +162,7 @@ func (sc *SafeConfig) ReloadConfig(configFile string) (err error) {
155162
// Initialize metrics.
156163
for auth := range sc.C.Auths {
157164
for module := range sc.C.Modules {
158-
collector.SnmpDuration.WithLabelValues(auth, module)
165+
collector.InitModuleMetrics(auth, module)
159166
}
160167
}
161168
sc.Unlock()

0 commit comments

Comments
 (0)
Please sign in to comment.