Repository: prometheus/snmp_exporter
Pull request #616 (2021-02-21 17:24:17)

Split collector into separate package
Make it easier to embed the snmp_exporter into other systems.
* Update metric registration to use promauto.
* Update minimum version to Go 1.14. (required for exporter-toolkit)
* Update snmp.yml with latest mibs.

Re-implementation of https://github.com/prometheus/snmp_exporter/pull/456

Signed-off-by: Ben Kochie <superq@gmail.com>


Patch:
diff --git a/collector.go b/collector/collector.go
similarity index 98%
rename from collector.go
rename to collector/collector.go
index e938f5e..2b4dd7c 100644
--- a/collector.go
+++ b/collector/collector.go
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package collector
 
 import (
 	"context"
@@ -26,13 +26,14 @@ import (
 	"github.com/go-kit/kit/log/level"
 	"github.com/gosnmp/gosnmp"
 	"github.com/prometheus/client_golang/prometheus"
+	"github.com/prometheus/client_golang/prometheus/promauto"
 	"gopkg.in/alecthomas/kingpin.v2"
 
 	"github.com/prometheus/snmp_exporter/config"
 )
 
 var (
-	snmpUnexpectedPduType = prometheus.NewCounter(
+	snmpUnexpectedPduType = promauto.NewCounter(
 		prometheus.CounterOpts{
 			Name: "snmp_unexpected_pdu_type_total",
 			Help: "Unexpected Go types in a PDU.",
@@ -43,10 +44,6 @@ var (
 	wrapCounters           = kingpin.Flag("snmp.wrap-large-counters", "Wrap 64-bit counters to avoid floating point rounding.").Default("true").Bool()
 )
 
-func init() {
-	prometheus.MustRegister(snmpUnexpectedPduType)
-}
-
 // Types preceded by an enum with their actual type.
 var combinedTypeMapping = map[string]map[int]string{
 	"InetAddress": {
@@ -211,6 +208,10 @@ type collector struct {
 	logger log.Logger
 }
 
+func New(target string, module *config.Module, logger log.Logger) *collector {
+	return &collector{target: target, module: module, logger: logger}
+}
+
 // Describe implements Prometheus.Collector.
 func (c collector) Describe(ch chan<- *prometheus.Desc) {
 	ch <- prometheus.NewDesc("dummy", "dummy", nil, nil)
diff --git a/go.mod b/go.mod
index 8c190d7..64a7bc1 100644
--- a/go.mod
+++ b/go.mod
@@ -11,4 +11,4 @@ require (
 	gopkg.in/yaml.v2 v2.4.0
 )
 
-go 1.13
+go 1.14
diff --git a/main.go b/main.go
index e9a277c..2466b57 100644
--- a/main.go
+++ b/main.go
@@ -26,6 +26,7 @@ import (
 	"github.com/go-kit/kit/log"
 	"github.com/go-kit/kit/log/level"
 	"github.com/prometheus/client_golang/prometheus"
+	"github.com/prometheus/client_golang/prometheus/promauto"
 	"github.com/prometheus/client_golang/prometheus/promhttp"
 	"github.com/prometheus/common/promlog"
 	"github.com/prometheus/common/promlog/flag"
@@ -35,6 +36,7 @@ import (
 	"gopkg.in/alecthomas/kingpin.v2"
 	yaml "gopkg.in/yaml.v2"
 
+	"github.com/prometheus/snmp_exporter/collector"
 	"github.com/prometheus/snmp_exporter/config"
 )
 
@@ -45,14 +47,14 @@ var (
 	dryRun        = kingpin.Flag("dry-run", "Only verify configuration is valid and exit.").Default("false").Bool()
 
 	// Metrics about the SNMP exporter itself.
-	snmpDuration = prometheus.NewSummaryVec(
+	snmpDuration = promauto.NewSummaryVec(
 		prometheus.SummaryOpts{
 			Name: "snmp_collection_duration_seconds",
 			Help: "Duration of collections by the SNMP exporter",
 		},
 		[]string{"module"},
 	)
-	snmpRequestErrors = prometheus.NewCounter(
+	snmpRequestErrors = promauto.NewCounter(
 		prometheus.CounterOpts{
 			Name: "snmp_request_errors_total",
 			Help: "Errors in requests to the SNMP exporter",
@@ -64,12 +66,6 @@ var (
 	reloadCh chan chan error
 )
 
-func init() {
-	prometheus.MustRegister(snmpDuration)
-	prometheus.MustRegister(snmpRequestErrors)
-	prometheus.MustRegister(version.NewCollector("snmp_exporter"))
-}
-
 func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) {
 	query := r.URL.Query()
 
@@ -103,8 +99,8 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) {
 
 	start := time.Now()
 	registry := prometheus.NewRegistry()
-	collector := collector{ctx: r.Context(), target: target, module: module, logger: logger}
-	registry.MustRegister(collector)
+	c := collector.New(target, module, logger)
+	registry.MustRegister(c)
 	// Delegate http serving to Prometheus client library, which will call collector.Collect.
 	h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
 	h.ServeHTTP(w, r)
@@ -152,6 +148,8 @@ func main() {
 	level.Info(logger).Log("msg", "Starting snmp_exporter", "version", version.Info())
 	level.Info(logger).Log("build_context", version.BuildContext())
 
+	prometheus.MustRegister(version.NewCollector("snmp_exporter"))
+
 	// Bail early if the config is bad.
 	var err error
 	sc.C, err = config.LoadFile(*configFile)
diff --git a/snmp.yml b/snmp.yml
index a14f94d..61e3353 100644
--- a/snmp.yml
+++ b/snmp.yml
@@ -341,6 +341,7 @@ apcups:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -2659,6 +2660,7 @@ arista_sw:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -3894,6 +3896,7 @@ cisco_wlc:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -5022,6 +5025,7 @@ ddwrt:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -6441,6 +6445,7 @@ if_mib:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -9244,6 +9249,7 @@ kemp_loadmaster:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -13029,6 +13035,7 @@ mikrotik:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -19647,6 +19654,7 @@ paloalto_fw:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -24220,6 +24228,7 @@ synology:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -26771,6 +26780,7 @@ ubiquiti_airfiber:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -28619,6 +28629,7 @@ ubiquiti_airmax:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge
@@ -29944,6 +29955,7 @@ ubiquiti_unifi:
       298: ax25
       299: ieee19061nanocom
       300: cpri
+      301: omni
   - name: ifMtu
     oid: 1.3.6.1.2.1.2.2.1.4
     type: gauge


Test patch:
diff --git a/collector_test.go b/collector/collector_test.go
similarity index 99%
rename from collector_test.go
rename to collector/collector_test.go
index e4d5095..5760a75 100644
--- a/collector_test.go
+++ b/collector/collector_test.go
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package collector
 
 import (
 	"errors"