|
| 1 | +local signal = import './signal.libsonnet'; |
| 2 | +local test = import 'jsonnetunit/test.libsonnet'; |
| 3 | + |
| 4 | +{ |
| 5 | + queriesSelectorGroupOnly: { |
| 6 | + local signalInit = signal.init( |
| 7 | + filteringSelector=['job="integrations/agent"'], |
| 8 | + groupLabels=['job'], |
| 9 | + instanceLabels=['instance'], |
| 10 | + aggLevel='group', |
| 11 | + ), |
| 12 | + |
| 13 | + local testSignal = signalInit.addSignal( |
| 14 | + name='Test metric', |
| 15 | + nameShort='test', |
| 16 | + type='gauge', |
| 17 | + unit='short', |
| 18 | + description='Test metric', |
| 19 | + sourceMaps=[ |
| 20 | + { |
| 21 | + expr: 'test_metric{%(queriesSelectorGroupOnly)s}', |
| 22 | + exprWrappers: [], |
| 23 | + rangeFunction: 'rate', |
| 24 | + aggFunction: 'avg', |
| 25 | + aggKeepLabels: [], |
| 26 | + infoLabel: null, |
| 27 | + type: 'gauge', |
| 28 | + legendCustomTemplate: null, |
| 29 | + valueMappings: [], |
| 30 | + quantile: 0.95, |
| 31 | + }, |
| 32 | + ], |
| 33 | + ), |
| 34 | + |
| 35 | + local raw = testSignal.asTarget(), |
| 36 | + testResult: test.suite({ |
| 37 | + testExpression: { |
| 38 | + actual: raw.expr, |
| 39 | + expect: 'avg by (job) (\n test_metric{job="integrations/agent",job=~"$job"}\n)', |
| 40 | + }, |
| 41 | + }), |
| 42 | + }, |
| 43 | + |
| 44 | + queriesSelectorFilterOnly: { |
| 45 | + local signalInit = signal.init( |
| 46 | + filteringSelector=['job="integrations/agent"'], |
| 47 | + groupLabels=['job'], |
| 48 | + instanceLabels=['instance'], |
| 49 | + aggLevel='group', |
| 50 | + ), |
| 51 | + |
| 52 | + local testSignal = signalInit.addSignal( |
| 53 | + name='Test metric', |
| 54 | + nameShort='test', |
| 55 | + type='gauge', |
| 56 | + unit='short', |
| 57 | + description='Test metric', |
| 58 | + sourceMaps=[ |
| 59 | + { |
| 60 | + expr: 'test_metric{%(queriesSelectorFilterOnly)s}', |
| 61 | + exprWrappers: [], |
| 62 | + rangeFunction: 'rate', |
| 63 | + aggFunction: 'avg', |
| 64 | + aggKeepLabels: [], |
| 65 | + infoLabel: null, |
| 66 | + type: 'gauge', |
| 67 | + legendCustomTemplate: null, |
| 68 | + valueMappings: [], |
| 69 | + quantile: 0.95, |
| 70 | + }, |
| 71 | + ], |
| 72 | + ), |
| 73 | + |
| 74 | + local raw = testSignal.asTarget(), |
| 75 | + testResult: test.suite({ |
| 76 | + testExpression: { |
| 77 | + actual: raw.expr, |
| 78 | + expect: 'avg by (job) (\n test_metric{job="integrations/agent"}\n)', |
| 79 | + }, |
| 80 | + }), |
| 81 | + }, |
| 82 | + |
| 83 | + // Test asRuleExpression - returns expression without Grafana variables, suitable for alerts |
| 84 | + asRuleExpression: { |
| 85 | + // Test with gauge type |
| 86 | + gauge: { |
| 87 | + local signalInit = signal.init( |
| 88 | + filteringSelector=['job="integrations/agent"'], |
| 89 | + groupLabels=['job'], |
| 90 | + instanceLabels=['instance'], |
| 91 | + aggLevel='group', |
| 92 | + alertsInterval='5m', |
| 93 | + ), |
| 94 | + |
| 95 | + local testSignal = signalInit.addSignal( |
| 96 | + name='Test metric', |
| 97 | + nameShort='test', |
| 98 | + type='gauge', |
| 99 | + unit='short', |
| 100 | + description='Test metric', |
| 101 | + sourceMaps=[ |
| 102 | + { |
| 103 | + expr: 'test_metric{%(queriesSelector)s}', |
| 104 | + exprWrappers: [], |
| 105 | + rangeFunction: 'rate', |
| 106 | + aggFunction: 'avg', |
| 107 | + aggKeepLabels: [], |
| 108 | + infoLabel: null, |
| 109 | + type: 'gauge', |
| 110 | + legendCustomTemplate: null, |
| 111 | + valueMappings: [], |
| 112 | + quantile: 0.95, |
| 113 | + }, |
| 114 | + ], |
| 115 | + ), |
| 116 | + |
| 117 | + local raw = testSignal.asRuleExpression(), |
| 118 | + testResult: test.suite({ |
| 119 | + testExpression: { |
| 120 | + actual: raw, |
| 121 | + expect: 'test_metric{job="integrations/agent"}', |
| 122 | + }, |
| 123 | + }), |
| 124 | + }, |
| 125 | + |
| 126 | + // Test with counter type |
| 127 | + counter: { |
| 128 | + local signalInit = signal.init( |
| 129 | + filteringSelector=['job="integrations/agent"'], |
| 130 | + groupLabels=['job'], |
| 131 | + instanceLabels=['instance'], |
| 132 | + aggLevel='group', |
| 133 | + alertsInterval='5m', |
| 134 | + ), |
| 135 | + |
| 136 | + local testSignal = signalInit.addSignal( |
| 137 | + name='Test counter', |
| 138 | + nameShort='counter', |
| 139 | + type='counter', |
| 140 | + unit='requests', |
| 141 | + description='Test counter', |
| 142 | + sourceMaps=[ |
| 143 | + { |
| 144 | + expr: 'test_counter_total{%(queriesSelector)s}', |
| 145 | + exprWrappers: [], |
| 146 | + rangeFunction: 'increase', |
| 147 | + aggFunction: 'avg', |
| 148 | + aggKeepLabels: [], |
| 149 | + infoLabel: null, |
| 150 | + type: 'counter', |
| 151 | + legendCustomTemplate: null, |
| 152 | + valueMappings: [], |
| 153 | + quantile: 0.95, |
| 154 | + }, |
| 155 | + ], |
| 156 | + ), |
| 157 | + |
| 158 | + local raw = testSignal.asRuleExpression(), |
| 159 | + testResult: test.suite({ |
| 160 | + testExpression: { |
| 161 | + actual: raw, |
| 162 | + expect: 'increase(test_counter_total{job="integrations/agent"}[5m:] offset -5m)', |
| 163 | + }, |
| 164 | + }), |
| 165 | + }, |
| 166 | + }, |
| 167 | + |
| 168 | + // Test withFilteringSelectorMixin - adds additional filtering selector |
| 169 | + withFilteringSelectorMixin: { |
| 170 | + // Test basic filtering selector mixin |
| 171 | + basic: { |
| 172 | + local signalInit = signal.init( |
| 173 | + filteringSelector=['job="integrations/agent"'], |
| 174 | + groupLabels=['job'], |
| 175 | + instanceLabels=['instance'], |
| 176 | + aggLevel='group', |
| 177 | + ), |
| 178 | + |
| 179 | + local testSignal = signalInit.addSignal( |
| 180 | + name='Test metric', |
| 181 | + nameShort='test', |
| 182 | + type='gauge', |
| 183 | + unit='short', |
| 184 | + description='Test metric', |
| 185 | + sourceMaps=[ |
| 186 | + { |
| 187 | + expr: 'test_metric{%(queriesSelector)s}', |
| 188 | + exprWrappers: [], |
| 189 | + rangeFunction: 'rate', |
| 190 | + aggFunction: 'avg', |
| 191 | + aggKeepLabels: [], |
| 192 | + infoLabel: null, |
| 193 | + type: 'gauge', |
| 194 | + legendCustomTemplate: null, |
| 195 | + valueMappings: [], |
| 196 | + quantile: 0.95, |
| 197 | + }, |
| 198 | + ], |
| 199 | + ), |
| 200 | + |
| 201 | + local raw = testSignal.withFilteringSelectorMixin('environment="prod"').asTarget(), |
| 202 | + testResult: test.suite({ |
| 203 | + testExpression: { |
| 204 | + actual: raw.expr, |
| 205 | + expect: 'avg by (job) (\n test_metric{job="integrations/agent",job=~"$job",instance=~"$instance",environment="prod"}\n)', |
| 206 | + }, |
| 207 | + }), |
| 208 | + }, |
| 209 | + |
| 210 | + // Test filtering selector mixin with asRuleExpression |
| 211 | + withAsRuleExpression: { |
| 212 | + local signalInit = signal.init( |
| 213 | + filteringSelector=['job="integrations/agent"'], |
| 214 | + groupLabels=['job'], |
| 215 | + instanceLabels=['instance'], |
| 216 | + aggLevel='group', |
| 217 | + alertsInterval='5m', |
| 218 | + ), |
| 219 | + |
| 220 | + local testSignal = signalInit.addSignal( |
| 221 | + name='Test metric', |
| 222 | + nameShort='test', |
| 223 | + type='gauge', |
| 224 | + unit='short', |
| 225 | + description='Test metric', |
| 226 | + sourceMaps=[ |
| 227 | + { |
| 228 | + expr: 'test_metric{%(queriesSelector)s}', |
| 229 | + exprWrappers: [], |
| 230 | + rangeFunction: 'rate', |
| 231 | + aggFunction: 'avg', |
| 232 | + aggKeepLabels: [], |
| 233 | + infoLabel: null, |
| 234 | + type: 'gauge', |
| 235 | + legendCustomTemplate: null, |
| 236 | + valueMappings: [], |
| 237 | + quantile: 0.95, |
| 238 | + }, |
| 239 | + ], |
| 240 | + ), |
| 241 | + |
| 242 | + local raw = testSignal.withFilteringSelectorMixin('environment="prod"').asRuleExpression(), |
| 243 | + testResult: test.suite({ |
| 244 | + testExpression: { |
| 245 | + actual: raw, |
| 246 | + expect: 'test_metric{job="integrations/agent",environment="prod"}', |
| 247 | + }, |
| 248 | + }), |
| 249 | + }, |
| 250 | + }, |
| 251 | +} |
0 commit comments