Skip to content

Commit

Permalink
process_collector: collect received/transmitted bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Huw Jones <huw@pexip.com>
  • Loading branch information
huwcbjones committed Jul 16, 2024
1 parent fec6b22 commit 5a104d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
27 changes: 19 additions & 8 deletions prometheus/process_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
)

type processCollector struct {
collectFn func(chan<- Metric)
pidFn func() (int, error)
reportErrors bool
cpuTotal *Desc
openFDs, maxFDs *Desc
vsize, maxVsize *Desc
rss *Desc
startTime *Desc
collectFn func(chan<- Metric)
pidFn func() (int, error)
reportErrors bool
cpuTotal *Desc
openFDs, maxFDs *Desc
vsize, maxVsize *Desc
rss *Desc
startTime *Desc
inBytes, outBytes *Desc
}

// ProcessCollectorOpts defines the behavior of a process metrics collector
Expand Down Expand Up @@ -100,6 +101,16 @@ func NewProcessCollector(opts ProcessCollectorOpts) Collector {
"Start time of the process since unix epoch in seconds.",
nil, nil,
),
inBytes: NewDesc(
ns+"process_network_receive_bytes_total",
"Number of bytes received by the process over the network.",
nil, nil,
),
outBytes: NewDesc(
ns+"process_network_transmit_bytes_total",
"Number of bytes sent by the process over the network.",
nil, nil,
),
}

if opts.PidFn == nil {
Expand Down
14 changes: 14 additions & 0 deletions prometheus/process_collector_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,18 @@ func (c *processCollector) processCollect(ch chan<- Metric) {
} else {
c.reportError(ch, nil, err)
}

if netstat, err := p.Netstat(); err == nil {
var inOctets, outOctets float64
if netstat.IpExt.InOctets != nil {
inOctets = *netstat.IpExt.InOctets
}
if netstat.IpExt.OutOctets != nil {
outOctets = *netstat.IpExt.OutOctets
}
ch <- MustNewConstMetric(c.inBytes, CounterValue, inOctets)
ch <- MustNewConstMetric(c.outBytes, CounterValue, outOctets)
} else {
c.reportError(ch, nil, err)
}
}
4 changes: 4 additions & 0 deletions prometheus/process_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ func TestProcessCollector(t *testing.T) {
regexp.MustCompile("\nprocess_virtual_memory_bytes [1-9]"),
regexp.MustCompile("\nprocess_resident_memory_bytes [1-9]"),
regexp.MustCompile("\nprocess_start_time_seconds [0-9.]{10,}"),
regexp.MustCompile("\nprocess_network_receive_bytes_total [0-9]+"),
regexp.MustCompile("\nprocess_network_transmit_bytes_total [0-9]+"),
regexp.MustCompile("\nfoobar_process_cpu_seconds_total [0-9]"),
regexp.MustCompile("\nfoobar_process_max_fds [1-9]"),
regexp.MustCompile("\nfoobar_process_open_fds [1-9]"),
regexp.MustCompile("\nfoobar_process_virtual_memory_max_bytes (-1|[1-9])"),
regexp.MustCompile("\nfoobar_process_virtual_memory_bytes [1-9]"),
regexp.MustCompile("\nfoobar_process_resident_memory_bytes [1-9]"),
regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9.]{10,}"),
regexp.MustCompile("\nfoobar_process_network_receive_bytes_total [0-9]+"),
regexp.MustCompile("\nfoobar_process_network_transmit_bytes_total [0-9]+"),
} {
if !re.Match(buf.Bytes()) {
t.Errorf("want body to match %s\n%s", re, buf.String())
Expand Down

0 comments on commit 5a104d9

Please sign in to comment.