Skip to content

Commit 5a104d9

Browse files
committed
process_collector: collect received/transmitted bytes
Signed-off-by: Huw Jones <[email protected]>
1 parent fec6b22 commit 5a104d9

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

prometheus/process_collector.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import (
2222
)
2323

2424
type processCollector struct {
25-
collectFn func(chan<- Metric)
26-
pidFn func() (int, error)
27-
reportErrors bool
28-
cpuTotal *Desc
29-
openFDs, maxFDs *Desc
30-
vsize, maxVsize *Desc
31-
rss *Desc
32-
startTime *Desc
25+
collectFn func(chan<- Metric)
26+
pidFn func() (int, error)
27+
reportErrors bool
28+
cpuTotal *Desc
29+
openFDs, maxFDs *Desc
30+
vsize, maxVsize *Desc
31+
rss *Desc
32+
startTime *Desc
33+
inBytes, outBytes *Desc
3334
}
3435

3536
// ProcessCollectorOpts defines the behavior of a process metrics collector
@@ -100,6 +101,16 @@ func NewProcessCollector(opts ProcessCollectorOpts) Collector {
100101
"Start time of the process since unix epoch in seconds.",
101102
nil, nil,
102103
),
104+
inBytes: NewDesc(
105+
ns+"process_network_receive_bytes_total",
106+
"Number of bytes received by the process over the network.",
107+
nil, nil,
108+
),
109+
outBytes: NewDesc(
110+
ns+"process_network_transmit_bytes_total",
111+
"Number of bytes sent by the process over the network.",
112+
nil, nil,
113+
),
103114
}
104115

105116
if opts.PidFn == nil {

prometheus/process_collector_other.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,18 @@ func (c *processCollector) processCollect(ch chan<- Metric) {
6363
} else {
6464
c.reportError(ch, nil, err)
6565
}
66+
67+
if netstat, err := p.Netstat(); err == nil {
68+
var inOctets, outOctets float64
69+
if netstat.IpExt.InOctets != nil {
70+
inOctets = *netstat.IpExt.InOctets
71+
}
72+
if netstat.IpExt.OutOctets != nil {
73+
outOctets = *netstat.IpExt.OutOctets
74+
}
75+
ch <- MustNewConstMetric(c.inBytes, CounterValue, inOctets)
76+
ch <- MustNewConstMetric(c.outBytes, CounterValue, outOctets)
77+
} else {
78+
c.reportError(ch, nil, err)
79+
}
6680
}

prometheus/process_collector_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,17 @@ func TestProcessCollector(t *testing.T) {
6969
regexp.MustCompile("\nprocess_virtual_memory_bytes [1-9]"),
7070
regexp.MustCompile("\nprocess_resident_memory_bytes [1-9]"),
7171
regexp.MustCompile("\nprocess_start_time_seconds [0-9.]{10,}"),
72+
regexp.MustCompile("\nprocess_network_receive_bytes_total [0-9]+"),
73+
regexp.MustCompile("\nprocess_network_transmit_bytes_total [0-9]+"),
7274
regexp.MustCompile("\nfoobar_process_cpu_seconds_total [0-9]"),
7375
regexp.MustCompile("\nfoobar_process_max_fds [1-9]"),
7476
regexp.MustCompile("\nfoobar_process_open_fds [1-9]"),
7577
regexp.MustCompile("\nfoobar_process_virtual_memory_max_bytes (-1|[1-9])"),
7678
regexp.MustCompile("\nfoobar_process_virtual_memory_bytes [1-9]"),
7779
regexp.MustCompile("\nfoobar_process_resident_memory_bytes [1-9]"),
7880
regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9.]{10,}"),
81+
regexp.MustCompile("\nfoobar_process_network_receive_bytes_total [0-9]+"),
82+
regexp.MustCompile("\nfoobar_process_network_transmit_bytes_total [0-9]+"),
7983
} {
8084
if !re.Match(buf.Bytes()) {
8185
t.Errorf("want body to match %s\n%s", re, buf.String())

0 commit comments

Comments
 (0)