|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo/v2" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 7 | +) |
| 8 | + |
| 9 | +var _ = Describe("buildRelabelConfigs", func() { |
| 10 | + It("should return only the rename rule when no allowlist or drops are provided", func() { |
| 11 | + configs := buildRelabelConfigs(nil, nil) |
| 12 | + Expect(configs).To(HaveLen(1)) |
| 13 | + Expect(configs[0].TargetLabel).To(Equal("__name__")) |
| 14 | + Expect(configs[0].Regex).To(Equal("(.*)-(.*)")) |
| 15 | + Expect(configs[0].Replacement).To(Equal("${1}_${2}")) |
| 16 | + }) |
| 17 | + |
| 18 | + It("should return rename + keep when an allowlist is provided", func() { |
| 19 | + allowlist := &metricAllowlistConfig{ |
| 20 | + allowedMetrics: []string{"metric_a", "metric_b"}, |
| 21 | + } |
| 22 | + configs := buildRelabelConfigs(allowlist, nil) |
| 23 | + Expect(configs).To(HaveLen(2)) |
| 24 | + |
| 25 | + Expect(configs[0].Regex).To(Equal("(.*)-(.*)")) |
| 26 | + |
| 27 | + Expect(string(configs[1].Action)).To(Equal("keep")) |
| 28 | + Expect(configs[1].SourceLabels).To(Equal([]monitoringv1.LabelName{"__name__"})) |
| 29 | + Expect(configs[1].Regex).To(Equal("metric_a|metric_b")) |
| 30 | + }) |
| 31 | + |
| 32 | + It("should return rename + keep + drop when allowlist and drops are provided", func() { |
| 33 | + allowlist := &metricAllowlistConfig{ |
| 34 | + allowedMetrics: []string{"metric_a", "metric_b", "metric_c"}, |
| 35 | + } |
| 36 | + drops := []metricDropConfig{ |
| 37 | + { |
| 38 | + labelName: "component_kind", |
| 39 | + labelValue: "transform", |
| 40 | + excludeMetrics: []string{"metric_a", "metric_b"}, |
| 41 | + }, |
| 42 | + } |
| 43 | + configs := buildRelabelConfigs(allowlist, drops) |
| 44 | + Expect(configs).To(HaveLen(3)) |
| 45 | + |
| 46 | + Expect(string(configs[1].Action)).To(Equal("keep")) |
| 47 | + Expect(configs[1].Regex).To(Equal("metric_a|metric_b|metric_c")) |
| 48 | + |
| 49 | + Expect(string(configs[2].Action)).To(Equal("drop")) |
| 50 | + Expect(configs[2].SourceLabels).To(Equal([]monitoringv1.LabelName{"component_kind", "__name__"})) |
| 51 | + Expect(configs[2].Regex).To(Equal("transform;(metric_a|metric_b)")) |
| 52 | + }) |
| 53 | + |
| 54 | + It("should build valid CollectorMinimalRelabelConfigs", func() { |
| 55 | + Expect(CollectorMinimalRelabelConfigs).To(HaveLen(3)) |
| 56 | + Expect(string(CollectorMinimalRelabelConfigs[1].Action)).To(Equal("keep")) |
| 57 | + Expect(string(CollectorMinimalRelabelConfigs[2].Action)).To(Equal("drop")) |
| 58 | + |
| 59 | + keepRegex := CollectorMinimalRelabelConfigs[1].Regex |
| 60 | + for _, m := range collectorMinimalAllowlist.allowedMetrics { |
| 61 | + Expect(keepRegex).To(ContainSubstring(m), "missing metric in keep regex: %s", m) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + It("should build valid LFMEMinimalRelabelConfigs", func() { |
| 66 | + Expect(LFMEMinimalRelabelConfigs).To(HaveLen(2)) |
| 67 | + Expect(string(LFMEMinimalRelabelConfigs[1].Action)).To(Equal("keep")) |
| 68 | + Expect(LFMEMinimalRelabelConfigs[1].Regex).To(Equal("log_logged_bytes_total")) |
| 69 | + }) |
| 70 | + |
| 71 | + It("should build FullRelabelConfigs with only the rename rule", func() { |
| 72 | + Expect(FullRelabelConfigs).To(HaveLen(1)) |
| 73 | + Expect(FullRelabelConfigs[0].Regex).To(Equal("(.*)-(.*)")) |
| 74 | + }) |
| 75 | + |
| 76 | + It("should return only the rename rule when allowlist has empty metrics", func() { |
| 77 | + allowlist := &metricAllowlistConfig{ |
| 78 | + allowedMetrics: []string{}, |
| 79 | + } |
| 80 | + configs := buildRelabelConfigs(allowlist, nil) |
| 81 | + Expect(configs).To(HaveLen(1)) |
| 82 | + Expect(configs[0].TargetLabel).To(Equal("__name__")) |
| 83 | + }) |
| 84 | + |
| 85 | + It("should return rename + drop when only drop configs are provided", func() { |
| 86 | + drops := []metricDropConfig{ |
| 87 | + { |
| 88 | + labelName: "component_kind", |
| 89 | + labelValue: "transform", |
| 90 | + excludeMetrics: []string{"metric_a"}, |
| 91 | + }, |
| 92 | + } |
| 93 | + configs := buildRelabelConfigs(nil, drops) |
| 94 | + Expect(configs).To(HaveLen(2)) |
| 95 | + |
| 96 | + Expect(configs[0].Regex).To(Equal("(.*)-(.*)")) |
| 97 | + Expect(string(configs[1].Action)).To(Equal("drop")) |
| 98 | + Expect(configs[1].SourceLabels).To(Equal([]monitoringv1.LabelName{"component_kind", "__name__"})) |
| 99 | + Expect(configs[1].Regex).To(Equal("transform;(metric_a)")) |
| 100 | + }) |
| 101 | +}) |
0 commit comments