Merge pull request #281 from prometheus/netstat-const-metrics

Convert netstat collector to use ConstMetrics
pull/282/head
Julius Volz 8 years ago committed by GitHub
commit 55761fc788

@ -30,20 +30,16 @@ const (
netStatsSubsystem = "netstat" netStatsSubsystem = "netstat"
) )
type netStatCollector struct { type netStatCollector struct{}
metrics map[string]prometheus.Gauge
}
func init() { func init() {
Factories["netstat"] = NewNetStatCollector Factories["netstat"] = NewNetStatCollector
} }
// NewNetStatCollector takes a returns // NewNetStatCollector takes and returns
// a new Collector exposing network stats. // a new Collector exposing network stats.
func NewNetStatCollector() (Collector, error) { func NewNetStatCollector() (Collector, error) {
return &netStatCollector{ return &netStatCollector{}, nil
metrics: map[string]prometheus.Gauge{},
}, nil
} }
func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
@ -56,34 +52,28 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
return fmt.Errorf("couldn't get SNMP stats: %s", err) return fmt.Errorf("couldn't get SNMP stats: %s", err)
} }
// Merge the results of snmpStats into netStats (collisions are possible, but // Merge the results of snmpStats into netStats (collisions are possible, but
// we know that the keys are always unique for the given use case) // we know that the keys are always unique for the given use case).
for k, v := range snmpStats { for k, v := range snmpStats {
netStats[k] = v netStats[k] = v
} }
for protocol, protocolStats := range netStats { for protocol, protocolStats := range netStats {
for name, value := range protocolStats { for name, value := range protocolStats {
key := protocol + "_" + name key := protocol + "_" + name
if _, ok := c.metrics[key]; !ok {
c.metrics[key] = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: netStatsSubsystem,
Name: key,
Help: fmt.Sprintf("Protocol %s statistic %s.", protocol, name),
},
)
}
v, err := strconv.ParseFloat(value, 64) v, err := strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
return fmt.Errorf("invalid value %s in netstats: %s", value, err) return fmt.Errorf("invalid value %s in netstats: %s", value, err)
} }
c.metrics[key].Set(v) ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, netStatsSubsystem, key),
fmt.Sprintf("Protocol %s statistic %s.", protocol, name),
nil, nil,
),
prometheus.UntypedValue, v,
)
} }
} }
for _, m := range c.metrics { return nil
m.Collect(ch)
}
return err
} }
func getNetStats(fileName string) (map[string]map[string]string, error) { func getNetStats(fileName string) (map[string]map[string]string, error) {

Loading…
Cancel
Save