Skip to content

Commit 7280b38

Browse files
committed
added the node_uptime_seconds metric
1 parent ce52987 commit 7280b38

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

node/collector.go

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ var (
2121
"Meta information about the cloud instance",
2222
[]string{"provider", "account_id", "instance_id", "instance_type", "instance_life_cycle", "region", "availability_zone", "availability_zone_id", "local_ipv4", "public_ipv4"}, nil,
2323
)
24+
uptimeDesc = prometheus.NewDesc(
25+
"node_uptime_seconds",
26+
"Uptime of the node in seconds",
27+
[]string{}, nil,
28+
)
2429
cpuUsageDesc = prometheus.NewDesc(
2530
"node_resources_cpu_usage_seconds_total",
2631
"The amount of CPU time spent in each mode",
@@ -159,6 +164,14 @@ func NewCollector(hostname, kernelVersion string) *Collector {
159164

160165
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
161166
ch <- gauge(infoDesc, 1, c.hostname, c.kernelVersion)
167+
168+
v, err := uptime(procRoot)
169+
if err != nil {
170+
klog.Errorln(err)
171+
} else {
172+
ch <- gauge(uptimeDesc, v)
173+
}
174+
162175
cpu, err := cpuStat(procRoot)
163176
if err != nil {
164177
if !common.IsNotExist(err) {
@@ -236,6 +249,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
236249
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
237250
ch <- infoDesc
238251
ch <- cloudInfoDesc
252+
ch <- uptimeDesc
239253
ch <- cpuUsageDesc
240254
ch <- cpuLogicalCoresDesc
241255
ch <- memTotalDesc

node/fixtures/proc/uptime

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2659150.03 1208658.10

node/uptime.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package node
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func uptime(procRoot string) (float64, error) {
12+
data, err := os.ReadFile(path.Join(procRoot, "uptime"))
13+
if err != nil {
14+
return 0, err
15+
}
16+
fields := strings.Fields(string(data))
17+
if len(fields) != 2 {
18+
return 0, fmt.Errorf("invalid format of /proc/uptime")
19+
}
20+
return strconv.ParseFloat(fields[0], 64)
21+
}

node/uptime_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package node
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNode_uptime(t *testing.T) {
9+
v, err := uptime("fixtures/proc")
10+
assert.Nil(t, err)
11+
assert.Equal(t, 2659150.03, v)
12+
}

0 commit comments

Comments
 (0)