Convert sockstat collector to use ConstMetrics

This suffers from the same concurrency bug as the netstat one:
https://github.com/prometheus/node_exporter/issues/280
pull/284/head
Julius Volz 8 years ago
parent 9128952454
commit cef3d98256

@ -18,11 +18,12 @@ package collector
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/prometheus/client_golang/prometheus"
"io" "io"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/prometheus/client_golang/prometheus"
) )
const ( const (
@ -32,9 +33,7 @@ const (
// Used for calculating the total memory bytes on TCP and UDP. // Used for calculating the total memory bytes on TCP and UDP.
var pageSize = os.Getpagesize() var pageSize = os.Getpagesize()
type sockStatCollector struct { type sockStatCollector struct{}
metrics map[string]prometheus.Gauge
}
func init() { func init() {
Factories[sockStatSubsystem] = NewSockStatCollector Factories[sockStatSubsystem] = NewSockStatCollector
@ -42,9 +41,7 @@ func init() {
// NewSockStatCollector returns a new Collector exposing socket stats. // NewSockStatCollector returns a new Collector exposing socket stats.
func NewSockStatCollector() (Collector, error) { func NewSockStatCollector() (Collector, error) {
return &sockStatCollector{ return &sockStatCollector{}, nil
metrics: map[string]prometheus.Gauge{},
}, nil
} }
func (c *sockStatCollector) Update(ch chan<- prometheus.Metric) (err error) { func (c *sockStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
@ -54,27 +51,20 @@ func (c *sockStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
} }
for protocol, protocolStats := range sockStats { for protocol, protocolStats := range sockStats {
for name, value := range protocolStats { for name, value := range protocolStats {
key := protocol + "_" + name
if _, ok := c.metrics[key]; !ok {
c.metrics[key] = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: sockStatSubsystem,
Name: key,
Help: fmt.Sprintf("Number of %s sockets in state %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 sockstats: %s", value, err) return fmt.Errorf("invalid value %s in sockstats: %s", value, err)
} }
c.metrics[key].Set(v) ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, sockStatSubsystem, protocol+"_"+name),
fmt.Sprintf("Number of %s sockets in state %s.", protocol, name),
nil, nil,
),
prometheus.GaugeValue, v,
)
} }
} }
for _, m := range c.metrics {
m.Collect(ch)
}
return err return err
} }

Loading…
Cancel
Save