|
|
|
@ -3,6 +3,7 @@ package prom |
|
|
|
|
import ( |
|
|
|
|
"encoding/json" |
|
|
|
|
"fmt" |
|
|
|
|
"maps" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
@ -142,7 +143,7 @@ func TestPrometheusRulesToGrafana(t *testing.T) { |
|
|
|
|
errorMsg: "limit is not supported", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "rule group with labels is not supported", |
|
|
|
|
name: "rule group with labels", |
|
|
|
|
orgID: 1, |
|
|
|
|
namespace: "namespaceUID", |
|
|
|
|
promGroup: PrometheusRuleGroup{ |
|
|
|
@ -156,8 +157,7 @@ func TestPrometheusRulesToGrafana(t *testing.T) { |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
expectError: true, |
|
|
|
|
errorMsg: "labels are not supported", |
|
|
|
|
expectError: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -208,10 +208,9 @@ func TestPrometheusRulesToGrafana(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
require.Equal(t, expectedFor, grafanaRule.For, tc.name) |
|
|
|
|
|
|
|
|
|
expectedLabels := make(map[string]string, len(promRule.Labels)+1) |
|
|
|
|
for k, v := range promRule.Labels { |
|
|
|
|
expectedLabels[k] = v |
|
|
|
|
} |
|
|
|
|
expectedLabels := make(map[string]string, len(promRule.Labels)+len(tc.promGroup.Labels)) |
|
|
|
|
maps.Copy(expectedLabels, tc.promGroup.Labels) |
|
|
|
|
maps.Copy(expectedLabels, promRule.Labels) |
|
|
|
|
|
|
|
|
|
uidData := fmt.Sprintf("%d|%s|%s|%d", tc.orgID, tc.namespace, tc.promGroup.Name, j) |
|
|
|
|
u := uuid.NewSHA1(uuid.NameSpaceOID, []byte(uidData)) |
|
|
|
@ -390,6 +389,129 @@ func TestPrometheusRulesToGrafana_NodesInRules(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPrometheusRulesToGrafana_GroupLabels(t *testing.T) { |
|
|
|
|
cfg := Config{ |
|
|
|
|
DatasourceUID: "datasource-uid", |
|
|
|
|
DatasourceType: datasources.DS_PROMETHEUS, |
|
|
|
|
DefaultInterval: 2 * time.Minute, |
|
|
|
|
} |
|
|
|
|
converter, err := NewConverter(cfg) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
t.Run("group labels are merged with alert rule labels", func(t *testing.T) { |
|
|
|
|
promGroup := PrometheusRuleGroup{ |
|
|
|
|
Name: "test-group-1", |
|
|
|
|
Interval: prommodel.Duration(10 * time.Second), |
|
|
|
|
Labels: map[string]string{ |
|
|
|
|
"group_label": "group_value", |
|
|
|
|
"common_label": "group_value", |
|
|
|
|
}, |
|
|
|
|
Rules: []PrometheusRule{ |
|
|
|
|
{ |
|
|
|
|
Alert: "alert-1", |
|
|
|
|
Expr: "cpu_usage > 80", |
|
|
|
|
Labels: map[string]string{ |
|
|
|
|
"rule_label": "rule_value", |
|
|
|
|
"common_label": "rule_value", // rule-level label should take precedence
|
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grafanaGroup, err := converter.PrometheusRulesToGrafana(1, "namespace", promGroup) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Len(t, grafanaGroup.Rules, 1) |
|
|
|
|
|
|
|
|
|
// Check that the labels are merged and the rule label takes precedence
|
|
|
|
|
require.Equal( |
|
|
|
|
t, |
|
|
|
|
map[string]string{ |
|
|
|
|
"group_label": "group_value", |
|
|
|
|
"rule_label": "rule_value", |
|
|
|
|
"common_label": "rule_value", |
|
|
|
|
}, |
|
|
|
|
grafanaGroup.Rules[0].Labels, |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("group labels are merged with recording rule labels", func(t *testing.T) { |
|
|
|
|
promGroup := PrometheusRuleGroup{ |
|
|
|
|
Name: "recording-group", |
|
|
|
|
Interval: prommodel.Duration(10 * time.Second), |
|
|
|
|
Labels: map[string]string{ |
|
|
|
|
"group_label": "group_value", |
|
|
|
|
"common_label": "group_value", |
|
|
|
|
}, |
|
|
|
|
Rules: []PrometheusRule{ |
|
|
|
|
{ |
|
|
|
|
Record: "recording_metric", |
|
|
|
|
Expr: "sum(rate(http_requests_total[5m]))", |
|
|
|
|
Labels: map[string]string{ |
|
|
|
|
"rule_label": "rule_value", |
|
|
|
|
"common_label": "rule_value", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grafanaGroup, err := converter.PrometheusRulesToGrafana(1, "namespace", promGroup) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Len(t, grafanaGroup.Rules, 1) |
|
|
|
|
|
|
|
|
|
// Check that the labels are merged and the rule label takes precedence
|
|
|
|
|
require.Equal( |
|
|
|
|
t, |
|
|
|
|
map[string]string{ |
|
|
|
|
"group_label": "group_value", |
|
|
|
|
"rule_label": "rule_value", |
|
|
|
|
"common_label": "rule_value", |
|
|
|
|
}, |
|
|
|
|
grafanaGroup.Rules[0].Labels, |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("rule with no labels gets group labels", func(t *testing.T) { |
|
|
|
|
promGroup := PrometheusRuleGroup{ |
|
|
|
|
Name: "group-with-labels", |
|
|
|
|
Interval: prommodel.Duration(10 * time.Second), |
|
|
|
|
Labels: map[string]string{ |
|
|
|
|
"group_label1": "group_value1", |
|
|
|
|
"group_label2": "group_value2", |
|
|
|
|
}, |
|
|
|
|
Rules: []PrometheusRule{ |
|
|
|
|
{ |
|
|
|
|
Alert: "alert-no-labels", |
|
|
|
|
Expr: "up == 0", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grafanaGroup, err := converter.PrometheusRulesToGrafana(1, "namespace", promGroup) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Len(t, grafanaGroup.Rules, 1) |
|
|
|
|
|
|
|
|
|
require.Equal(t, promGroup.Labels, grafanaGroup.Rules[0].Labels) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("rule and group with nil labels", func(t *testing.T) { |
|
|
|
|
promGroup := PrometheusRuleGroup{ |
|
|
|
|
Name: "group-no-labels", |
|
|
|
|
Interval: prommodel.Duration(10 * time.Second), |
|
|
|
|
Rules: []PrometheusRule{ |
|
|
|
|
{ |
|
|
|
|
Alert: "alert-no-labels", |
|
|
|
|
Expr: "up == 0", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grafanaGroup, err := converter.PrometheusRulesToGrafana(1, "namespace", promGroup) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Len(t, grafanaGroup.Rules, 1) |
|
|
|
|
require.Empty(t, grafanaGroup.Rules[0].Labels) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPrometheusRulesToGrafana_UID(t *testing.T) { |
|
|
|
|
orgID := int64(1) |
|
|
|
|
namespace := "some-namespace" |
|
|
|
|