Skip to content

Commit

Permalink
add group field to metrics, so they can be grouped
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Dec 17, 2015
1 parent 9f5ba9b commit 4ba0a9e
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions render/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type MetricRow struct {
ID string `json:"id"`
Label string `json:"label"`
Format string `json:"format,omitempty"`
Group string `json:"group,omitempty"`
Value float64 `json:"value"`
*report.Metric
}
Expand All @@ -46,6 +47,9 @@ func (m MetricRow) MarshalJSON() ([]byte, error) {
if m.Format != "" {
j["format"] = m.Format
}
if m.Group != "" {
j["group"] = m.Group
}
return json.Marshal(j)
}

Expand All @@ -56,7 +60,7 @@ func renderTime(t time.Time) string {
return t.Format(time.RFC3339Nano)
}

func metricRow(id, label string, metric report.Metric, format string) MetricRow {
func metricRow(id, label string, metric report.Metric, format, group string) MetricRow {
var last float64
if s := metric.LastSample(); s != nil {
last = s.Value
Expand All @@ -65,6 +69,7 @@ func metricRow(id, label string, metric report.Metric, format string) MetricRow
ID: id,
Label: label,
Format: format,
Group: group,
Value: toFixed(last, 2),
Metric: &metric,
}
Expand Down Expand Up @@ -102,6 +107,7 @@ func containerNodeMetrics(nmd report.Node) []MetricRow {
"Memory Usage",
val,
filesizeFormat,
"",
))
}
if val, ok := nmd.Metrics[docker.CPUTotalUsage]; ok {
Expand All @@ -110,6 +116,7 @@ func containerNodeMetrics(nmd report.Node) []MetricRow {
"CPU Usage",
val,
percentFormat,
"",
))
}
return rows
Expand All @@ -130,22 +137,22 @@ func hostNodeMetrics(nmd report.Node) []MetricRow {
}

rows := []MetricRow{}
for _, tuple := range []struct{ ID, Label, fmt string }{
{host.CPUUsage, "CPU Usage", percentFormat},
{host.MemUsage, "Memory Usage", percentFormat},
} {
if val, ok := nmd.Metrics[tuple.ID]; ok {
rows = append(rows, metricRow(tuple.ID, tuple.Label, val, tuple.fmt, ""))
}
}
for _, tuple := range []struct{ ID, Label string }{
{host.Load1, "Load (1m)"},
{host.Load5, "Load (5m)"},
{host.Load15, "Load (15m)"},
} {
if val, ok := nmd.Metrics[tuple.ID]; ok {
val.Max = maxLoad
rows = append(rows, metricRow(tuple.ID, tuple.Label, val, defaultFormat))
}
}
for _, tuple := range []struct{ ID, Label, fmt string }{
{host.CPUUsage, "CPU Usage", percentFormat},
{host.MemUsage, "Memory Usage", percentFormat},
} {
if val, ok := nmd.Metrics[tuple.ID]; ok {
rows = append(rows, metricRow(tuple.ID, tuple.Label, val, tuple.fmt))
rows = append(rows, metricRow(tuple.ID, tuple.Label, val, defaultFormat, "load"))
}
}
return rows
Expand Down

0 comments on commit 4ba0a9e

Please sign in to comment.