Skip to content

Commit

Permalink
add sys.uptime metric (#134)
Browse files Browse the repository at this point in the history
This metric tracks the number of seconds that have elapsed since the last system
boot.
  • Loading branch information
copperlight authored Jul 12, 2024
1 parent 182d1f4 commit caad3c7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bin/atlas-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static void gather_slow_titus_metrics(CGroup* cGroup, Proc* proc, Disk* disk, Aw
proc->network_stats();
proc->process_stats();
proc->snmp_stats();
proc->uptime_stats();
}
#else
static void gather_peak_system_metrics(Proc* proc) { proc->peak_cpu_stats(); }
Expand All @@ -89,6 +90,7 @@ static void gather_slow_system_metrics(Proc* proc, Disk* disk, Ethtool* ethtool,
proc->process_stats();
proc->snmp_stats();
proc->socket_stats();
proc->uptime_stats();
proc->vmstats();
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/proc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,15 @@ inline void set_if_present(const std::unordered_map<std::string, int64_t>& stats
}
}

template <typename Reg>
void Proc<Reg>::uptime_stats() noexcept {
static auto sys_uptime = registry_->GetGauge("sys.uptime");
// uptime values are in seconds, reported as doubles, but given how large they will be over
// time, the 10ths of a second will not matter for the purpose of producing this metric
auto uptime_seconds = read_num_vector_from_file(path_prefix_, "uptime");
sys_uptime->Set(uptime_seconds[0]);
}

template <typename Reg>
void Proc<Reg>::vmstats() noexcept {
static auto processes = registry_->GetMonotonicCounter("vmstat.procs.count");
Expand Down
1 change: 1 addition & 0 deletions lib/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Proc {
void memory_stats() noexcept;
void process_stats() noexcept;
void socket_stats() noexcept;
void uptime_stats() noexcept;
void vmstats() noexcept;
[[nodiscard]] bool is_container() const noexcept;

Expand Down
11 changes: 11 additions & 0 deletions lib/proc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ TEST(Proc, CpuStats) {
EXPECT_EQ(17, ms2.size());
}

TEST(Proc, UptimeStats) {
Registry registry;
spectator::Tags extra{{"nf.test", "extra"}};
Proc proc{&registry, extra, "testdata/resources/proc"};
proc.uptime_stats();
auto ms = my_measurements(&registry);
auto ms_map = measurements_to_map(ms, "proto");
expect_value(&ms_map, "sys.uptime|gauge", 517407);
EXPECT_TRUE(ms_map.empty());
}

TEST(Proc, VmStats) {
Registry registry;
spectator::Tags extra{{"nf.test", "extra"}};
Expand Down
1 change: 1 addition & 0 deletions testdata/resources/proc/uptime
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
517407.10 4128064.05

0 comments on commit caad3c7

Please sign in to comment.