Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics: Disk IO reads and writes #890

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion metrics/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package metrics

import "net/http"
import (
"net/http"
"time"
)

// noopMetrics implements a no operations metrics service
type noopMetrics struct{}
Expand Down Expand Up @@ -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) {}
61 changes: 61 additions & 0 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -147,6 +155,59 @@ 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 {
readsMeter := o.GetOrCreateGaugeMeter("disk_reads")
freemanzMrojo marked this conversation as resolved.
Show resolved Hide resolved
readsMeter.Set(reads)

writesMeter := o.GetOrCreateGaugeMeter("disk_writes")
writesMeter.Set(writes)
}

time.Sleep(refresh)
}
}

type promHistogramMeter struct {
histogram prometheus.Histogram
}
Expand Down