Skip to content

Commit

Permalink
Add promql expression to the usage block (fix #8)
Browse files Browse the repository at this point in the history
Signed-off-by: Augustin Husson <[email protected]>
  • Loading branch information
Nexucis committed Oct 31, 2024
1 parent faef6ab commit f841a76
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand All @@ -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\"}"
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/v1/metric_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions source/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down

0 comments on commit f841a76

Please sign in to comment.