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

node_disk_*: Keep a cumulative count of bytes read and written #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 32 additions & 7 deletions enhanced/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

var (
diskRead = prometheus.NewCounterVec(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diskRead is a global variable (from gochecknoglobals)

prometheus.CounterOpts{
Namespace: "node",
Subsystem: "disk",
Name: "read_bytes_total",
Help: "The total number of bytes read successfully.",
},
[]string{"region", "instance", "device"},
)
diskWritten = prometheus.NewCounterVec(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diskWritten is a global variable (from gochecknoglobals)

prometheus.CounterOpts{
Namespace: "node",
Subsystem: "disk",
Name: "written_bytes_total",
Help: "The total number of bytes written successfully.",
},
[]string{"region", "instance", "device"},
)
)

// osMetrics represents available Enhanced Monitoring OS metrics from CloudWatch Logs.
//
// See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Monitoring.OS.html#USER_Monitoring.OS.CloudWatchLogs
Expand Down Expand Up @@ -265,19 +286,23 @@ func makeRDSDiskIOMetrics(s *diskIO, constLabels prometheus.Labels) []prometheus

// makeNodeDiskMetrics returns node_exporter-like node_disk_ metrics.
func makeNodeDiskMetrics(s *diskIO, constLabels prometheus.Labels) []prometheus.Metric {
// move device name to label
labelKeys := []string{"device"}
labelValues := []string{s.Device}
labels := make(prometheus.Labels, len(constLabels)+1)

for k, v := range constLabels {
labels[k] = v
}

labels["device"] = s.Device
stevenjm marked this conversation as resolved.
Show resolved Hide resolved
res := make([]prometheus.Metric, 0, 2)

if s.ReadKb != nil {
desc := prometheus.NewDesc("node_disk_read_bytes_total", "The total number of bytes read successfully.", labelKeys, constLabels)
m := prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(*s.ReadKb*1024), labelValues...)
m := diskRead.With(labels)
m.Add(float64(*s.ReadKb * 1024))
res = append(res, m)
}
if s.WriteKb != nil {
desc := prometheus.NewDesc("node_disk_written_bytes_total", "The total number of bytes written successfully.", labelKeys, constLabels)
m := prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(*s.WriteKb*1024), labelValues...)
m := diskWritten.With(labels)
m.Add(float64(*s.WriteKb * 1024))
res = append(res, m)
}

Expand Down
19 changes: 18 additions & 1 deletion enhanced/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ func TestParse(t *testing.T) {
t.Run(data.instance, func(t *testing.T) {
// Test that metrics created from fixed testdata JSON produce expected result.

m, err := parseOSMetrics(readTestDataJSON(t, data.instance), true)
d := readTestDataJSON(t, data.instance)

m, err := parseOSMetrics(d, true)
require.NoError(t, err)

m2, err := parseOSMetrics(d, true)
require.NoError(t, err)

actualMetrics := helpers.ReadMetrics(m.makePrometheusMetrics(data.region, nil))
sort.Slice(actualMetrics, func(i, j int) bool { return actualMetrics[i].Less(actualMetrics[j]) })
actualLines := helpers.Format(helpers.WriteMetrics(actualMetrics))

actualMetrics2 := helpers.ReadMetrics(m2.makePrometheusMetrics(data.region, nil))
sort.Slice(actualMetrics2, func(i, j int) bool { return actualMetrics2[i].Less(actualMetrics2[j]) })

if *goldenTXT {
writeTestDataMetrics(t, data.instance, actualLines)
}
Expand All @@ -41,6 +49,15 @@ func TestParse(t *testing.T) {
// compare both to try to avoid go-difflib bug
assert.Equal(t, expectedLines, actualLines)
assert.Equal(t, expectedMetrics, actualMetrics)

for i, v := range actualMetrics {
switch v.Name {
case "node_disk_read_bytes_total", "node_disk_written_bytes_total":
assert.Equal(t, 2*v.Value, actualMetrics2[i].Value)
default:
assert.Equal(t, v.Value, actualMetrics2[i].Value)
}
}
})
}
}
Expand Down
13 changes: 10 additions & 3 deletions enhanced/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,23 @@ func (s *scraper) scrape(ctx context.Context) (map[string][]prometheus.Metric, m
l = l.With("IngestionTime", aws.MillisecondsTimeValue(event.IngestionTime).UTC())

var instance *sessions.Instance
for _, i := range s.instances {
if i.ResourceID == *event.LogStreamName {
instance = &i

for i := range s.instances {
stevenjm marked this conversation as resolved.
Show resolved Hide resolved
if s.instances[i].ResourceID == *event.LogStreamName {
instance = &s.instances[i]
break
}
}
if instance == nil {
l.Errorf("Failed to find instance.")
continue
}

if *event.Timestamp <= instance.LastEventTimestamp {
stevenjm marked this conversation as resolved.
Show resolved Hide resolved
continue
}

instance.LastEventTimestamp = *event.Timestamp
stevenjm marked this conversation as resolved.
Show resolved Hide resolved
l = l.With("region", instance.Region).With("instance", instance.Instance)

// l.Debugf("Message:\n%s", *event.Message)
Expand Down
1 change: 1 addition & 0 deletions sessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Instance struct {
ResourceID string
Labels map[string]string
EnhancedMonitoringInterval time.Duration
LastEventTimestamp int64
}

func (i Instance) String() string {
Expand Down