This is an automated email from the ASF dual-hosted git repository.

hanahmily pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git


The following commit(s) were added to refs/heads/main by this push:
     new c5f67abf [GsoC][BanyanDB] Self-Observability: Add Metrics - Uptime, 
Disk  (#481)
c5f67abf is described below

commit c5f67abf723b2a396c49f97f2171c114fb245a49
Author: Sylvie-Wxr <[email protected]>
AuthorDate: Mon Jul 1 20:07:41 2024 -0700

    [GsoC][BanyanDB] Self-Observability: Add Metrics - Uptime, Disk  (#481)
    
    * init
    
    * update labels - used, total
    
    ---------
    
    Co-authored-by: 吴晟 Wu Sheng <[email protected]>
---
 banyand/observability/metrics_system.go | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/banyand/observability/metrics_system.go 
b/banyand/observability/metrics_system.go
index c638ab6c..864dd843 100644
--- a/banyand/observability/metrics_system.go
+++ b/banyand/observability/metrics_system.go
@@ -21,8 +21,10 @@ import (
        "context"
        "strings"
        "sync"
+       "time"
 
        "github.com/shirou/gopsutil/v3/cpu"
+       "github.com/shirou/gopsutil/v3/disk"
        "github.com/shirou/gopsutil/v3/mem"
        "github.com/shirou/gopsutil/v3/net"
 
@@ -37,6 +39,7 @@ var (
        once4CpuCount sync.Once
        cpuCountsFunc = cpu.Counts
        cpuTimesFunc  = cpu.Times
+       startTime     = time.Now()
 )
 
 var (
@@ -48,6 +51,8 @@ var (
        cpuNumGauge     meter.Gauge
        memorySateGauge meter.Gauge
        netStateGauge   meter.Gauge
+       upTimeGauge     meter.Gauge
+       diskStateGauge  meter.Gauge
        initMetricsOnce sync.Once
 )
 
@@ -55,6 +60,8 @@ func init() {
        MetricsCollector.Register("cpu", collectCPU)
        MetricsCollector.Register("memory", collectMemory)
        MetricsCollector.Register("net", collectNet)
+       MetricsCollector.Register("upTime", collectUpTime)
+       MetricsCollector.Register("disk", collectDisk)
 }
 
 func initMetrics(modes []string) {
@@ -63,6 +70,8 @@ func initMetrics(modes []string) {
                cpuNumGauge = NewGauge(modes, "cpu_num")
                memorySateGauge = NewGauge(modes, "memory_state", "kind")
                netStateGauge = NewGauge(modes, "net_state", "kind", "name")
+               upTimeGauge = NewGauge(modes, "up_time")
+               diskStateGauge = NewGauge(modes, "disk", "path", "kind")
        })
 }
 
@@ -104,7 +113,8 @@ func collectMemory() {
                log.Error().Err(err).Msg("cannot get memory stat")
        }
        memorySateGauge.Set(m.UsedPercent/100, "used_percent")
-       memorySateGauge.Set(float64(m.Used)/float64(m.Total), "used")
+       memorySateGauge.Set(float64(m.Used), "used")
+       memorySateGauge.Set(float64(m.Total), "total")
 }
 
 func collectNet() {
@@ -152,3 +162,23 @@ func getNetStat(ctx context.Context) 
([]net.IOCountersStat, error) {
        }
        return availableStats, nil
 }
+
+func collectUpTime() {
+       upTimeGauge.Set(time.Since(startTime).Seconds())
+}
+
+func collectDisk() {
+       for path := range getPath() {
+               usage, err := disk.Usage(path)
+               if err != nil {
+                       // skip logging the case where the data has not been 
created
+                       if err.Error() == "no such file or directory" {
+                               log.Error().Err(err).Msgf("failed to get usage 
for path: %s", path)
+                       }
+                       return
+               }
+               diskStateGauge.Set(usage.UsedPercent/100, path, "used_percent")
+               diskStateGauge.Set(float64(usage.Used), path, "used")
+               diskStateGauge.Set(float64(usage.Total), path, "total")
+       }
+}

Reply via email to