Skip to content

Commit

Permalink
added the node_uptime_seconds metric
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed Feb 23, 2023
1 parent ce52987 commit 7280b38
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions node/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ var (
"Meta information about the cloud instance",
[]string{"provider", "account_id", "instance_id", "instance_type", "instance_life_cycle", "region", "availability_zone", "availability_zone_id", "local_ipv4", "public_ipv4"}, nil,
)
uptimeDesc = prometheus.NewDesc(
"node_uptime_seconds",
"Uptime of the node in seconds",
[]string{}, nil,
)
cpuUsageDesc = prometheus.NewDesc(
"node_resources_cpu_usage_seconds_total",
"The amount of CPU time spent in each mode",
Expand Down Expand Up @@ -159,6 +164,14 @@ func NewCollector(hostname, kernelVersion string) *Collector {

func (c *Collector) Collect(ch chan<- prometheus.Metric) {
ch <- gauge(infoDesc, 1, c.hostname, c.kernelVersion)

v, err := uptime(procRoot)
if err != nil {
klog.Errorln(err)
} else {
ch <- gauge(uptimeDesc, v)
}

cpu, err := cpuStat(procRoot)
if err != nil {
if !common.IsNotExist(err) {
Expand Down Expand Up @@ -236,6 +249,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- infoDesc
ch <- cloudInfoDesc
ch <- uptimeDesc
ch <- cpuUsageDesc
ch <- cpuLogicalCoresDesc
ch <- memTotalDesc
Expand Down
1 change: 1 addition & 0 deletions node/fixtures/proc/uptime
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2659150.03 1208658.10
21 changes: 21 additions & 0 deletions node/uptime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package node

import (
"fmt"
"os"
"path"
"strconv"
"strings"
)

func uptime(procRoot string) (float64, error) {
data, err := os.ReadFile(path.Join(procRoot, "uptime"))
if err != nil {
return 0, err
}
fields := strings.Fields(string(data))
if len(fields) != 2 {
return 0, fmt.Errorf("invalid format of /proc/uptime")
}
return strconv.ParseFloat(fields[0], 64)
}
12 changes: 12 additions & 0 deletions node/uptime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package node

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNode_uptime(t *testing.T) {
v, err := uptime("fixtures/proc")
assert.Nil(t, err)
assert.Equal(t, 2659150.03, v)
}

0 comments on commit 7280b38

Please sign in to comment.