From 14835e8aa6a65c0d8715699bcb77fc6a6f81f645 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Wed, 20 Nov 2024 17:38:28 +0000 Subject: [PATCH 1/4] changes --- metrics/noop.go | 7 ++++- metrics/prometheus.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/metrics/noop.go b/metrics/noop.go index 6eb909ff9..51278b263 100644 --- a/metrics/noop.go +++ b/metrics/noop.go @@ -5,7 +5,10 @@ package metrics -import "net/http" +import ( + "net/http" + "time" +) // noopMetrics implements a no operations metrics service type noopMetrics struct{} @@ -48,3 +51,5 @@ func (n noopMeters) Set(int64) {} func (n noopMeters) Observe(int64) {} func (n *noopMetrics) ObserveWithLabels(int64, map[string]string) {} + +func (n *noopMetrics) collectDiskIO(refresh time.Duration) {} diff --git a/metrics/prometheus.go b/metrics/prometheus.go index 50745752c..2d186db81 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -6,8 +6,14 @@ package metrics import ( + "bufio" + "fmt" "net/http" + "os" + "strconv" + "strings" "sync" + "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -24,6 +30,8 @@ func InitializePrometheusMetrics() { // don't allow for reset if _, ok := metrics.(*prometheusMetrics); !ok { metrics = newPrometheusMetrics() + // collection disk io metrics every 5 seconds + go metrics.(*prometheusMetrics).collectDiskIO(5 * time.Second) } } @@ -147,6 +155,63 @@ func (o *prometheusMetrics) newHistogramMeter(name string, buckets []int64) Hist } } +func getIOLineValue(line string) (int64) { + fields := strings.Fields(line) + if len(fields) != 2 { + logger.Warn("this io file line is malformed", "err", line) + return 0 + } + value, err := strconv.ParseInt(fields[1], 10, 64) + if err != nil { + logger.Warn("unable to parse int", "err", err) + return 0 + } + + return value +} + +func getDiskIOData() (int64, int64, error) { + pid := os.Getpid() + ioFilePath := fmt.Sprintf("/proc/%d/io", pid) + file, err := os.Open(ioFilePath) + if err != nil { + return 0, 0, err + } + + // Parse the file line by line + scanner := bufio.NewScanner(file) + var reads, writes int64 + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "syscr") { + reads = getIOLineValue(line) + } else if strings.HasPrefix(line, "syscw") { + writes = getIOLineValue(line) + } + } + + return reads, writes, nil +} + +func (o *prometheusMetrics) collectDiskIO(refresh time.Duration) { + for { + reads, writes, err := getDiskIOData() + if err != nil { + continue + } + + logger.Info("LLEGA", "reads", reads, "writes", writes) + + readsMeter := o.GetOrCreateGaugeMeter("disk_reads") + readsMeter.Set(reads) + + writesMeter := o.GetOrCreateGaugeMeter("disk_writes") + writesMeter.Set(writes) + + time.Sleep(refresh) + } +} + type promHistogramMeter struct { histogram prometheus.Histogram } From 683f236159a762806f508e6a75f839719dd17589 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Fri, 22 Nov 2024 12:30:00 +0000 Subject: [PATCH 2/4] removed log --- metrics/prometheus.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/metrics/prometheus.go b/metrics/prometheus.go index 2d186db81..27a8e3f98 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -200,8 +200,6 @@ func (o *prometheusMetrics) collectDiskIO(refresh time.Duration) { continue } - logger.Info("LLEGA", "reads", reads, "writes", writes) - readsMeter := o.GetOrCreateGaugeMeter("disk_reads") readsMeter.Set(reads) From 1b3d9696b5971eb1ba6dbfec5f7ff29f935e98a5 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Fri, 22 Nov 2024 13:01:13 +0000 Subject: [PATCH 3/4] sleeping any way --- metrics/prometheus.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/metrics/prometheus.go b/metrics/prometheus.go index 27a8e3f98..1ea314b85 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -198,13 +198,13 @@ func (o *prometheusMetrics) collectDiskIO(refresh time.Duration) { reads, writes, err := getDiskIOData() if err != nil { continue - } - - readsMeter := o.GetOrCreateGaugeMeter("disk_reads") - readsMeter.Set(reads) + } else { + readsMeter := o.GetOrCreateGaugeMeter("disk_reads") + readsMeter.Set(reads) - writesMeter := o.GetOrCreateGaugeMeter("disk_writes") - writesMeter.Set(writes) + writesMeter := o.GetOrCreateGaugeMeter("disk_writes") + writesMeter.Set(writes) + } time.Sleep(refresh) } From 2191a476a00092f6c25327658ecc20686c5511c0 Mon Sep 17 00:00:00 2001 From: Miguel Angel Rojo Fernandez Date: Fri, 22 Nov 2024 14:28:20 +0000 Subject: [PATCH 4/4] pr review --- metrics/prometheus.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/metrics/prometheus.go b/metrics/prometheus.go index 1ea314b85..90b41486d 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -196,9 +196,7 @@ func getDiskIOData() (int64, int64, error) { func (o *prometheusMetrics) collectDiskIO(refresh time.Duration) { for { reads, writes, err := getDiskIOData() - if err != nil { - continue - } else { + if err == nil { readsMeter := o.GetOrCreateGaugeMeter("disk_reads") readsMeter.Set(reads)