| 1 |
// Copyright 2020 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package promlint_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
dto "github.com/prometheus/client_model/go" |
| 24 |
|
| 25 |
"github.com/prometheus/client_golang/prometheus/testutil/promlint" |
| 26 |
) |
| 27 |
|
| 28 |
type test struct { |
| 29 |
name string |
| 30 |
in string |
| 31 |
problems []promlint.Problem |
| 32 |
} |
| 33 |
|
| 34 |
func TestLintNoHelpText(t *testing.T) { |
| 35 |
const msg = "no help text" |
| 36 |
|
| 37 |
tests := []test{ |
| 38 |
{ |
| 39 |
name: "no help", |
| 40 |
in: ` |
| 41 |
# TYPE go_goroutines gauge |
| 42 |
go_goroutines 24 |
| 43 |
`, |
| 44 |
problems: []promlint.Problem{{ |
| 45 |
Metric: "go_goroutines", |
| 46 |
Text: msg, |
| 47 |
}}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "empty help", |
| 51 |
in: ` |
| 52 |
# HELP go_goroutines |
| 53 |
# TYPE go_goroutines gauge |
| 54 |
go_goroutines 24 |
| 55 |
`, |
| 56 |
problems: []promlint.Problem{{ |
| 57 |
Metric: "go_goroutines", |
| 58 |
Text: msg, |
| 59 |
}}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "no help and empty help", |
| 63 |
in: ` |
| 64 |
# HELP go_goroutines |
| 65 |
# TYPE go_goroutines gauge |
| 66 |
go_goroutines 24 |
| 67 |
# TYPE go_threads gauge |
| 68 |
go_threads 10 |
| 69 |
`, |
| 70 |
problems: []promlint.Problem{ |
| 71 |
{ |
| 72 |
Metric: "go_goroutines", |
| 73 |
Text: msg, |
| 74 |
}, |
| 75 |
{ |
| 76 |
Metric: "go_threads", |
| 77 |
Text: msg, |
| 78 |
}, |
| 79 |
}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "OK", |
| 83 |
in: ` |
| 84 |
# HELP go_goroutines Number of goroutines that currently exist. |
| 85 |
# TYPE go_goroutines gauge |
| 86 |
go_goroutines 24 |
| 87 |
`, |
| 88 |
}, |
| 89 |
} |
| 90 |
runTests(t, tests) |
| 91 |
} |
| 92 |
|
| 93 |
func TestLintMetricUnits(t *testing.T) { |
| 94 |
tests := []struct { |
| 95 |
name string |
| 96 |
in string |
| 97 |
problems []promlint.Problem |
| 98 |
}{ |
| 99 |
// good cases. |
| 100 |
{ |
| 101 |
name: "amperes", |
| 102 |
in: ` |
| 103 |
# HELP x_amperes Test metric. |
| 104 |
# TYPE x_amperes untyped |
| 105 |
x_amperes 10 |
| 106 |
`, |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "bytes", |
| 110 |
in: ` |
| 111 |
# HELP x_bytes Test metric. |
| 112 |
# TYPE x_bytes untyped |
| 113 |
x_bytes 10 |
| 114 |
`, |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "grams", |
| 118 |
in: ` |
| 119 |
# HELP x_grams Test metric. |
| 120 |
# TYPE x_grams untyped |
| 121 |
x_grams 10 |
| 122 |
`, |
| 123 |
}, |
| 124 |
{ |
| 125 |
name: "celsius", |
| 126 |
in: ` |
| 127 |
# HELP x_celsius Test metric. |
| 128 |
# TYPE x_celsius untyped |
| 129 |
x_celsius 10 |
| 130 |
`, |
| 131 |
}, |
| 132 |
{ |
| 133 |
name: "meters", |
| 134 |
in: ` |
| 135 |
# HELP x_meters Test metric. |
| 136 |
# TYPE x_meters untyped |
| 137 |
x_meters 10 |
| 138 |
`, |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "metres", |
| 142 |
in: ` |
| 143 |
# HELP x_metres Test metric. |
| 144 |
# TYPE x_metres untyped |
| 145 |
x_metres 10 |
| 146 |
`, |
| 147 |
}, |
| 148 |
{ |
| 149 |
name: "moles", |
| 150 |
in: ` |
| 151 |
# HELP x_moles Test metric. |
| 152 |
# TYPE x_moles untyped |
| 153 |
x_moles 10 |
| 154 |
`, |
| 155 |
}, |
| 156 |
{ |
| 157 |
name: "seconds", |
| 158 |
in: ` |
| 159 |
# HELP x_seconds Test metric. |
| 160 |
# TYPE x_seconds untyped |
| 161 |
x_seconds 10 |
| 162 |
`, |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "joules", |
| 166 |
in: ` |
| 167 |
# HELP x_joules Test metric. |
| 168 |
# TYPE x_joules untyped |
| 169 |
x_joules 10 |
| 170 |
`, |
| 171 |
}, |
| 172 |
{ |
| 173 |
name: "kelvin", |
| 174 |
in: ` |
| 175 |
# HELP x_kelvin Test metric. |
| 176 |
# TYPE x_kelvin untyped |
| 177 |
x_kelvin 10 |
| 178 |
`, |
| 179 |
}, |
| 180 |
// bad cases. |
| 181 |
{ |
| 182 |
name: "milliamperes", |
| 183 |
in: ` |
| 184 |
# HELP x_milliamperes Test metric. |
| 185 |
# TYPE x_milliamperes untyped |
| 186 |
x_milliamperes 10 |
| 187 |
`, |
| 188 |
problems: []promlint.Problem{{ |
| 189 |
Metric: "x_milliamperes", |
| 190 |
Text: `use base unit "amperes" instead of "milliamperes"`, |
| 191 |
}}, |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "gigabytes", |
| 195 |
in: ` |
| 196 |
# HELP x_gigabytes Test metric. |
| 197 |
# TYPE x_gigabytes untyped |
| 198 |
x_gigabytes 10 |
| 199 |
`, |
| 200 |
problems: []promlint.Problem{{ |
| 201 |
Metric: "x_gigabytes", |
| 202 |
Text: `use base unit "bytes" instead of "gigabytes"`, |
| 203 |
}}, |
| 204 |
}, |
| 205 |
{ |
| 206 |
name: "kilograms", |
| 207 |
in: ` |
| 208 |
# HELP x_kilograms Test metric. |
| 209 |
# TYPE x_kilograms untyped |
| 210 |
x_kilograms 10 |
| 211 |
`, |
| 212 |
problems: []promlint.Problem{{ |
| 213 |
Metric: "x_kilograms", |
| 214 |
Text: `use base unit "grams" instead of "kilograms"`, |
| 215 |
}}, |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "nanocelsius", |
| 219 |
in: ` |
| 220 |
# HELP x_nanocelsius Test metric. |
| 221 |
# TYPE x_nanocelsius untyped |
| 222 |
x_nanocelsius 10 |
| 223 |
`, |
| 224 |
problems: []promlint.Problem{{ |
| 225 |
Metric: "x_nanocelsius", |
| 226 |
Text: `use base unit "celsius" instead of "nanocelsius"`, |
| 227 |
}}, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "kilometers", |
| 231 |
in: ` |
| 232 |
# HELP x_kilometers Test metric. |
| 233 |
# TYPE x_kilometers untyped |
| 234 |
x_kilometers 10 |
| 235 |
`, |
| 236 |
problems: []promlint.Problem{{ |
| 237 |
Metric: "x_kilometers", |
| 238 |
Text: `use base unit "meters" instead of "kilometers"`, |
| 239 |
}}, |
| 240 |
}, |
| 241 |
{ |
| 242 |
name: "picometers", |
| 243 |
in: ` |
| 244 |
# HELP x_picometers Test metric. |
| 245 |
# TYPE x_picometers untyped |
| 246 |
x_picometers 10 |
| 247 |
`, |
| 248 |
problems: []promlint.Problem{{ |
| 249 |
Metric: "x_picometers", |
| 250 |
Text: `use base unit "meters" instead of "picometers"`, |
| 251 |
}}, |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "microseconds", |
| 255 |
in: ` |
| 256 |
# HELP x_microseconds Test metric. |
| 257 |
# TYPE x_microseconds untyped |
| 258 |
x_microseconds 10 |
| 259 |
`, |
| 260 |
problems: []promlint.Problem{{ |
| 261 |
Metric: "x_microseconds", |
| 262 |
Text: `use base unit "seconds" instead of "microseconds"`, |
| 263 |
}}, |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "minutes", |
| 267 |
in: ` |
| 268 |
# HELP x_minutes Test metric. |
| 269 |
# TYPE x_minutes untyped |
| 270 |
x_minutes 10 |
| 271 |
`, |
| 272 |
problems: []promlint.Problem{{ |
| 273 |
Metric: "x_minutes", |
| 274 |
Text: `use base unit "seconds" instead of "minutes"`, |
| 275 |
}}, |
| 276 |
}, |
| 277 |
{ |
| 278 |
name: "hours", |
| 279 |
in: ` |
| 280 |
# HELP x_hours Test metric. |
| 281 |
# TYPE x_hours untyped |
| 282 |
x_hours 10 |
| 283 |
`, |
| 284 |
problems: []promlint.Problem{{ |
| 285 |
Metric: "x_hours", |
| 286 |
Text: `use base unit "seconds" instead of "hours"`, |
| 287 |
}}, |
| 288 |
}, |
| 289 |
{ |
| 290 |
name: "days", |
| 291 |
in: ` |
| 292 |
# HELP x_days Test metric. |
| 293 |
# TYPE x_days untyped |
| 294 |
x_days 10 |
| 295 |
`, |
| 296 |
problems: []promlint.Problem{{ |
| 297 |
Metric: "x_days", |
| 298 |
Text: `use base unit "seconds" instead of "days"`, |
| 299 |
}}, |
| 300 |
}, |
| 301 |
{ |
| 302 |
name: "kelvins", |
| 303 |
in: ` |
| 304 |
# HELP x_kelvins Test metric. |
| 305 |
# TYPE x_kelvins untyped |
| 306 |
x_kelvins 10 |
| 307 |
`, |
| 308 |
problems: []promlint.Problem{{ |
| 309 |
Metric: "x_kelvins", |
| 310 |
Text: `use base unit "kelvin" instead of "kelvins"`, |
| 311 |
}}, |
| 312 |
}, |
| 313 |
{ |
| 314 |
name: "fahrenheit", |
| 315 |
in: ` |
| 316 |
# HELP thermometers_fahrenheit Test metric. |
| 317 |
# TYPE thermometers_fahrenheit untyped |
| 318 |
thermometers_fahrenheit 10 |
| 319 |
`, |
| 320 |
problems: []promlint.Problem{{ |
| 321 |
Metric: "thermometers_fahrenheit", |
| 322 |
Text: `use base unit "celsius" instead of "fahrenheit"`, |
| 323 |
}}, |
| 324 |
}, |
| 325 |
{ |
| 326 |
name: "rankine", |
| 327 |
in: ` |
| 328 |
# HELP thermometers_rankine Test metric. |
| 329 |
# TYPE thermometers_rankine untyped |
| 330 |
thermometers_rankine 10 |
| 331 |
`, |
| 332 |
problems: []promlint.Problem{{ |
| 333 |
Metric: "thermometers_rankine", |
| 334 |
Text: `use base unit "celsius" instead of "rankine"`, |
| 335 |
}}, |
| 336 |
}, |
| 337 |
{ |
| 338 |
name: "inches", |
| 339 |
in: ` |
| 340 |
# HELP x_inches Test metric. |
| 341 |
# TYPE x_inches untyped |
| 342 |
x_inches 10 |
| 343 |
`, |
| 344 |
problems: []promlint.Problem{{ |
| 345 |
Metric: "x_inches", |
| 346 |
Text: `use base unit "meters" instead of "inches"`, |
| 347 |
}}, |
| 348 |
}, |
| 349 |
{ |
| 350 |
name: "yards", |
| 351 |
in: ` |
| 352 |
# HELP x_yards Test metric. |
| 353 |
# TYPE x_yards untyped |
| 354 |
x_yards 10 |
| 355 |
`, |
| 356 |
problems: []promlint.Problem{{ |
| 357 |
Metric: "x_yards", |
| 358 |
Text: `use base unit "meters" instead of "yards"`, |
| 359 |
}}, |
| 360 |
}, |
| 361 |
{ |
| 362 |
name: "miles", |
| 363 |
in: ` |
| 364 |
# HELP x_miles Test metric. |
| 365 |
# TYPE x_miles untyped |
| 366 |
x_miles 10 |
| 367 |
`, |
| 368 |
problems: []promlint.Problem{{ |
| 369 |
Metric: "x_miles", |
| 370 |
Text: `use base unit "meters" instead of "miles"`, |
| 371 |
}}, |
| 372 |
}, |
| 373 |
{ |
| 374 |
name: "bits", |
| 375 |
in: ` |
| 376 |
# HELP x_bits Test metric. |
| 377 |
# TYPE x_bits untyped |
| 378 |
x_bits 10 |
| 379 |
`, |
| 380 |
problems: []promlint.Problem{{ |
| 381 |
Metric: "x_bits", |
| 382 |
Text: `use base unit "bytes" instead of "bits"`, |
| 383 |
}}, |
| 384 |
}, |
| 385 |
{ |
| 386 |
name: "calories", |
| 387 |
in: ` |
| 388 |
# HELP x_calories Test metric. |
| 389 |
# TYPE x_calories untyped |
| 390 |
x_calories 10 |
| 391 |
`, |
| 392 |
problems: []promlint.Problem{{ |
| 393 |
Metric: "x_calories", |
| 394 |
Text: `use base unit "joules" instead of "calories"`, |
| 395 |
}}, |
| 396 |
}, |
| 397 |
{ |
| 398 |
name: "pounds", |
| 399 |
in: ` |
| 400 |
# HELP x_pounds Test metric. |
| 401 |
# TYPE x_pounds untyped |
| 402 |
x_pounds 10 |
| 403 |
`, |
| 404 |
problems: []promlint.Problem{{ |
| 405 |
Metric: "x_pounds", |
| 406 |
Text: `use base unit "grams" instead of "pounds"`, |
| 407 |
}}, |
| 408 |
}, |
| 409 |
{ |
| 410 |
name: "ounces", |
| 411 |
in: ` |
| 412 |
# HELP x_ounces Test metric. |
| 413 |
# TYPE x_ounces untyped |
| 414 |
x_ounces 10 |
| 415 |
`, |
| 416 |
problems: []promlint.Problem{{ |
| 417 |
Metric: "x_ounces", |
| 418 |
Text: `use base unit "grams" instead of "ounces"`, |
| 419 |
}}, |
| 420 |
}, |
| 421 |
} |
| 422 |
|
| 423 |
for _, tt := range tests { |
| 424 |
t.Run(tt.name, func(t *testing.T) { |
| 425 |
l := promlint.New(strings.NewReader(tt.in)) |
| 426 |
|
| 427 |
problems, err := l.Lint() |
| 428 |
if err != nil { |
| 429 |
t.Fatalf("unexpected error: %v", err) |
| 430 |
} |
| 431 |
|
| 432 |
if want, got := tt.problems, problems; !reflect.DeepEqual(want, got) { |
| 433 |
t.Fatalf("unexpected problems:\n- want: %v\n- got: %v", |
| 434 |
want, got) |
| 435 |
} |
| 436 |
}) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
func TestLintCounter(t *testing.T) { |
| 441 |
tests := []test{ |
| 442 |
{ |
| 443 |
name: "counter without _total suffix", |
| 444 |
in: ` |
| 445 |
# HELP x_bytes Test metric. |
| 446 |
# TYPE x_bytes counter |
| 447 |
x_bytes 10 |
| 448 |
`, |
| 449 |
problems: []promlint.Problem{{ |
| 450 |
Metric: "x_bytes", |
| 451 |
Text: `counter metrics should have "_total" suffix`, |
| 452 |
}}, |
| 453 |
}, |
| 454 |
{ |
| 455 |
name: "gauge with _total suffix", |
| 456 |
in: ` |
| 457 |
# HELP x_bytes_total Test metric. |
| 458 |
# TYPE x_bytes_total gauge |
| 459 |
x_bytes_total 10 |
| 460 |
`, |
| 461 |
problems: []promlint.Problem{{ |
| 462 |
Metric: "x_bytes_total", |
| 463 |
Text: `non-counter metrics should not have "_total" suffix`, |
| 464 |
}}, |
| 465 |
}, |
| 466 |
{ |
| 467 |
name: "counter with _total suffix", |
| 468 |
in: ` |
| 469 |
# HELP x_bytes_total Test metric. |
| 470 |
# TYPE x_bytes_total counter |
| 471 |
x_bytes_total 10 |
| 472 |
`, |
| 473 |
}, |
| 474 |
{ |
| 475 |
name: "gauge without _total suffix", |
| 476 |
in: ` |
| 477 |
# HELP x_bytes Test metric. |
| 478 |
# TYPE x_bytes gauge |
| 479 |
x_bytes 10 |
| 480 |
`, |
| 481 |
}, |
| 482 |
{ |
| 483 |
name: "untyped with _total suffix", |
| 484 |
in: ` |
| 485 |
# HELP x_bytes_total Test metric. |
| 486 |
# TYPE x_bytes_total untyped |
| 487 |
x_bytes_total 10 |
| 488 |
`, |
| 489 |
}, |
| 490 |
{ |
| 491 |
name: "untyped without _total suffix", |
| 492 |
in: ` |
| 493 |
# HELP x_bytes Test metric. |
| 494 |
# TYPE x_bytes untyped |
| 495 |
x_bytes 10 |
| 496 |
`, |
| 497 |
}, |
| 498 |
} |
| 499 |
|
| 500 |
runTests(t, tests) |
| 501 |
} |
| 502 |
|
| 503 |
func TestLintHistogramSummaryReserved(t *testing.T) { |
| 504 |
tests := []test{ |
| 505 |
{ |
| 506 |
name: "gauge with _bucket suffix", |
| 507 |
in: ` |
| 508 |
# HELP x_bytes_bucket Test metric. |
| 509 |
# TYPE x_bytes_bucket gauge |
| 510 |
x_bytes_bucket 10 |
| 511 |
`, |
| 512 |
problems: []promlint.Problem{{ |
| 513 |
Metric: "x_bytes_bucket", |
| 514 |
Text: `non-histogram metrics should not have "_bucket" suffix`, |
| 515 |
}}, |
| 516 |
}, |
| 517 |
{ |
| 518 |
name: "gauge with _count suffix", |
| 519 |
in: ` |
| 520 |
# HELP x_bytes_count Test metric. |
| 521 |
# TYPE x_bytes_count gauge |
| 522 |
x_bytes_count 10 |
| 523 |
`, |
| 524 |
problems: []promlint.Problem{{ |
| 525 |
Metric: "x_bytes_count", |
| 526 |
Text: `non-histogram and non-summary metrics should not have "_count" suffix`, |
| 527 |
}}, |
| 528 |
}, |
| 529 |
{ |
| 530 |
name: "gauge with _sum suffix", |
| 531 |
in: ` |
| 532 |
# HELP x_bytes_sum Test metric. |
| 533 |
# TYPE x_bytes_sum gauge |
| 534 |
x_bytes_sum 10 |
| 535 |
`, |
| 536 |
problems: []promlint.Problem{{ |
| 537 |
Metric: "x_bytes_sum", |
| 538 |
Text: `non-histogram and non-summary metrics should not have "_sum" suffix`, |
| 539 |
}}, |
| 540 |
}, |
| 541 |
{ |
| 542 |
name: "gauge with le label", |
| 543 |
in: ` |
| 544 |
# HELP x_bytes Test metric. |
| 545 |
# TYPE x_bytes gauge |
| 546 |
x_bytes{le="1"} 10 |
| 547 |
`, |
| 548 |
problems: []promlint.Problem{{ |
| 549 |
Metric: "x_bytes", |
| 550 |
Text: `non-histogram metrics should not have "le" label`, |
| 551 |
}}, |
| 552 |
}, |
| 553 |
{ |
| 554 |
name: "gauge with quantile label", |
| 555 |
in: ` |
| 556 |
# HELP x_bytes Test metric. |
| 557 |
# TYPE x_bytes gauge |
| 558 |
x_bytes{quantile="1"} 10 |
| 559 |
`, |
| 560 |
problems: []promlint.Problem{{ |
| 561 |
Metric: "x_bytes", |
| 562 |
Text: `non-summary metrics should not have "quantile" label`, |
| 563 |
}}, |
| 564 |
}, |
| 565 |
{ |
| 566 |
name: "histogram with quantile label", |
| 567 |
in: ` |
| 568 |
# HELP tsdb_compaction_duration Duration of compaction runs. |
| 569 |
# TYPE tsdb_compaction_duration histogram |
| 570 |
tsdb_compaction_duration_bucket{le="0.005",quantile="0.01"} 0 |
| 571 |
tsdb_compaction_duration_bucket{le="0.01",quantile="0.01"} 0 |
| 572 |
tsdb_compaction_duration_bucket{le="0.025",quantile="0.01"} 0 |
| 573 |
tsdb_compaction_duration_bucket{le="0.05",quantile="0.01"} 0 |
| 574 |
tsdb_compaction_duration_bucket{le="0.1",quantile="0.01"} 0 |
| 575 |
tsdb_compaction_duration_bucket{le="0.25",quantile="0.01"} 0 |
| 576 |
tsdb_compaction_duration_bucket{le="0.5",quantile="0.01"} 57 |
| 577 |
tsdb_compaction_duration_bucket{le="1",quantile="0.01"} 68 |
| 578 |
tsdb_compaction_duration_bucket{le="2.5",quantile="0.01"} 69 |
| 579 |
tsdb_compaction_duration_bucket{le="5",quantile="0.01"} 69 |
| 580 |
tsdb_compaction_duration_bucket{le="10",quantile="0.01"} 69 |
| 581 |
tsdb_compaction_duration_bucket{le="+Inf",quantile="0.01"} 69 |
| 582 |
tsdb_compaction_duration_sum 28.740810936000006 |
| 583 |
tsdb_compaction_duration_count 69 |
| 584 |
`, |
| 585 |
problems: []promlint.Problem{{ |
| 586 |
Metric: "tsdb_compaction_duration", |
| 587 |
Text: `non-summary metrics should not have "quantile" label`, |
| 588 |
}}, |
| 589 |
}, |
| 590 |
{ |
| 591 |
name: "summary with le label", |
| 592 |
in: ` |
| 593 |
# HELP go_gc_duration_seconds A summary of the GC invocation durations. |
| 594 |
# TYPE go_gc_duration_seconds summary |
| 595 |
go_gc_duration_seconds{quantile="0",le="0.01"} 4.2365e-05 |
| 596 |
go_gc_duration_seconds{quantile="0.25",le="0.01"} 8.1492e-05 |
| 597 |
go_gc_duration_seconds{quantile="0.5",le="0.01"} 0.000100656 |
| 598 |
go_gc_duration_seconds{quantile="0.75",le="0.01"} 0.000113913 |
| 599 |
go_gc_duration_seconds{quantile="1",le="0.01"} 0.021754305 |
| 600 |
go_gc_duration_seconds_sum 1.769429004 |
| 601 |
go_gc_duration_seconds_count 5962 |
| 602 |
`, |
| 603 |
problems: []promlint.Problem{{ |
| 604 |
Metric: "go_gc_duration_seconds", |
| 605 |
Text: `non-histogram metrics should not have "le" label`, |
| 606 |
}}, |
| 607 |
}, |
| 608 |
{ |
| 609 |
name: "histogram OK", |
| 610 |
in: ` |
| 611 |
# HELP tsdb_compaction_duration Duration of compaction runs. |
| 612 |
# TYPE tsdb_compaction_duration histogram |
| 613 |
tsdb_compaction_duration_bucket{le="0.005"} 0 |
| 614 |
tsdb_compaction_duration_bucket{le="0.01"} 0 |
| 615 |
tsdb_compaction_duration_bucket{le="0.025"} 0 |
| 616 |
tsdb_compaction_duration_bucket{le="0.05"} 0 |
| 617 |
tsdb_compaction_duration_bucket{le="0.1"} 0 |
| 618 |
tsdb_compaction_duration_bucket{le="0.25"} 0 |
| 619 |
tsdb_compaction_duration_bucket{le="0.5"} 57 |
| 620 |
tsdb_compaction_duration_bucket{le="1"} 68 |
| 621 |
tsdb_compaction_duration_bucket{le="2.5"} 69 |
| 622 |
tsdb_compaction_duration_bucket{le="5"} 69 |
| 623 |
tsdb_compaction_duration_bucket{le="10"} 69 |
| 624 |
tsdb_compaction_duration_bucket{le="+Inf"} 69 |
| 625 |
tsdb_compaction_duration_sum 28.740810936000006 |
| 626 |
tsdb_compaction_duration_count 69 |
| 627 |
`, |
| 628 |
}, |
| 629 |
{ |
| 630 |
name: "summary OK", |
| 631 |
in: ` |
| 632 |
# HELP go_gc_duration_seconds A summary of the GC invocation durations. |
| 633 |
# TYPE go_gc_duration_seconds summary |
| 634 |
go_gc_duration_seconds{quantile="0"} 4.2365e-05 |
| 635 |
go_gc_duration_seconds{quantile="0.25"} 8.1492e-05 |
| 636 |
go_gc_duration_seconds{quantile="0.5"} 0.000100656 |
| 637 |
go_gc_duration_seconds{quantile="0.75"} 0.000113913 |
| 638 |
go_gc_duration_seconds{quantile="1"} 0.021754305 |
| 639 |
go_gc_duration_seconds_sum 1.769429004 |
| 640 |
go_gc_duration_seconds_count 5962 |
| 641 |
`, |
| 642 |
}, |
| 643 |
} |
| 644 |
runTests(t, tests) |
| 645 |
} |
| 646 |
|
| 647 |
func TestLintMetricTypeInName(t *testing.T) { |
| 648 |
genTest := func(n, t, err string, problems ...promlint.Problem) test { |
| 649 |
return test{ |
| 650 |
name: fmt.Sprintf("%s with _%s suffix", t, t), |
| 651 |
in: fmt.Sprintf(` |
| 652 |
# HELP %s Test metric. |
| 653 |
# TYPE %s %s |
| 654 |
%s 10 |
| 655 |
`, n, n, t, n), |
| 656 |
problems: append(problems, promlint.Problem{ |
| 657 |
Metric: n, |
| 658 |
Text: fmt.Sprintf(`metric name should not include type '%s'`, err), |
| 659 |
}), |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
twoProbTest := genTest("http_requests_counter", "counter", "counter", promlint.Problem{ |
| 664 |
Metric: "http_requests_counter", |
| 665 |
Text: `counter metrics should have "_total" suffix`, |
| 666 |
}) |
| 667 |
|
| 668 |
tests := []test{ |
| 669 |
twoProbTest, |
| 670 |
genTest("instance_memory_limit_bytes_gauge", "gauge", "gauge"), |
| 671 |
genTest("request_duration_seconds_summary", "summary", "summary"), |
| 672 |
genTest("request_duration_seconds_histogram", "histogram", "histogram"), |
| 673 |
genTest("request_duration_seconds_HISTOGRAM", "histogram", "histogram"), |
| 674 |
|
| 675 |
genTest("instance_memory_limit_gauge_bytes", "gauge", "gauge"), |
| 676 |
} |
| 677 |
runTests(t, tests) |
| 678 |
} |
| 679 |
|
| 680 |
func TestLintReservedChars(t *testing.T) { |
| 681 |
tests := []test{ |
| 682 |
{ |
| 683 |
name: "request_duration::_seconds", |
| 684 |
in: ` |
| 685 |
# HELP request_duration::_seconds Test metric. |
| 686 |
# TYPE request_duration::_seconds histogram |
| 687 |
request_duration::_seconds 10 |
| 688 |
`, |
| 689 |
problems: []promlint.Problem{ |
| 690 |
{ |
| 691 |
Metric: "request_duration::_seconds", |
| 692 |
Text: "metric names should not contain ':'", |
| 693 |
}, |
| 694 |
}, |
| 695 |
}, |
| 696 |
} |
| 697 |
runTests(t, tests) |
| 698 |
} |
| 699 |
|
| 700 |
func TestLintCamelCase(t *testing.T) { |
| 701 |
tests := []test{ |
| 702 |
{ |
| 703 |
name: "requestDuration_seconds", |
| 704 |
in: ` |
| 705 |
# HELP requestDuration_seconds Test metric. |
| 706 |
# TYPE requestDuration_seconds histogram |
| 707 |
requestDuration_seconds 10 |
| 708 |
`, |
| 709 |
problems: []promlint.Problem{ |
| 710 |
{ |
| 711 |
Metric: "requestDuration_seconds", |
| 712 |
Text: "metric names should be written in 'snake_case' not 'camelCase'", |
| 713 |
}, |
| 714 |
}, |
| 715 |
}, |
| 716 |
{ |
| 717 |
name: "request_duration_seconds", |
| 718 |
in: ` |
| 719 |
# HELP request_duration_seconds Test metric. |
| 720 |
# TYPE request_duration_seconds histogram |
| 721 |
request_duration_seconds{httpService="foo"} 10 |
| 722 |
`, |
| 723 |
problems: []promlint.Problem{ |
| 724 |
{ |
| 725 |
Metric: "request_duration_seconds", |
| 726 |
Text: "label names should be written in 'snake_case' not 'camelCase'", |
| 727 |
}, |
| 728 |
}, |
| 729 |
}, |
| 730 |
} |
| 731 |
runTests(t, tests) |
| 732 |
} |
| 733 |
|
| 734 |
func TestLintUnitAbbreviations(t *testing.T) { |
| 735 |
genTest := func(n string) test { |
| 736 |
return test{ |
| 737 |
name: n + " with abbreviated unit", |
| 738 |
in: fmt.Sprintf(` |
| 739 |
# HELP %s Test metric. |
| 740 |
# TYPE %s gauge |
| 741 |
%s 10 |
| 742 |
`, n, n, n), |
| 743 |
problems: []promlint.Problem{ |
| 744 |
{ |
| 745 |
Metric: n, |
| 746 |
Text: "metric names should not contain abbreviated units", |
| 747 |
}, |
| 748 |
}, |
| 749 |
} |
| 750 |
} |
| 751 |
tests := []test{ |
| 752 |
genTest("instance_memory_limit_b"), |
| 753 |
genTest("instance_memory_limit_kb"), |
| 754 |
genTest("instance_memory_limit_mb"), |
| 755 |
genTest("instance_memory_limit_MB"), |
| 756 |
genTest("instance_memory_limit_gb"), |
| 757 |
genTest("instance_memory_limit_tb"), |
| 758 |
genTest("instance_memory_limit_pb"), |
| 759 |
|
| 760 |
genTest("request_duration_s"), |
| 761 |
genTest("request_duration_ms"), |
| 762 |
genTest("request_duration_us"), |
| 763 |
genTest("request_duration_ns"), |
| 764 |
genTest("request_duration_sec"), |
| 765 |
genTest("request_sec_duration"), |
| 766 |
genTest("request_duration_m"), |
| 767 |
genTest("request_duration_h"), |
| 768 |
genTest("request_duration_d"), |
| 769 |
} |
| 770 |
runTests(t, tests) |
| 771 |
} |
| 772 |
|
| 773 |
func runTests(t *testing.T, tests []test) { |
| 774 |
t.Helper() |
| 775 |
for _, tt := range tests { |
| 776 |
t.Run(tt.name, func(t *testing.T) { |
| 777 |
l := promlint.New(strings.NewReader(tt.in)) |
| 778 |
|
| 779 |
problems, err := l.Lint() |
| 780 |
if err != nil { |
| 781 |
t.Fatalf("unexpected error: %v", err) |
| 782 |
} |
| 783 |
|
| 784 |
if want, got := tt.problems, problems; !reflect.DeepEqual(want, got) { |
| 785 |
t.Fatalf("unexpected problems:\n- want: %v\n- got: %v", |
| 786 |
want, got) |
| 787 |
} |
| 788 |
}) |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
func TestCustomValidations(t *testing.T) { |
| 793 |
lintAndVerify := func(l *promlint.Linter, cv test) { |
| 794 |
problems, err := l.Lint() |
| 795 |
if err != nil { |
| 796 |
t.Fatalf("unexpected error: %v", err) |
| 797 |
} |
| 798 |
|
| 799 |
if want, got := cv.problems, problems; !reflect.DeepEqual(want, got) { |
| 800 |
t.Fatalf("unexpected problems:\n- want: %v\n- got: %v", |
| 801 |
want, got) |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
prob := []promlint.Problem{ |
| 806 |
{ |
| 807 |
Metric: "mc_something_total", |
| 808 |
Text: "expected metric name to start with 'memcached_'", |
| 809 |
}, |
| 810 |
} |
| 811 |
|
| 812 |
cv := test{ |
| 813 |
name: "metric without necessary prefix", |
| 814 |
in: ` |
| 815 |
# HELP mc_something_total Test metric. |
| 816 |
# TYPE mc_something_total counter |
| 817 |
mc_something_total 10 |
| 818 |
`, |
| 819 |
problems: nil, |
| 820 |
} |
| 821 |
|
| 822 |
prefixValidation := func(mf *dto.MetricFamily) []error { |
| 823 |
if !strings.HasPrefix(mf.GetName(), "memcached_") { |
| 824 |
return []error{errors.New("expected metric name to start with 'memcached_'")} |
| 825 |
} |
| 826 |
return nil |
| 827 |
} |
| 828 |
|
| 829 |
t.Helper() |
| 830 |
t.Run(cv.name, func(t *testing.T) { |
| 831 |
// no problems |
| 832 |
l1 := promlint.New(strings.NewReader(cv.in)) |
| 833 |
lintAndVerify(l1, cv) |
| 834 |
}) |
| 835 |
t.Run(cv.name, func(t *testing.T) { |
| 836 |
// prefix problems |
| 837 |
l2 := promlint.New(strings.NewReader(cv.in)) |
| 838 |
l2.AddCustomValidations(prefixValidation) |
| 839 |
cv.problems = prob |
| 840 |
lintAndVerify(l2, cv) |
| 841 |
}) |
| 842 |
} |
| 843 |
|
| 844 |
func TestLintDuplicateMetric(t *testing.T) { |
| 845 |
const msg = "metric not unique" |
| 846 |
|
| 847 |
tests := []test{ |
| 848 |
{ |
| 849 |
name: "metric not unique", |
| 850 |
in: ` |
| 851 |
# HELP not_unique_total the helptext |
| 852 |
# TYPE not_unique_total counter |
| 853 |
not_unique_total{bar="abc", spam="xyz"} 1 |
| 854 |
not_unique_total{bar="abc", spam="xyz"} 2 |
| 855 |
`, |
| 856 |
problems: []promlint.Problem{ |
| 857 |
{ |
| 858 |
Metric: "not_unique_total", |
| 859 |
Text: msg, |
| 860 |
}, |
| 861 |
}, |
| 862 |
}, |
| 863 |
} |
| 864 |
|
| 865 |
runTests(t, tests) |
| 866 |
} |