From f841a766d761a627e5f9a5e4fb958b8dcdc3e1be Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Thu, 31 Oct 2024 12:11:25 +0100 Subject: [PATCH] Add promql expression to the usage block (fix #8) Signed-off-by: Augustin Husson --- README.md | 15 ++++++++++----- database/database.go | 8 ++++---- pkg/api/v1/metric_usage.go | 7 ++++--- source/rules/rules.go | 14 ++++++++------ 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f690f5b..e9912d6 100644 --- a/README.md +++ b/README.md @@ -24,24 +24,28 @@ This tool provides an API that can be used to get the usage for each metrics col { "prom_link": "https://prometheus.demo.do.prometheus.io", "group_name": "node-exporter.rules", - "name": "instance:node_num_cpu:sum" + "name": "instance:node_num_cpu:sum", + "expression": "count without (cpu, mode) (node_cpu_seconds_total{job=\"node\",mode=\"idle\"})" }, { "prom_link": "https://prometheus.demo.do.prometheus.io", "group_name": "node-exporter.rules", - "name": "instance:node_cpu_utilisation:rate5m" + "name": "instance:node_cpu_utilisation:rate5m", + "expression": "1 - avg without (cpu) (sum without (mode) (rate(node_cpu_seconds_total{job=\"node\",mode=~\"idle|iowait|steal\"}[5m])))" } ], "alertRules": [ { "prom_link": "https://prometheus.demo.do.prometheus.io", "group_name": "node-exporter", - "name": "NodeCPUHighUsage" + "name": "NodeCPUHighUsage", + "expression": "sum without (mode) (avg without (cpu) (rate(node_cpu_seconds_total{job=\"node\",mode!=\"idle\"}[2m]))) * 100 > 90" }, { "prom_link": "https://prometheus.demo.do.prometheus.io", "group_name": "node-exporter", - "name": "NodeSystemSaturation" + "name": "NodeSystemSaturation", + "expression": "node_load1{job=\"node\"} / count without (cpu, mode) (node_cpu_seconds_total{job=\"node\",mode=\"idle\"}) > 2" } ] } @@ -52,7 +56,8 @@ This tool provides an API that can be used to get the usage for each metrics col { "prom_link": "https://prometheus.demo.do.prometheus.io", "group_name": "ansible managed alert rules", - "name": "NodeCPUUtilizationHigh" + "name": "NodeCPUUtilizationHigh", + "expression": "instance:node_cpu_utilisation:rate5m * 100 > ignoring (severity) node_cpu_utilization_percent_threshold{severity=\"critical\"}" } ] } diff --git a/database/database.go b/database/database.go index 52e2e81..0e801a4 100644 --- a/database/database.go +++ b/database/database.go @@ -192,16 +192,16 @@ func mergeSlice[T comparable](old, new []T) []T { if len(new) == 0 { return old } - for _, oldDashboard := range old { + for _, a := range old { found := false - for _, newDashboard := range new { - if oldDashboard == newDashboard { + for _, b := range new { + if a == b { found = true break } } if !found { - new = append(new, oldDashboard) + new = append(new, a) } } return new diff --git a/pkg/api/v1/metric_usage.go b/pkg/api/v1/metric_usage.go index 0a6882b..f759f19 100644 --- a/pkg/api/v1/metric_usage.go +++ b/pkg/api/v1/metric_usage.go @@ -14,9 +14,10 @@ package v1 type RuleUsage struct { - PromLink string `json:"prom_link"` - GroupName string `json:"group_name"` - Name string `json:"name"` + PromLink string `json:"prom_link"` + GroupName string `json:"group_name"` + Name string `json:"name"` + Expression string `json:"expression"` } type MetricUsage struct { diff --git a/source/rules/rules.go b/source/rules/rules.go index a5cc4c1..189ec9c 100644 --- a/source/rules/rules.go +++ b/source/rules/rules.go @@ -93,9 +93,10 @@ func extractMetricUsageFromRules(ruleGroups []v1.RuleGroup, source string) map[s populateUsage(metricUsage, metricNames, modelAPIV1.RuleUsage{ - PromLink: source, - GroupName: ruleGroup.Name, - Name: v.Name, + PromLink: source, + GroupName: ruleGroup.Name, + Name: v.Name, + Expression: v.Query, }, false, ) @@ -108,9 +109,10 @@ func extractMetricUsageFromRules(ruleGroups []v1.RuleGroup, source string) map[s populateUsage(metricUsage, metricNames, modelAPIV1.RuleUsage{ - PromLink: source, - GroupName: ruleGroup.Name, - Name: v.Name, + PromLink: source, + GroupName: ruleGroup.Name, + Name: v.Name, + Expression: v.Query, }, true, )