github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/configs/legacy_promql/testdata/functions.test (about) 1 # Testdata for resets() and changes(). 2 load 5m 3 http_requests{path="/foo"} 1 2 3 0 1 0 0 1 2 0 4 http_requests{path="/bar"} 1 2 3 4 5 1 2 3 4 5 5 http_requests{path="/biz"} 0 0 0 0 0 1 1 1 1 1 6 7 # Tests for resets(). 8 eval instant at 50m resets(http_requests[5m]) 9 {path="/foo"} 0 10 {path="/bar"} 0 11 {path="/biz"} 0 12 13 eval instant at 50m resets(http_requests[20m]) 14 {path="/foo"} 1 15 {path="/bar"} 0 16 {path="/biz"} 0 17 18 eval instant at 50m resets(http_requests[30m]) 19 {path="/foo"} 2 20 {path="/bar"} 1 21 {path="/biz"} 0 22 23 eval instant at 50m resets(http_requests[50m]) 24 {path="/foo"} 3 25 {path="/bar"} 1 26 {path="/biz"} 0 27 28 eval instant at 50m resets(nonexistent_metric[50m]) 29 30 # Tests for changes(). 31 eval instant at 50m changes(http_requests[5m]) 32 {path="/foo"} 0 33 {path="/bar"} 0 34 {path="/biz"} 0 35 36 eval instant at 50m changes(http_requests[20m]) 37 {path="/foo"} 3 38 {path="/bar"} 3 39 {path="/biz"} 0 40 41 eval instant at 50m changes(http_requests[30m]) 42 {path="/foo"} 4 43 {path="/bar"} 5 44 {path="/biz"} 1 45 46 eval instant at 50m changes(http_requests[50m]) 47 {path="/foo"} 8 48 {path="/bar"} 9 49 {path="/biz"} 1 50 51 eval instant at 50m changes(nonexistent_metric[50m]) 52 53 clear 54 55 load 5m 56 x{a="b"} NaN NaN NaN 57 x{a="c"} 0 NaN 0 58 59 eval instant at 15m changes(x[15m]) 60 {a="b"} 0 61 {a="c"} 2 62 63 clear 64 65 # Tests for increase(). 66 load 5m 67 http_requests{path="/foo"} 0+10x10 68 http_requests{path="/bar"} 0+10x5 0+10x5 69 70 # Tests for increase(). 71 eval instant at 50m increase(http_requests[50m]) 72 {path="/foo"} 100 73 {path="/bar"} 90 74 75 eval instant at 50m increase(http_requests[100m]) 76 {path="/foo"} 100 77 {path="/bar"} 90 78 79 clear 80 81 # Test for increase() with counter reset. 82 # When the counter is reset, it always starts at 0. 83 # So the sequence 3 2 (decreasing counter = reset) is interpreted the same as 3 0 1 2. 84 # Prometheus assumes it missed the intermediate values 0 and 1. 85 load 5m 86 http_requests{path="/foo"} 0 1 2 3 2 3 4 87 88 eval instant at 30m increase(http_requests[30m]) 89 {path="/foo"} 7 90 91 clear 92 93 # Tests for irate(). 94 load 5m 95 http_requests{path="/foo"} 0+10x10 96 http_requests{path="/bar"} 0+10x5 0+10x5 97 98 eval instant at 50m irate(http_requests[50m]) 99 {path="/foo"} .03333333333333333333 100 {path="/bar"} .03333333333333333333 101 102 # Counter reset. 103 eval instant at 30m irate(http_requests[50m]) 104 {path="/foo"} .03333333333333333333 105 {path="/bar"} 0 106 107 clear 108 109 # Tests for delta(). 110 load 5m 111 http_requests{path="/foo"} 0 50 100 150 200 112 http_requests{path="/bar"} 200 150 100 50 0 113 114 eval instant at 20m delta(http_requests[20m]) 115 {path="/foo"} 200 116 {path="/bar"} -200 117 118 clear 119 120 # Tests for idelta(). 121 load 5m 122 http_requests{path="/foo"} 0 50 100 150 123 http_requests{path="/bar"} 0 50 100 50 124 125 eval instant at 20m idelta(http_requests[20m]) 126 {path="/foo"} 50 127 {path="/bar"} -50 128 129 clear 130 131 # Tests for deriv() and predict_linear(). 132 load 5m 133 testcounter_reset_middle 0+10x4 0+10x5 134 http_requests{job="app-server", instance="1", group="canary"} 0+80x10 135 136 # deriv should return the same as rate in simple cases. 137 eval instant at 50m rate(http_requests{group="canary", instance="1", job="app-server"}[50m]) 138 {group="canary", instance="1", job="app-server"} 0.26666666666666666 139 140 eval instant at 50m deriv(http_requests{group="canary", instance="1", job="app-server"}[50m]) 141 {group="canary", instance="1", job="app-server"} 0.26666666666666666 142 143 # deriv should return correct result. 144 eval instant at 50m deriv(testcounter_reset_middle[100m]) 145 {} 0.010606060606060607 146 147 # predict_linear should return correct result. 148 # X/s = [ 0, 300, 600, 900,1200,1500,1800,2100,2400,2700,3000] 149 # Y = [ 0, 10, 20, 30, 40, 0, 10, 20, 30, 40, 50] 150 # sumX = 16500 151 # sumY = 250 152 # sumXY = 480000 153 # sumX2 = 34650000 154 # n = 11 155 # covXY = 105000 156 # varX = 9900000 157 # slope = 0.010606060606060607 158 # intercept at t=0: 6.818181818181818 159 # intercept at t=3000: 38.63636363636364 160 # intercept at t=3000+3600: 76.81818181818181 161 eval instant at 50m predict_linear(testcounter_reset_middle[100m], 3600) 162 {} 76.81818181818181 163 164 # With http_requests, there is a sample value exactly at the end of 165 # the range, and it has exactly the predicted value, so predict_linear 166 # can be emulated with deriv. 167 eval instant at 50m predict_linear(http_requests[50m], 3600) - (http_requests + deriv(http_requests[50m]) * 3600) 168 {group="canary", instance="1", job="app-server"} 0 169 170 clear 171 172 # Tests for label_replace. 173 load 5m 174 testmetric{src="source-value-10",dst="original-destination-value"} 0 175 testmetric{src="source-value-20",dst="original-destination-value"} 1 176 177 # label_replace does a full-string match and replace. 178 eval instant at 0m label_replace(testmetric, "dst", "destination-value-$1", "src", "source-value-(.*)") 179 testmetric{src="source-value-10",dst="destination-value-10"} 0 180 testmetric{src="source-value-20",dst="destination-value-20"} 1 181 182 # label_replace does not do a sub-string match. 183 eval instant at 0m label_replace(testmetric, "dst", "destination-value-$1", "src", "value-(.*)") 184 testmetric{src="source-value-10",dst="original-destination-value"} 0 185 testmetric{src="source-value-20",dst="original-destination-value"} 1 186 187 # label_replace works with multiple capture groups. 188 eval instant at 0m label_replace(testmetric, "dst", "$1-value-$2", "src", "(.*)-value-(.*)") 189 testmetric{src="source-value-10",dst="source-value-10"} 0 190 testmetric{src="source-value-20",dst="source-value-20"} 1 191 192 # label_replace does not overwrite the destination label if the source label 193 # does not exist. 194 eval instant at 0m label_replace(testmetric, "dst", "value-$1", "nonexistent-src", "source-value-(.*)") 195 testmetric{src="source-value-10",dst="original-destination-value"} 0 196 testmetric{src="source-value-20",dst="original-destination-value"} 1 197 198 # label_replace overwrites the destination label if the source label is empty, 199 # but matched. 200 eval instant at 0m label_replace(testmetric, "dst", "value-$1", "nonexistent-src", "(.*)") 201 testmetric{src="source-value-10",dst="value-"} 0 202 testmetric{src="source-value-20",dst="value-"} 1 203 204 # label_replace does not overwrite the destination label if the source label 205 # is not matched. 206 eval instant at 0m label_replace(testmetric, "dst", "value-$1", "src", "non-matching-regex") 207 testmetric{src="source-value-10",dst="original-destination-value"} 0 208 testmetric{src="source-value-20",dst="original-destination-value"} 1 209 210 # label_replace drops labels that are set to empty values. 211 eval instant at 0m label_replace(testmetric, "dst", "", "dst", ".*") 212 testmetric{src="source-value-10"} 0 213 testmetric{src="source-value-20"} 1 214 215 # label_replace fails when the regex is invalid. 216 eval_fail instant at 0m label_replace(testmetric, "dst", "value-$1", "src", "(.*") 217 218 # label_replace fails when the destination label name is not a valid Prometheus label name. 219 eval_fail instant at 0m label_replace(testmetric, "invalid-label-name", "", "src", "(.*)") 220 221 # label_replace fails when there would be duplicated identical output label sets. 222 eval_fail instant at 0m label_replace(testmetric, "src", "", "", "") 223 224 clear 225 226 # Tests for vector, time and timestamp. 227 load 10s 228 metric 1 1 229 230 eval instant at 0s timestamp(metric) 231 {} 0 232 233 eval instant at 5s timestamp(metric) 234 {} 0 235 236 eval instant at 10s timestamp(metric) 237 {} 10 238 239 # Tests for label_join. 240 load 5m 241 testmetric{src="a",src1="b",src2="c",dst="original-destination-value"} 0 242 testmetric{src="d",src1="e",src2="f",dst="original-destination-value"} 1 243 244 # label_join joins all src values in order. 245 eval instant at 0m label_join(testmetric, "dst", "-", "src", "src1", "src2") 246 testmetric{src="a",src1="b",src2="c",dst="a-b-c"} 0 247 testmetric{src="d",src1="e",src2="f",dst="d-e-f"} 1 248 249 # label_join treats non existent src labels as empty strings. 250 eval instant at 0m label_join(testmetric, "dst", "-", "src", "src3", "src1") 251 testmetric{src="a",src1="b",src2="c",dst="a--b"} 0 252 testmetric{src="d",src1="e",src2="f",dst="d--e"} 1 253 254 # label_join overwrites the destination label even if the resulting dst label is empty string 255 eval instant at 0m label_join(testmetric, "dst", "", "emptysrc", "emptysrc1", "emptysrc2") 256 testmetric{src="a",src1="b",src2="c"} 0 257 testmetric{src="d",src1="e",src2="f"} 1 258 259 # test without src label for label_join 260 eval instant at 0m label_join(testmetric, "dst", ", ") 261 testmetric{src="a",src1="b",src2="c"} 0 262 testmetric{src="d",src1="e",src2="f"} 1 263 264 # test without dst label for label_join 265 load 5m 266 testmetric1{src="foo",src1="bar",src2="foobar"} 0 267 testmetric1{src="fizz",src1="buzz",src2="fizzbuzz"} 1 268 269 # label_join creates dst label if not present. 270 eval instant at 0m label_join(testmetric1, "dst", ", ", "src", "src1", "src2") 271 testmetric1{src="foo",src1="bar",src2="foobar",dst="foo, bar, foobar"} 0 272 testmetric1{src="fizz",src1="buzz",src2="fizzbuzz",dst="fizz, buzz, fizzbuzz"} 1 273 274 clear 275 276 # Tests for vector. 277 eval instant at 0m vector(1) 278 {} 1 279 280 eval instant at 0s vector(time()) 281 {} 0 282 283 eval instant at 5s vector(time()) 284 {} 5 285 286 eval instant at 60m vector(time()) 287 {} 3600 288 289 290 # Tests for clamp_max and clamp_min(). 291 load 5m 292 test_clamp{src="clamp-a"} -50 293 test_clamp{src="clamp-b"} 0 294 test_clamp{src="clamp-c"} 100 295 296 eval instant at 0m clamp_max(test_clamp, 75) 297 {src="clamp-a"} -50 298 {src="clamp-b"} 0 299 {src="clamp-c"} 75 300 301 eval instant at 0m clamp_min(test_clamp, -25) 302 {src="clamp-a"} -25 303 {src="clamp-b"} 0 304 {src="clamp-c"} 100 305 306 eval instant at 0m clamp_max(clamp_min(test_clamp, -20), 70) 307 {src="clamp-a"} -20 308 {src="clamp-b"} 0 309 {src="clamp-c"} 70 310 311 312 # Tests for sort/sort_desc. 313 clear 314 load 5m 315 http_requests{job="api-server", instance="0", group="production"} 0+10x10 316 http_requests{job="api-server", instance="1", group="production"} 0+20x10 317 http_requests{job="api-server", instance="0", group="canary"} 0+30x10 318 http_requests{job="api-server", instance="1", group="canary"} 0+40x10 319 http_requests{job="api-server", instance="2", group="canary"} NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 320 http_requests{job="app-server", instance="0", group="production"} 0+50x10 321 http_requests{job="app-server", instance="1", group="production"} 0+60x10 322 http_requests{job="app-server", instance="0", group="canary"} 0+70x10 323 http_requests{job="app-server", instance="1", group="canary"} 0+80x10 324 325 eval_ordered instant at 50m sort(http_requests) 326 http_requests{group="production", instance="0", job="api-server"} 100 327 http_requests{group="production", instance="1", job="api-server"} 200 328 http_requests{group="canary", instance="0", job="api-server"} 300 329 http_requests{group="canary", instance="1", job="api-server"} 400 330 http_requests{group="production", instance="0", job="app-server"} 500 331 http_requests{group="production", instance="1", job="app-server"} 600 332 http_requests{group="canary", instance="0", job="app-server"} 700 333 http_requests{group="canary", instance="1", job="app-server"} 800 334 http_requests{group="canary", instance="2", job="api-server"} NaN 335 336 eval_ordered instant at 50m sort_desc(http_requests) 337 http_requests{group="canary", instance="1", job="app-server"} 800 338 http_requests{group="canary", instance="0", job="app-server"} 700 339 http_requests{group="production", instance="1", job="app-server"} 600 340 http_requests{group="production", instance="0", job="app-server"} 500 341 http_requests{group="canary", instance="1", job="api-server"} 400 342 http_requests{group="canary", instance="0", job="api-server"} 300 343 http_requests{group="production", instance="1", job="api-server"} 200 344 http_requests{group="production", instance="0", job="api-server"} 100 345 http_requests{group="canary", instance="2", job="api-server"} NaN 346 347 # Tests for holt_winters 348 clear 349 350 # positive trends 351 load 10s 352 http_requests{job="api-server", instance="0", group="production"} 0+10x1000 100+30x1000 353 http_requests{job="api-server", instance="1", group="production"} 0+20x1000 200+30x1000 354 http_requests{job="api-server", instance="0", group="canary"} 0+30x1000 300+80x1000 355 http_requests{job="api-server", instance="1", group="canary"} 0+40x2000 356 357 eval instant at 8000s holt_winters(http_requests[1m], 0.01, 0.1) 358 {job="api-server", instance="0", group="production"} 8000 359 {job="api-server", instance="1", group="production"} 16000 360 {job="api-server", instance="0", group="canary"} 24000 361 {job="api-server", instance="1", group="canary"} 32000 362 363 # negative trends 364 clear 365 load 10s 366 http_requests{job="api-server", instance="0", group="production"} 8000-10x1000 367 http_requests{job="api-server", instance="1", group="production"} 0-20x1000 368 http_requests{job="api-server", instance="0", group="canary"} 0+30x1000 300-80x1000 369 http_requests{job="api-server", instance="1", group="canary"} 0-40x1000 0+40x1000 370 371 eval instant at 8000s holt_winters(http_requests[1m], 0.01, 0.1) 372 {job="api-server", instance="0", group="production"} 0 373 {job="api-server", instance="1", group="production"} -16000 374 {job="api-server", instance="0", group="canary"} 24000 375 {job="api-server", instance="1", group="canary"} -32000 376 377 # Tests for stddev_over_time and stdvar_over_time. 378 clear 379 load 10s 380 metric 0 8 8 2 3 381 382 eval instant at 1m stdvar_over_time(metric[1m]) 383 {} 10.56 384 385 eval instant at 1m stddev_over_time(metric[1m]) 386 {} 3.249615 387 388 # Tests for quantile_over_time 389 clear 390 391 load 10s 392 data{test="two samples"} 0 1 393 data{test="three samples"} 0 1 2 394 data{test="uneven samples"} 0 1 4 395 396 eval instant at 1m quantile_over_time(0, data[1m]) 397 {test="two samples"} 0 398 {test="three samples"} 0 399 {test="uneven samples"} 0 400 401 eval instant at 1m quantile_over_time(0.5, data[1m]) 402 {test="two samples"} 0.5 403 {test="three samples"} 1 404 {test="uneven samples"} 1 405 406 eval instant at 1m quantile_over_time(0.75, data[1m]) 407 {test="two samples"} 0.75 408 {test="three samples"} 1.5 409 {test="uneven samples"} 2.5 410 411 eval instant at 1m quantile_over_time(0.8, data[1m]) 412 {test="two samples"} 0.8 413 {test="three samples"} 1.6 414 {test="uneven samples"} 2.8 415 416 eval instant at 1m quantile_over_time(1, data[1m]) 417 {test="two samples"} 1 418 {test="three samples"} 2 419 {test="uneven samples"} 4 420 421 eval instant at 1m quantile_over_time(-1, data[1m]) 422 {test="two samples"} -Inf 423 {test="three samples"} -Inf 424 {test="uneven samples"} -Inf 425 426 eval instant at 1m quantile_over_time(2, data[1m]) 427 {test="two samples"} +Inf 428 {test="three samples"} +Inf 429 {test="uneven samples"} +Inf 430 431 clear 432 433 # Test time-related functions. 434 eval instant at 0m year() 435 {} 1970 436 437 eval instant at 1ms time() 438 0.001 439 440 eval instant at 0m year(vector(1136239445)) 441 {} 2006 442 443 eval instant at 0m month() 444 {} 1 445 446 eval instant at 0m month(vector(1136239445)) 447 {} 1 448 449 eval instant at 0m day_of_month() 450 {} 1 451 452 eval instant at 0m day_of_month(vector(1136239445)) 453 {} 2 454 455 # Thursday. 456 eval instant at 0m day_of_week() 457 {} 4 458 459 eval instant at 0m day_of_week(vector(1136239445)) 460 {} 1 461 462 eval instant at 0m hour() 463 {} 0 464 465 eval instant at 0m hour(vector(1136239445)) 466 {} 22 467 468 eval instant at 0m minute() 469 {} 0 470 471 eval instant at 0m minute(vector(1136239445)) 472 {} 4 473 474 # 2008-12-31 23:59:59 just before leap second. 475 eval instant at 0m year(vector(1230767999)) 476 {} 2008 477 478 # 2009-01-01 00:00:00 just after leap second. 479 eval instant at 0m year(vector(1230768000)) 480 {} 2009 481 482 # 2016-02-29 23:59:59 February 29th in leap year. 483 eval instant at 0m month(vector(1456790399)) + day_of_month(vector(1456790399)) / 100 484 {} 2.29 485 486 # 2016-03-01 00:00:00 March 1st in leap year. 487 eval instant at 0m month(vector(1456790400)) + day_of_month(vector(1456790400)) / 100 488 {} 3.01 489 490 # February 1st 2016 in leap year. 491 eval instant at 0m days_in_month(vector(1454284800)) 492 {} 29 493 494 # February 1st 2017 not in leap year. 495 eval instant at 0m days_in_month(vector(1485907200)) 496 {} 28 497