-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding protocols collector #1921
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
protocol size sockets memory press maxhdr slab module cl co di ac io in de sh ss gs se re sp bi br ha uh gp em | ||
PACKET 1344 2 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n | ||
PINGv6 1112 0 -1 NI 0 yes kernel y y y n n y n n y y y y n y y y y y n | ||
RAWv6 1112 1 -1 NI 0 yes kernel y y y n y y y n y y y y n y y y y n n | ||
UDPLITEv6 1216 0 57 NI 0 yes kernel y y y n y y y n y y y y n n n y y y n | ||
UDPv6 1216 10 57 NI 0 yes kernel y y y n y y y n y y y y n n n y y y n | ||
TCPv6 2144 1937 1225378 no 320 yes kernel y y y y y y y y y y y y y n y y y y y | ||
UNIX 1024 120 -1 NI 0 yes kernel n n n n n n n n n n n n n n n n n n n | ||
UDP-Lite 1024 0 57 NI 0 yes kernel y y y n y y y n y y y y y n n y y y n | ||
PING 904 0 -1 NI 0 yes kernel y y y n n y n n y y y y n y y y y y n | ||
RAW 912 0 -1 NI 0 yes kernel y y y n y y y n y y y y n y y y y n n | ||
UDP 1024 73 57 NI 0 yes kernel y y y n y y y n y y y y y n n y y y n | ||
TCP 1984 93064 1225378 yes 320 yes kernel y y y y y y y y y y y y y n y y y y y | ||
NETLINK 1040 16 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright 2015 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/procfs" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
const ( | ||
subsystem = "protocols" | ||
) | ||
|
||
var ( | ||
netProtocolFilter = kingpin.Flag("collector.protocols.filter", "Regex of protocols to return for the collector.").Default("^tcp.*").String() | ||
) | ||
|
||
type protocolsCollector struct { | ||
fs procfs.FS | ||
logger log.Logger | ||
} | ||
|
||
func init() { | ||
registerCollector(subsystem, defaultEnabled, NewProtocolsCollector) | ||
} | ||
|
||
// NewProtocolsCollector returns a Collector exposing net/protocols stats | ||
func NewProtocolsCollector(logger log.Logger) (Collector, error) { | ||
fs, err := procfs.NewFS(*procPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open procfs: %w", err) | ||
} | ||
return &protocolsCollector{fs, logger}, nil | ||
} | ||
|
||
// Update implements Collector and exposes /proc/net/protocols metrics | ||
func (c *protocolsCollector) Update(ch chan<- prometheus.Metric) error { | ||
|
||
protocolStats, err := c.fs.NetProtocols() | ||
if err != nil { | ||
return fmt.Errorf("couldn't get protocols: %w", err) | ||
} | ||
|
||
// In the interest of reudcing cardinality we are only interested in the | ||
// first 8 fields as subsequent fields are not numerical or likely to change | ||
// over time. | ||
for _, p := range protocolStats { | ||
p.Name = strings.Replace(strings.ToLower(p.Name), "-", "", -1) | ||
re := regexp.MustCompile(*netProtocolFilter) | ||
if !re.MatchString(p.Name) { | ||
continue | ||
} | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName( | ||
namespace, | ||
subsystem, | ||
fmt.Sprintf("%s_size", p.Name), | ||
), | ||
fmt.Sprintf("The size, in bytes, of %s", p.Name), | ||
nil, | ||
nil, | ||
), | ||
prometheus.GaugeValue, | ||
float64(p.Size), | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName( | ||
namespace, | ||
subsystem, | ||
fmt.Sprintf("%s_sockets", p.Name), | ||
), | ||
fmt.Sprintf("Number of sockets in use by %s", p.Name), | ||
nil, | ||
nil, | ||
), | ||
prometheus.GaugeValue, | ||
float64(p.Sockets), | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName( | ||
namespace, | ||
subsystem, | ||
fmt.Sprintf("%s_memory", p.Name), | ||
), | ||
fmt.Sprintf("Total number of 4KB pages allocated by %s", p.Name), | ||
Comment on lines
+106
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be converted to bytes. |
||
nil, | ||
nil, | ||
), | ||
prometheus.GaugeValue, | ||
float64(p.Memory), | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName( | ||
namespace, | ||
subsystem, | ||
fmt.Sprintf("%s_pressure", p.Name), | ||
), | ||
fmt.Sprintf("Indicates whether %s is experiencing memory pressure; 1 = true, 0 = false, -1 = not implemented", p.Name), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be split into two metrics.
It looks like this is an issue in the procfs implementation. This should be fixed to return the proper state options as an |
||
nil, | ||
nil, | ||
), | ||
prometheus.GaugeValue, | ||
float64(p.Pressure), | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName( | ||
namespace, | ||
subsystem, | ||
fmt.Sprintf("%s_maxhdr", p.Name), | ||
), | ||
fmt.Sprintf("Max header size for %s", p.Name), | ||
nil, | ||
nil, | ||
), | ||
prometheus.GaugeValue, | ||
float64(p.MaxHeader), | ||
) | ||
slabVal := float64(0.0) | ||
if p.Slab { | ||
slabVal = 1.0 | ||
} | ||
ch <- prometheus.MustNewConstMetric( | ||
prometheus.NewDesc( | ||
prometheus.BuildFQName( | ||
namespace, | ||
subsystem, | ||
fmt.Sprintf("%s_slab", p.Name), | ||
), | ||
fmt.Sprintf("Bool indicating whether %s is allocated from SLAB", p.Name), | ||
nil, | ||
nil, | ||
), | ||
prometheus.GaugeValue, | ||
slabVal, | ||
) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.