mirror of
				https://github.com/prometheus/node_exporter.git
				synced 2025-08-20 18:33:52 -07:00 
			
		
		
		
	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
This commit is contained in:
		
							parent
							
								
									9128952454
								
							
						
					
					
						commit
						cef3d98256
					
				| 
						 | 
				
			
			@ -18,11 +18,12 @@ package collector
 | 
			
		|||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus"
 | 
			
		||||
	"io"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
| 
						 | 
				
			
			@ -32,9 +33,7 @@ const (
 | 
			
		|||
// Used for calculating the total memory bytes on TCP and UDP.
 | 
			
		||||
var pageSize = os.Getpagesize()
 | 
			
		||||
 | 
			
		||||
type sockStatCollector struct {
 | 
			
		||||
	metrics map[string]prometheus.Gauge
 | 
			
		||||
}
 | 
			
		||||
type sockStatCollector struct{}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	Factories[sockStatSubsystem] = NewSockStatCollector
 | 
			
		||||
| 
						 | 
				
			
			@ -42,9 +41,7 @@ func init() {
 | 
			
		|||
 | 
			
		||||
// NewSockStatCollector returns a new Collector exposing socket stats.
 | 
			
		||||
func NewSockStatCollector() (Collector, error) {
 | 
			
		||||
	return &sockStatCollector{
 | 
			
		||||
		metrics: map[string]prometheus.Gauge{},
 | 
			
		||||
	}, nil
 | 
			
		||||
	return &sockStatCollector{}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 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)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue