Greatly trim what netstat collector exposes by default (#876)

Netstat is 40% of the metrics on my laptop, many of which
are highly detailed information about IP internals in the kernel.
~300 such metrics on every machine in your fleet is excessive,
so focus on key metrics by default, overridable by the user.

Fixes #515

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
This commit is contained in:
Brian Brazil 2018-03-30 19:28:08 +01:00 committed by GitHub
commit 31ce32f1fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 677 deletions

View file

@ -20,17 +20,25 @@ import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
netStatsSubsystem = "netstat"
)
type netStatCollector struct{}
var (
netStatFields = kingpin.Flag("collector.netstat.fields", "Regexp of fields to return for netstat collector.").Default("^(.*_(InErrors|InErrs)|Ip_Forwarding|Ip(6|Ext)_(InOctets|OutOctets)|Icmp6?_(InMsgs|OutMsgs)|TcpExt_(Listen.*|Syncookies.*)|Tcp_(ActiveOpens|PassiveOpens|RetransSegs|CurrEstab)|Udp6?_(InDatagrams|OutDatagrams|NoPorts))$").String()
)
type netStatCollector struct {
fieldPattern *regexp.Regexp
}
func init() {
registerCollector("netstat", defaultEnabled, NewNetStatCollector)
@ -39,7 +47,10 @@ func init() {
// NewNetStatCollector takes and returns
// a new Collector exposing network stats.
func NewNetStatCollector() (Collector, error) {
return &netStatCollector{}, nil
pattern := regexp.MustCompile(*netStatFields)
return &netStatCollector{
fieldPattern: pattern,
}, nil
}
func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error {
@ -70,6 +81,9 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error {
if err != nil {
return fmt.Errorf("invalid value %s in netstats: %s", value, err)
}
if !c.fieldPattern.MatchString(key) {
continue
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, netStatsSubsystem, key),