github.com/netdata/go.d.plugin@v0.58.1/pkg/prometheus/parse_test.go (about) 1 package prometheus 2 3 import ( 4 "bytes" 5 "fmt" 6 "math" 7 "os" 8 "testing" 9 10 "github.com/netdata/go.d.plugin/pkg/prometheus/selector" 11 12 "github.com/prometheus/prometheus/model/labels" 13 "github.com/prometheus/prometheus/model/textparse" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 var ( 19 dataMultilineHelp, _ = os.ReadFile("testdata/multiline-help.txt") 20 21 dataGaugeMeta, _ = os.ReadFile("testdata/gauge-meta.txt") 22 dataGaugeNoMeta, _ = os.ReadFile("testdata/gauge-no-meta.txt") 23 dataCounterMeta, _ = os.ReadFile("testdata/counter-meta.txt") 24 dataCounterNoMeta, _ = os.ReadFile("testdata/counter-no-meta.txt") 25 dataSummaryMeta, _ = os.ReadFile("testdata/summary-meta.txt") 26 dataSummaryNoMeta, _ = os.ReadFile("testdata/summary-no-meta.txt") 27 dataHistogramMeta, _ = os.ReadFile("testdata/histogram-meta.txt") 28 dataHistogramNoMeta, _ = os.ReadFile("testdata/histogram-no-meta.txt") 29 dataAllTypes = joinData( 30 dataGaugeMeta, dataGaugeNoMeta, dataCounterMeta, dataCounterNoMeta, 31 dataSummaryMeta, dataSummaryNoMeta, dataHistogramMeta, dataHistogramNoMeta, 32 ) 33 ) 34 35 func Test_testParseDataIsValid(t *testing.T) { 36 for name, data := range map[string][]byte{ 37 "dataMultilineHelp": dataMultilineHelp, 38 "dataGaugeMeta": dataGaugeMeta, 39 "dataGaugeNoMeta": dataGaugeNoMeta, 40 "dataCounterMeta": dataCounterMeta, 41 "dataCounterNoMeta": dataCounterNoMeta, 42 "dataSummaryMeta": dataSummaryMeta, 43 "dataSummaryNoMeta": dataSummaryNoMeta, 44 "dataHistogramMeta": dataHistogramMeta, 45 "dataHistogramNoMeta": dataHistogramNoMeta, 46 "dataAllTypes": dataAllTypes, 47 } { 48 require.NotNilf(t, data, name) 49 } 50 } 51 52 func TestPromTextParser_parseToMetricFamilies(t *testing.T) { 53 tests := map[string]struct { 54 input []byte 55 want MetricFamilies 56 }{ 57 "Gauge with multiline HELP": { 58 input: dataMultilineHelp, 59 want: MetricFamilies{ 60 "test_gauge_metric_1": { 61 name: "test_gauge_metric_1", 62 help: "First line. Second line.", 63 typ: textparse.MetricTypeGauge, 64 metrics: []Metric{ 65 { 66 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 67 gauge: &Gauge{value: 11}, 68 }, 69 }, 70 }, 71 }, 72 }, 73 "Gauge with meta parsed as Gauge": { 74 input: dataGaugeMeta, 75 want: MetricFamilies{ 76 "test_gauge_metric_1": { 77 name: "test_gauge_metric_1", 78 help: "Test Gauge Metric 1", 79 typ: textparse.MetricTypeGauge, 80 metrics: []Metric{ 81 { 82 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 83 gauge: &Gauge{value: 11}, 84 }, 85 { 86 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 87 gauge: &Gauge{value: 12}, 88 }, 89 { 90 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 91 gauge: &Gauge{value: 13}, 92 }, 93 { 94 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 95 gauge: &Gauge{value: 14}, 96 }, 97 }, 98 }, 99 "test_gauge_metric_2": { 100 name: "test_gauge_metric_2", 101 typ: textparse.MetricTypeGauge, 102 metrics: []Metric{ 103 { 104 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 105 gauge: &Gauge{value: 11}, 106 }, 107 { 108 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 109 gauge: &Gauge{value: 12}, 110 }, 111 { 112 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 113 gauge: &Gauge{value: 13}, 114 }, 115 { 116 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 117 gauge: &Gauge{value: 14}, 118 }, 119 }, 120 }, 121 }, 122 }, 123 "Counter with meta parsed as Counter": { 124 input: dataCounterMeta, 125 want: MetricFamilies{ 126 "test_counter_metric_1_total": { 127 name: "test_counter_metric_1_total", 128 help: "Test Counter Metric 1", 129 typ: textparse.MetricTypeCounter, 130 metrics: []Metric{ 131 { 132 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 133 counter: &Counter{value: 11}, 134 }, 135 { 136 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 137 counter: &Counter{value: 12}, 138 }, 139 { 140 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 141 counter: &Counter{value: 13}, 142 }, 143 { 144 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 145 counter: &Counter{value: 14}, 146 }, 147 }, 148 }, 149 "test_counter_metric_2_total": { 150 name: "test_counter_metric_2_total", 151 typ: textparse.MetricTypeCounter, 152 metrics: []Metric{ 153 { 154 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 155 counter: &Counter{value: 11}, 156 }, 157 { 158 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 159 counter: &Counter{value: 12}, 160 }, 161 { 162 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 163 counter: &Counter{value: 13}, 164 }, 165 { 166 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 167 counter: &Counter{value: 14}, 168 }, 169 }, 170 }, 171 }, 172 }, 173 "Summary with meta parsed as Summary": { 174 input: dataSummaryMeta, 175 want: MetricFamilies{ 176 "test_summary_1_duration_microseconds": { 177 name: "test_summary_1_duration_microseconds", 178 help: "Test Summary Metric 1", 179 typ: textparse.MetricTypeSummary, 180 metrics: []Metric{ 181 { 182 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 183 summary: &Summary{ 184 sum: 283201.29, 185 count: 31, 186 quantiles: []Quantile{ 187 {quantile: 0.5, value: 4931.921}, 188 {quantile: 0.9, value: 4932.921}, 189 {quantile: 0.99, value: 4933.921}, 190 }, 191 }, 192 }, 193 { 194 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 195 summary: &Summary{ 196 sum: 283201.29, 197 count: 31, 198 quantiles: []Quantile{ 199 {quantile: 0.5, value: 4931.921}, 200 {quantile: 0.9, value: 4932.921}, 201 {quantile: 0.99, value: 4933.921}, 202 }, 203 }, 204 }, 205 { 206 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 207 summary: &Summary{ 208 sum: 283201.29, 209 count: 31, 210 quantiles: []Quantile{ 211 {quantile: 0.5, value: 4931.921}, 212 {quantile: 0.9, value: 4932.921}, 213 {quantile: 0.99, value: 4933.921}, 214 }, 215 }, 216 }, 217 { 218 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 219 summary: &Summary{ 220 sum: 283201.29, 221 count: 31, 222 quantiles: []Quantile{ 223 {quantile: 0.5, value: 4931.921}, 224 {quantile: 0.9, value: 4932.921}, 225 {quantile: 0.99, value: 4933.921}, 226 }, 227 }, 228 }, 229 }, 230 }, 231 "test_summary_2_duration_microseconds": { 232 name: "test_summary_2_duration_microseconds", 233 typ: textparse.MetricTypeSummary, 234 metrics: []Metric{ 235 { 236 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 237 summary: &Summary{ 238 sum: 383201.29, 239 count: 41, 240 quantiles: []Quantile{ 241 {quantile: 0.5, value: 5931.921}, 242 {quantile: 0.9, value: 5932.921}, 243 {quantile: 0.99, value: 5933.921}, 244 }, 245 }, 246 }, 247 { 248 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 249 summary: &Summary{ 250 sum: 383201.29, 251 count: 41, 252 quantiles: []Quantile{ 253 {quantile: 0.5, value: 5931.921}, 254 {quantile: 0.9, value: 5932.921}, 255 {quantile: 0.99, value: 5933.921}, 256 }, 257 }, 258 }, 259 { 260 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 261 summary: &Summary{ 262 sum: 383201.29, 263 count: 41, 264 quantiles: []Quantile{ 265 {quantile: 0.5, value: 5931.921}, 266 {quantile: 0.9, value: 5932.921}, 267 {quantile: 0.99, value: 5933.921}, 268 }, 269 }, 270 }, 271 { 272 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 273 summary: &Summary{ 274 sum: 383201.29, 275 count: 41, 276 quantiles: []Quantile{ 277 {quantile: 0.5, value: 5931.921}, 278 {quantile: 0.9, value: 5932.921}, 279 {quantile: 0.99, value: 5933.921}, 280 }, 281 }, 282 }, 283 }, 284 }, 285 }, 286 }, 287 "Histogram with meta parsed as Histogram": { 288 input: dataHistogramMeta, 289 want: MetricFamilies{ 290 "test_histogram_1_duration_seconds": { 291 name: "test_histogram_1_duration_seconds", 292 help: "Test Histogram Metric 1", 293 typ: textparse.MetricTypeHistogram, 294 metrics: []Metric{ 295 { 296 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 297 histogram: &Histogram{ 298 sum: 0.00147889, 299 count: 6, 300 buckets: []Bucket{ 301 {upperBound: 0.1, cumulativeCount: 4}, 302 {upperBound: 0.5, cumulativeCount: 5}, 303 {upperBound: math.Inf(1), cumulativeCount: 6}, 304 }, 305 }, 306 }, 307 { 308 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 309 histogram: &Histogram{ 310 sum: 0.00147889, 311 count: 6, 312 buckets: []Bucket{ 313 {upperBound: 0.1, cumulativeCount: 4}, 314 {upperBound: 0.5, cumulativeCount: 5}, 315 {upperBound: math.Inf(1), cumulativeCount: 6}, 316 }, 317 }, 318 }, 319 { 320 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 321 histogram: &Histogram{ 322 sum: 0.00147889, 323 count: 6, 324 buckets: []Bucket{ 325 {upperBound: 0.1, cumulativeCount: 4}, 326 {upperBound: 0.5, cumulativeCount: 5}, 327 {upperBound: math.Inf(1), cumulativeCount: 6}, 328 }, 329 }, 330 }, 331 { 332 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 333 histogram: &Histogram{ 334 sum: 0.00147889, 335 count: 6, 336 buckets: []Bucket{ 337 {upperBound: 0.1, cumulativeCount: 4}, 338 {upperBound: 0.5, cumulativeCount: 5}, 339 {upperBound: math.Inf(1), cumulativeCount: 6}, 340 }, 341 }, 342 }, 343 }, 344 }, 345 "test_histogram_2_duration_seconds": { 346 name: "test_histogram_2_duration_seconds", 347 typ: textparse.MetricTypeHistogram, 348 metrics: []Metric{ 349 { 350 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 351 histogram: &Histogram{ 352 sum: 0.00247889, 353 count: 9, 354 buckets: []Bucket{ 355 {upperBound: 0.1, cumulativeCount: 7}, 356 {upperBound: 0.5, cumulativeCount: 8}, 357 {upperBound: math.Inf(1), cumulativeCount: 9}, 358 }, 359 }, 360 }, 361 { 362 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 363 histogram: &Histogram{ 364 sum: 0.00247889, 365 count: 9, 366 buckets: []Bucket{ 367 {upperBound: 0.1, cumulativeCount: 7}, 368 {upperBound: 0.5, cumulativeCount: 8}, 369 {upperBound: math.Inf(1), cumulativeCount: 9}, 370 }, 371 }, 372 }, 373 { 374 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 375 histogram: &Histogram{ 376 sum: 0.00247889, 377 count: 9, 378 buckets: []Bucket{ 379 {upperBound: 0.1, cumulativeCount: 7}, 380 {upperBound: 0.5, cumulativeCount: 8}, 381 {upperBound: math.Inf(1), cumulativeCount: 9}, 382 }, 383 }, 384 }, 385 { 386 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 387 histogram: &Histogram{ 388 sum: 0.00247889, 389 count: 9, 390 buckets: []Bucket{ 391 {upperBound: 0.1, cumulativeCount: 7}, 392 {upperBound: 0.5, cumulativeCount: 8}, 393 {upperBound: math.Inf(1), cumulativeCount: 9}, 394 }, 395 }, 396 }, 397 }, 398 }, 399 }, 400 }, 401 "Gauge no meta parsed as Untyped": { 402 input: dataGaugeNoMeta, 403 want: MetricFamilies{ 404 "test_gauge_no_meta_metric_1": { 405 name: "test_gauge_no_meta_metric_1", 406 typ: textparse.MetricTypeUnknown, 407 metrics: []Metric{ 408 { 409 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 410 untyped: &Untyped{value: 11}, 411 }, 412 { 413 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 414 untyped: &Untyped{value: 12}, 415 }, 416 { 417 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 418 untyped: &Untyped{value: 13}, 419 }, 420 { 421 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 422 untyped: &Untyped{value: 14}, 423 }, 424 }, 425 }, 426 "test_gauge_no_meta_metric_2": { 427 name: "test_gauge_no_meta_metric_2", 428 typ: textparse.MetricTypeUnknown, 429 metrics: []Metric{ 430 { 431 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 432 untyped: &Untyped{value: 11}, 433 }, 434 { 435 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 436 untyped: &Untyped{value: 12}, 437 }, 438 { 439 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 440 untyped: &Untyped{value: 13}, 441 }, 442 { 443 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 444 untyped: &Untyped{value: 14}, 445 }, 446 }, 447 }, 448 }, 449 }, 450 "Counter no meta parsed as Untyped": { 451 input: dataCounterNoMeta, 452 want: MetricFamilies{ 453 "test_counter_no_meta_metric_1_total": { 454 name: "test_counter_no_meta_metric_1_total", 455 typ: textparse.MetricTypeUnknown, 456 metrics: []Metric{ 457 { 458 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 459 untyped: &Untyped{value: 11}, 460 }, 461 { 462 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 463 untyped: &Untyped{value: 12}, 464 }, 465 { 466 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 467 untyped: &Untyped{value: 13}, 468 }, 469 { 470 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 471 untyped: &Untyped{value: 14}, 472 }, 473 }, 474 }, 475 "test_counter_no_meta_metric_2_total": { 476 name: "test_counter_no_meta_metric_2_total", 477 typ: textparse.MetricTypeUnknown, 478 metrics: []Metric{ 479 { 480 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 481 untyped: &Untyped{value: 11}, 482 }, 483 { 484 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 485 untyped: &Untyped{value: 12}, 486 }, 487 { 488 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 489 untyped: &Untyped{value: 13}, 490 }, 491 { 492 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 493 untyped: &Untyped{value: 14}, 494 }, 495 }, 496 }, 497 }, 498 }, 499 "Summary no meta parsed as Summary": { 500 input: dataSummaryNoMeta, 501 want: MetricFamilies{ 502 "test_summary_no_meta_1_duration_microseconds": { 503 name: "test_summary_no_meta_1_duration_microseconds", 504 typ: textparse.MetricTypeSummary, 505 metrics: []Metric{ 506 { 507 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 508 summary: &Summary{ 509 sum: 283201.29, 510 count: 31, 511 quantiles: []Quantile{ 512 {quantile: 0.5, value: 4931.921}, 513 {quantile: 0.9, value: 4932.921}, 514 {quantile: 0.99, value: 4933.921}, 515 }, 516 }, 517 }, 518 { 519 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 520 summary: &Summary{ 521 sum: 283201.29, 522 count: 31, 523 quantiles: []Quantile{ 524 {quantile: 0.5, value: 4931.921}, 525 {quantile: 0.9, value: 4932.921}, 526 {quantile: 0.99, value: 4933.921}, 527 }, 528 }, 529 }, 530 { 531 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 532 summary: &Summary{ 533 sum: 283201.29, 534 count: 31, 535 quantiles: []Quantile{ 536 {quantile: 0.5, value: 4931.921}, 537 {quantile: 0.9, value: 4932.921}, 538 {quantile: 0.99, value: 4933.921}, 539 }, 540 }, 541 }, 542 { 543 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 544 summary: &Summary{ 545 sum: 283201.29, 546 count: 31, 547 quantiles: []Quantile{ 548 {quantile: 0.5, value: 4931.921}, 549 {quantile: 0.9, value: 4932.921}, 550 {quantile: 0.99, value: 4933.921}, 551 }, 552 }, 553 }, 554 }, 555 }, 556 "test_summary_no_meta_2_duration_microseconds": { 557 name: "test_summary_no_meta_2_duration_microseconds", 558 typ: textparse.MetricTypeSummary, 559 metrics: []Metric{ 560 { 561 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 562 summary: &Summary{ 563 sum: 383201.29, 564 count: 41, 565 quantiles: []Quantile{ 566 {quantile: 0.5, value: 5931.921}, 567 {quantile: 0.9, value: 5932.921}, 568 {quantile: 0.99, value: 5933.921}, 569 }, 570 }, 571 }, 572 { 573 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 574 summary: &Summary{ 575 sum: 383201.29, 576 count: 41, 577 quantiles: []Quantile{ 578 {quantile: 0.5, value: 5931.921}, 579 {quantile: 0.9, value: 5932.921}, 580 {quantile: 0.99, value: 5933.921}, 581 }, 582 }, 583 }, 584 { 585 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 586 summary: &Summary{ 587 sum: 383201.29, 588 count: 41, 589 quantiles: []Quantile{ 590 {quantile: 0.5, value: 5931.921}, 591 {quantile: 0.9, value: 5932.921}, 592 {quantile: 0.99, value: 5933.921}, 593 }, 594 }, 595 }, 596 { 597 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 598 summary: &Summary{ 599 sum: 383201.29, 600 count: 41, 601 quantiles: []Quantile{ 602 {quantile: 0.5, value: 5931.921}, 603 {quantile: 0.9, value: 5932.921}, 604 {quantile: 0.99, value: 5933.921}, 605 }, 606 }, 607 }, 608 }, 609 }, 610 }, 611 }, 612 "Histogram no meta parsed as Histogram": { 613 input: dataHistogramNoMeta, 614 want: MetricFamilies{ 615 "test_histogram_no_meta_1_duration_seconds": { 616 name: "test_histogram_no_meta_1_duration_seconds", 617 typ: textparse.MetricTypeHistogram, 618 metrics: []Metric{ 619 { 620 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 621 histogram: &Histogram{ 622 sum: 0.00147889, 623 count: 6, 624 buckets: []Bucket{ 625 {upperBound: 0.1, cumulativeCount: 4}, 626 {upperBound: 0.5, cumulativeCount: 5}, 627 {upperBound: math.Inf(1), cumulativeCount: 6}, 628 }, 629 }, 630 }, 631 { 632 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 633 histogram: &Histogram{ 634 sum: 0.00147889, 635 count: 6, 636 buckets: []Bucket{ 637 {upperBound: 0.1, cumulativeCount: 4}, 638 {upperBound: 0.5, cumulativeCount: 5}, 639 {upperBound: math.Inf(1), cumulativeCount: 6}, 640 }, 641 }, 642 }, 643 { 644 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 645 histogram: &Histogram{ 646 sum: 0.00147889, 647 count: 6, 648 buckets: []Bucket{ 649 {upperBound: 0.1, cumulativeCount: 4}, 650 {upperBound: 0.5, cumulativeCount: 5}, 651 {upperBound: math.Inf(1), cumulativeCount: 6}, 652 }, 653 }, 654 }, 655 { 656 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 657 histogram: &Histogram{ 658 sum: 0.00147889, 659 count: 6, 660 buckets: []Bucket{ 661 {upperBound: 0.1, cumulativeCount: 4}, 662 {upperBound: 0.5, cumulativeCount: 5}, 663 {upperBound: math.Inf(1), cumulativeCount: 6}, 664 }, 665 }, 666 }, 667 }, 668 }, 669 "test_histogram_no_meta_2_duration_seconds": { 670 name: "test_histogram_no_meta_2_duration_seconds", 671 typ: textparse.MetricTypeHistogram, 672 metrics: []Metric{ 673 { 674 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 675 histogram: &Histogram{ 676 sum: 0.00247889, 677 count: 9, 678 buckets: []Bucket{ 679 {upperBound: 0.1, cumulativeCount: 7}, 680 {upperBound: 0.5, cumulativeCount: 8}, 681 {upperBound: math.Inf(1), cumulativeCount: 9}, 682 }, 683 }, 684 }, 685 { 686 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 687 histogram: &Histogram{ 688 sum: 0.00247889, 689 count: 9, 690 buckets: []Bucket{ 691 {upperBound: 0.1, cumulativeCount: 7}, 692 {upperBound: 0.5, cumulativeCount: 8}, 693 {upperBound: math.Inf(1), cumulativeCount: 9}, 694 }, 695 }, 696 }, 697 { 698 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 699 histogram: &Histogram{ 700 sum: 0.00247889, 701 count: 9, 702 buckets: []Bucket{ 703 {upperBound: 0.1, cumulativeCount: 7}, 704 {upperBound: 0.5, cumulativeCount: 8}, 705 {upperBound: math.Inf(1), cumulativeCount: 9}, 706 }, 707 }, 708 }, 709 { 710 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 711 histogram: &Histogram{ 712 sum: 0.00247889, 713 count: 9, 714 buckets: []Bucket{ 715 {upperBound: 0.1, cumulativeCount: 7}, 716 {upperBound: 0.5, cumulativeCount: 8}, 717 {upperBound: math.Inf(1), cumulativeCount: 9}, 718 }, 719 }, 720 }, 721 }, 722 }, 723 }, 724 }, 725 "All types": { 726 input: dataAllTypes, 727 want: MetricFamilies{ 728 "test_gauge_metric_1": { 729 name: "test_gauge_metric_1", 730 help: "Test Gauge Metric 1", 731 typ: textparse.MetricTypeGauge, 732 metrics: []Metric{ 733 { 734 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 735 gauge: &Gauge{value: 11}, 736 }, 737 { 738 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 739 gauge: &Gauge{value: 12}, 740 }, 741 { 742 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 743 gauge: &Gauge{value: 13}, 744 }, 745 { 746 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 747 gauge: &Gauge{value: 14}, 748 }, 749 }, 750 }, 751 "test_gauge_metric_2": { 752 name: "test_gauge_metric_2", 753 typ: textparse.MetricTypeGauge, 754 metrics: []Metric{ 755 { 756 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 757 gauge: &Gauge{value: 11}, 758 }, 759 { 760 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 761 gauge: &Gauge{value: 12}, 762 }, 763 { 764 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 765 gauge: &Gauge{value: 13}, 766 }, 767 { 768 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 769 gauge: &Gauge{value: 14}, 770 }, 771 }, 772 }, 773 "test_counter_metric_1_total": { 774 name: "test_counter_metric_1_total", 775 help: "Test Counter Metric 1", 776 typ: textparse.MetricTypeCounter, 777 metrics: []Metric{ 778 { 779 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 780 counter: &Counter{value: 11}, 781 }, 782 { 783 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 784 counter: &Counter{value: 12}, 785 }, 786 { 787 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 788 counter: &Counter{value: 13}, 789 }, 790 { 791 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 792 counter: &Counter{value: 14}, 793 }, 794 }, 795 }, 796 "test_counter_metric_2_total": { 797 name: "test_counter_metric_2_total", 798 typ: textparse.MetricTypeCounter, 799 metrics: []Metric{ 800 { 801 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 802 counter: &Counter{value: 11}, 803 }, 804 { 805 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 806 counter: &Counter{value: 12}, 807 }, 808 { 809 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 810 counter: &Counter{value: 13}, 811 }, 812 { 813 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 814 counter: &Counter{value: 14}, 815 }, 816 }, 817 }, 818 "test_summary_1_duration_microseconds": { 819 name: "test_summary_1_duration_microseconds", 820 help: "Test Summary Metric 1", 821 typ: textparse.MetricTypeSummary, 822 metrics: []Metric{ 823 { 824 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 825 summary: &Summary{ 826 sum: 283201.29, 827 count: 31, 828 quantiles: []Quantile{ 829 {quantile: 0.5, value: 4931.921}, 830 {quantile: 0.9, value: 4932.921}, 831 {quantile: 0.99, value: 4933.921}, 832 }, 833 }, 834 }, 835 { 836 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 837 summary: &Summary{ 838 sum: 283201.29, 839 count: 31, 840 quantiles: []Quantile{ 841 {quantile: 0.5, value: 4931.921}, 842 {quantile: 0.9, value: 4932.921}, 843 {quantile: 0.99, value: 4933.921}, 844 }, 845 }, 846 }, 847 { 848 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 849 summary: &Summary{ 850 sum: 283201.29, 851 count: 31, 852 quantiles: []Quantile{ 853 {quantile: 0.5, value: 4931.921}, 854 {quantile: 0.9, value: 4932.921}, 855 {quantile: 0.99, value: 4933.921}, 856 }, 857 }, 858 }, 859 { 860 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 861 summary: &Summary{ 862 sum: 283201.29, 863 count: 31, 864 quantiles: []Quantile{ 865 {quantile: 0.5, value: 4931.921}, 866 {quantile: 0.9, value: 4932.921}, 867 {quantile: 0.99, value: 4933.921}, 868 }, 869 }, 870 }, 871 }, 872 }, 873 "test_summary_2_duration_microseconds": { 874 name: "test_summary_2_duration_microseconds", 875 typ: textparse.MetricTypeSummary, 876 metrics: []Metric{ 877 { 878 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 879 summary: &Summary{ 880 sum: 383201.29, 881 count: 41, 882 quantiles: []Quantile{ 883 {quantile: 0.5, value: 5931.921}, 884 {quantile: 0.9, value: 5932.921}, 885 {quantile: 0.99, value: 5933.921}, 886 }, 887 }, 888 }, 889 { 890 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 891 summary: &Summary{ 892 sum: 383201.29, 893 count: 41, 894 quantiles: []Quantile{ 895 {quantile: 0.5, value: 5931.921}, 896 {quantile: 0.9, value: 5932.921}, 897 {quantile: 0.99, value: 5933.921}, 898 }, 899 }, 900 }, 901 { 902 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 903 summary: &Summary{ 904 sum: 383201.29, 905 count: 41, 906 quantiles: []Quantile{ 907 {quantile: 0.5, value: 5931.921}, 908 {quantile: 0.9, value: 5932.921}, 909 {quantile: 0.99, value: 5933.921}, 910 }, 911 }, 912 }, 913 { 914 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 915 summary: &Summary{ 916 sum: 383201.29, 917 count: 41, 918 quantiles: []Quantile{ 919 {quantile: 0.5, value: 5931.921}, 920 {quantile: 0.9, value: 5932.921}, 921 {quantile: 0.99, value: 5933.921}, 922 }, 923 }, 924 }, 925 }, 926 }, 927 "test_histogram_1_duration_seconds": { 928 name: "test_histogram_1_duration_seconds", 929 help: "Test Histogram Metric 1", 930 typ: textparse.MetricTypeHistogram, 931 metrics: []Metric{ 932 { 933 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 934 histogram: &Histogram{ 935 sum: 0.00147889, 936 count: 6, 937 buckets: []Bucket{ 938 {upperBound: 0.1, cumulativeCount: 4}, 939 {upperBound: 0.5, cumulativeCount: 5}, 940 {upperBound: math.Inf(1), cumulativeCount: 6}, 941 }, 942 }, 943 }, 944 { 945 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 946 histogram: &Histogram{ 947 sum: 0.00147889, 948 count: 6, 949 buckets: []Bucket{ 950 {upperBound: 0.1, cumulativeCount: 4}, 951 {upperBound: 0.5, cumulativeCount: 5}, 952 {upperBound: math.Inf(1), cumulativeCount: 6}, 953 }, 954 }, 955 }, 956 { 957 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 958 histogram: &Histogram{ 959 sum: 0.00147889, 960 count: 6, 961 buckets: []Bucket{ 962 {upperBound: 0.1, cumulativeCount: 4}, 963 {upperBound: 0.5, cumulativeCount: 5}, 964 {upperBound: math.Inf(1), cumulativeCount: 6}, 965 }, 966 }, 967 }, 968 { 969 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 970 histogram: &Histogram{ 971 sum: 0.00147889, 972 count: 6, 973 buckets: []Bucket{ 974 {upperBound: 0.1, cumulativeCount: 4}, 975 {upperBound: 0.5, cumulativeCount: 5}, 976 {upperBound: math.Inf(1), cumulativeCount: 6}, 977 }, 978 }, 979 }, 980 }, 981 }, 982 "test_histogram_2_duration_seconds": { 983 name: "test_histogram_2_duration_seconds", 984 typ: textparse.MetricTypeHistogram, 985 metrics: []Metric{ 986 { 987 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 988 histogram: &Histogram{ 989 sum: 0.00247889, 990 count: 9, 991 buckets: []Bucket{ 992 {upperBound: 0.1, cumulativeCount: 7}, 993 {upperBound: 0.5, cumulativeCount: 8}, 994 {upperBound: math.Inf(1), cumulativeCount: 9}, 995 }, 996 }, 997 }, 998 { 999 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1000 histogram: &Histogram{ 1001 sum: 0.00247889, 1002 count: 9, 1003 buckets: []Bucket{ 1004 {upperBound: 0.1, cumulativeCount: 7}, 1005 {upperBound: 0.5, cumulativeCount: 8}, 1006 {upperBound: math.Inf(1), cumulativeCount: 9}, 1007 }, 1008 }, 1009 }, 1010 { 1011 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1012 histogram: &Histogram{ 1013 sum: 0.00247889, 1014 count: 9, 1015 buckets: []Bucket{ 1016 {upperBound: 0.1, cumulativeCount: 7}, 1017 {upperBound: 0.5, cumulativeCount: 8}, 1018 {upperBound: math.Inf(1), cumulativeCount: 9}, 1019 }, 1020 }, 1021 }, 1022 { 1023 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1024 histogram: &Histogram{ 1025 sum: 0.00247889, 1026 count: 9, 1027 buckets: []Bucket{ 1028 {upperBound: 0.1, cumulativeCount: 7}, 1029 {upperBound: 0.5, cumulativeCount: 8}, 1030 {upperBound: math.Inf(1), cumulativeCount: 9}, 1031 }, 1032 }, 1033 }, 1034 }, 1035 }, 1036 "test_gauge_no_meta_metric_1": { 1037 name: "test_gauge_no_meta_metric_1", 1038 typ: textparse.MetricTypeUnknown, 1039 metrics: []Metric{ 1040 { 1041 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1042 untyped: &Untyped{value: 11}, 1043 }, 1044 { 1045 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1046 untyped: &Untyped{value: 12}, 1047 }, 1048 { 1049 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1050 untyped: &Untyped{value: 13}, 1051 }, 1052 { 1053 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1054 untyped: &Untyped{value: 14}, 1055 }, 1056 }, 1057 }, 1058 "test_gauge_no_meta_metric_2": { 1059 name: "test_gauge_no_meta_metric_2", 1060 typ: textparse.MetricTypeUnknown, 1061 metrics: []Metric{ 1062 { 1063 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1064 untyped: &Untyped{value: 11}, 1065 }, 1066 { 1067 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1068 untyped: &Untyped{value: 12}, 1069 }, 1070 { 1071 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1072 untyped: &Untyped{value: 13}, 1073 }, 1074 { 1075 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1076 untyped: &Untyped{value: 14}, 1077 }, 1078 }, 1079 }, 1080 "test_counter_no_meta_metric_1_total": { 1081 name: "test_counter_no_meta_metric_1_total", 1082 typ: textparse.MetricTypeUnknown, 1083 metrics: []Metric{ 1084 { 1085 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1086 untyped: &Untyped{value: 11}, 1087 }, 1088 { 1089 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1090 untyped: &Untyped{value: 12}, 1091 }, 1092 { 1093 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1094 untyped: &Untyped{value: 13}, 1095 }, 1096 { 1097 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1098 untyped: &Untyped{value: 14}, 1099 }, 1100 }, 1101 }, 1102 "test_counter_no_meta_metric_2_total": { 1103 name: "test_counter_no_meta_metric_2_total", 1104 typ: textparse.MetricTypeUnknown, 1105 metrics: []Metric{ 1106 { 1107 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1108 untyped: &Untyped{value: 11}, 1109 }, 1110 { 1111 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1112 untyped: &Untyped{value: 12}, 1113 }, 1114 { 1115 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1116 untyped: &Untyped{value: 13}, 1117 }, 1118 { 1119 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1120 untyped: &Untyped{value: 14}, 1121 }, 1122 }, 1123 }, 1124 "test_summary_no_meta_1_duration_microseconds": { 1125 name: "test_summary_no_meta_1_duration_microseconds", 1126 typ: textparse.MetricTypeSummary, 1127 metrics: []Metric{ 1128 { 1129 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1130 summary: &Summary{ 1131 sum: 283201.29, 1132 count: 31, 1133 quantiles: []Quantile{ 1134 {quantile: 0.5, value: 4931.921}, 1135 {quantile: 0.9, value: 4932.921}, 1136 {quantile: 0.99, value: 4933.921}, 1137 }, 1138 }, 1139 }, 1140 { 1141 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1142 summary: &Summary{ 1143 sum: 283201.29, 1144 count: 31, 1145 quantiles: []Quantile{ 1146 {quantile: 0.5, value: 4931.921}, 1147 {quantile: 0.9, value: 4932.921}, 1148 {quantile: 0.99, value: 4933.921}, 1149 }, 1150 }, 1151 }, 1152 { 1153 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1154 summary: &Summary{ 1155 sum: 283201.29, 1156 count: 31, 1157 quantiles: []Quantile{ 1158 {quantile: 0.5, value: 4931.921}, 1159 {quantile: 0.9, value: 4932.921}, 1160 {quantile: 0.99, value: 4933.921}, 1161 }, 1162 }, 1163 }, 1164 { 1165 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1166 summary: &Summary{ 1167 sum: 283201.29, 1168 count: 31, 1169 quantiles: []Quantile{ 1170 {quantile: 0.5, value: 4931.921}, 1171 {quantile: 0.9, value: 4932.921}, 1172 {quantile: 0.99, value: 4933.921}, 1173 }, 1174 }, 1175 }, 1176 }, 1177 }, 1178 "test_summary_no_meta_2_duration_microseconds": { 1179 name: "test_summary_no_meta_2_duration_microseconds", 1180 typ: textparse.MetricTypeSummary, 1181 metrics: []Metric{ 1182 { 1183 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1184 summary: &Summary{ 1185 sum: 383201.29, 1186 count: 41, 1187 quantiles: []Quantile{ 1188 {quantile: 0.5, value: 5931.921}, 1189 {quantile: 0.9, value: 5932.921}, 1190 {quantile: 0.99, value: 5933.921}, 1191 }, 1192 }, 1193 }, 1194 { 1195 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1196 summary: &Summary{ 1197 sum: 383201.29, 1198 count: 41, 1199 quantiles: []Quantile{ 1200 {quantile: 0.5, value: 5931.921}, 1201 {quantile: 0.9, value: 5932.921}, 1202 {quantile: 0.99, value: 5933.921}, 1203 }, 1204 }, 1205 }, 1206 { 1207 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1208 summary: &Summary{ 1209 sum: 383201.29, 1210 count: 41, 1211 quantiles: []Quantile{ 1212 {quantile: 0.5, value: 5931.921}, 1213 {quantile: 0.9, value: 5932.921}, 1214 {quantile: 0.99, value: 5933.921}, 1215 }, 1216 }, 1217 }, 1218 { 1219 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1220 summary: &Summary{ 1221 sum: 383201.29, 1222 count: 41, 1223 quantiles: []Quantile{ 1224 {quantile: 0.5, value: 5931.921}, 1225 {quantile: 0.9, value: 5932.921}, 1226 {quantile: 0.99, value: 5933.921}, 1227 }, 1228 }, 1229 }, 1230 }, 1231 }, 1232 "test_histogram_no_meta_1_duration_seconds": { 1233 name: "test_histogram_no_meta_1_duration_seconds", 1234 typ: textparse.MetricTypeHistogram, 1235 metrics: []Metric{ 1236 { 1237 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1238 histogram: &Histogram{ 1239 sum: 0.00147889, 1240 count: 6, 1241 buckets: []Bucket{ 1242 {upperBound: 0.1, cumulativeCount: 4}, 1243 {upperBound: 0.5, cumulativeCount: 5}, 1244 {upperBound: math.Inf(1), cumulativeCount: 6}, 1245 }, 1246 }, 1247 }, 1248 { 1249 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1250 histogram: &Histogram{ 1251 sum: 0.00147889, 1252 count: 6, 1253 buckets: []Bucket{ 1254 {upperBound: 0.1, cumulativeCount: 4}, 1255 {upperBound: 0.5, cumulativeCount: 5}, 1256 {upperBound: math.Inf(1), cumulativeCount: 6}, 1257 }, 1258 }, 1259 }, 1260 { 1261 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1262 histogram: &Histogram{ 1263 sum: 0.00147889, 1264 count: 6, 1265 buckets: []Bucket{ 1266 {upperBound: 0.1, cumulativeCount: 4}, 1267 {upperBound: 0.5, cumulativeCount: 5}, 1268 {upperBound: math.Inf(1), cumulativeCount: 6}, 1269 }, 1270 }, 1271 }, 1272 { 1273 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1274 histogram: &Histogram{ 1275 sum: 0.00147889, 1276 count: 6, 1277 buckets: []Bucket{ 1278 {upperBound: 0.1, cumulativeCount: 4}, 1279 {upperBound: 0.5, cumulativeCount: 5}, 1280 {upperBound: math.Inf(1), cumulativeCount: 6}, 1281 }, 1282 }, 1283 }, 1284 }, 1285 }, 1286 "test_histogram_no_meta_2_duration_seconds": { 1287 name: "test_histogram_no_meta_2_duration_seconds", 1288 typ: textparse.MetricTypeHistogram, 1289 metrics: []Metric{ 1290 { 1291 labels: labels.Labels{{Name: "label1", Value: "value1"}}, 1292 histogram: &Histogram{ 1293 sum: 0.00247889, 1294 count: 9, 1295 buckets: []Bucket{ 1296 {upperBound: 0.1, cumulativeCount: 7}, 1297 {upperBound: 0.5, cumulativeCount: 8}, 1298 {upperBound: math.Inf(1), cumulativeCount: 9}, 1299 }, 1300 }, 1301 }, 1302 { 1303 labels: labels.Labels{{Name: "label1", Value: "value2"}}, 1304 histogram: &Histogram{ 1305 sum: 0.00247889, 1306 count: 9, 1307 buckets: []Bucket{ 1308 {upperBound: 0.1, cumulativeCount: 7}, 1309 {upperBound: 0.5, cumulativeCount: 8}, 1310 {upperBound: math.Inf(1), cumulativeCount: 9}, 1311 }, 1312 }, 1313 }, 1314 { 1315 labels: labels.Labels{{Name: "label1", Value: "value3"}}, 1316 histogram: &Histogram{ 1317 sum: 0.00247889, 1318 count: 9, 1319 buckets: []Bucket{ 1320 {upperBound: 0.1, cumulativeCount: 7}, 1321 {upperBound: 0.5, cumulativeCount: 8}, 1322 {upperBound: math.Inf(1), cumulativeCount: 9}, 1323 }, 1324 }, 1325 }, 1326 { 1327 labels: labels.Labels{{Name: "label1", Value: "value4"}}, 1328 histogram: &Histogram{ 1329 sum: 0.00247889, 1330 count: 9, 1331 buckets: []Bucket{ 1332 {upperBound: 0.1, cumulativeCount: 7}, 1333 {upperBound: 0.5, cumulativeCount: 8}, 1334 {upperBound: math.Inf(1), cumulativeCount: 9}, 1335 }, 1336 }, 1337 }, 1338 }, 1339 }, 1340 }, 1341 }, 1342 } 1343 1344 for name, test := range tests { 1345 t.Run(name, func(t *testing.T) { 1346 var p promTextParser 1347 1348 for i := 0; i < 10; i++ { 1349 t.Run(fmt.Sprintf("parse num %d", i+1), func(t *testing.T) { 1350 mfs, err := p.parseToMetricFamilies(test.input) 1351 if len(test.want) > 0 { 1352 assert.Equal(t, test.want, mfs) 1353 } else { 1354 assert.Error(t, err) 1355 } 1356 }) 1357 } 1358 }) 1359 } 1360 } 1361 1362 func TestPromTextParser_parseToMetricFamiliesWithSelector(t *testing.T) { 1363 sr, err := selector.Parse(`test_gauge_metric_1{label1="value2"}`) 1364 require.NoError(t, err) 1365 1366 p := promTextParser{sr: sr} 1367 1368 txt := []byte(` 1369 test_gauge_metric_1{label1="value1"} 1 1370 test_gauge_metric_1{label1="value2"} 1 1371 test_gauge_metric_2{label1="value1"} 1 1372 test_gauge_metric_2{label1="value2"} 1 1373 `) 1374 1375 want := MetricFamilies{ 1376 "test_gauge_metric_1": &MetricFamily{ 1377 name: "test_gauge_metric_1", 1378 typ: textparse.MetricTypeUnknown, 1379 metrics: []Metric{ 1380 {labels: labels.Labels{{Name: "label1", Value: "value2"}}, untyped: &Untyped{value: 1}}, 1381 }, 1382 }, 1383 } 1384 1385 mfs, err := p.parseToMetricFamilies(txt) 1386 1387 require.NoError(t, err) 1388 assert.Equal(t, want, mfs) 1389 } 1390 1391 func TestPromTextParser_parseToSeries(t *testing.T) { 1392 tests := map[string]struct { 1393 input []byte 1394 want Series 1395 }{ 1396 "All types": { 1397 input: []byte(` 1398 # HELP test_gauge_metric_1 Test Gauge Metric 1 1399 # TYPE test_gauge_metric_1 gauge 1400 test_gauge_metric_1{label1="value1"} 11 1401 test_gauge_no_meta_metric_1{label1="value1"} 11 1402 # HELP test_counter_metric_1_total Test Counter Metric 1 1403 # TYPE test_counter_metric_1_total counter 1404 test_counter_metric_1_total{label1="value1"} 11 1405 test_counter_no_meta_metric_1_total{label1="value1"} 11 1406 # HELP test_summary_1_duration_microseconds Test Summary Metric 1 1407 # TYPE test_summary_1_duration_microseconds summary 1408 test_summary_1_duration_microseconds{label1="value1",quantile="0.5"} 4931.921 1409 test_summary_1_duration_microseconds{label1="value1",quantile="0.9"} 4932.921 1410 test_summary_1_duration_microseconds{label1="value1",quantile="0.99"} 4933.921 1411 test_summary_1_duration_microseconds_sum{label1="value1"} 283201.29 1412 test_summary_1_duration_microseconds_count{label1="value1"} 31 1413 test_summary_no_meta_1_duration_microseconds{label1="value1",quantile="0.5"} 4931.921 1414 test_summary_no_meta_1_duration_microseconds{label1="value1",quantile="0.9"} 4932.921 1415 test_summary_no_meta_1_duration_microseconds{label1="value1",quantile="0.99"} 4933.921 1416 test_summary_no_meta_1_duration_microseconds_sum{label1="value1"} 283201.29 1417 test_summary_no_meta_1_duration_microseconds_count{label1="value1"} 31 1418 # HELP test_histogram_1_duration_seconds Test Histogram Metric 1 1419 # TYPE test_histogram_1_duration_seconds histogram 1420 test_histogram_1_duration_seconds_bucket{label1="value1",le="0.1"} 4 1421 test_histogram_1_duration_seconds_bucket{label1="value1",le="0.5"} 5 1422 test_histogram_1_duration_seconds_bucket{label1="value1",le="+Inf"} 6 1423 test_histogram_1_duration_seconds_sum{label1="value1"} 0.00147889 1424 test_histogram_1_duration_seconds_count{label1="value1"} 6 1425 test_histogram_no_meta_1_duration_seconds_bucket{label1="value1",le="0.1"} 4 1426 test_histogram_no_meta_1_duration_seconds_bucket{label1="value1",le="0.5"} 5 1427 test_histogram_no_meta_1_duration_seconds_bucket{label1="value1",le="+Inf"} 6 1428 test_histogram_no_meta_1_duration_seconds_sum{label1="value1"} 0.00147889 1429 test_histogram_no_meta_1_duration_seconds_count{label1="value1"} 6 1430 `), 1431 want: Series{ 1432 // Gauge 1433 { 1434 Labels: labels.Labels{ 1435 {Name: "__name__", Value: "test_gauge_metric_1"}, 1436 {Name: "label1", Value: "value1"}, 1437 }, 1438 Value: 11, 1439 }, 1440 { 1441 Labels: labels.Labels{ 1442 {Name: "__name__", Value: "test_gauge_no_meta_metric_1"}, 1443 {Name: "label1", Value: "value1"}, 1444 }, 1445 Value: 11, 1446 }, 1447 // Counter 1448 { 1449 Labels: labels.Labels{ 1450 {Name: "__name__", Value: "test_counter_metric_1_total"}, 1451 {Name: "label1", Value: "value1"}, 1452 }, 1453 Value: 11, 1454 }, 1455 { 1456 Labels: labels.Labels{ 1457 {Name: "__name__", Value: "test_counter_no_meta_metric_1_total"}, 1458 {Name: "label1", Value: "value1"}, 1459 }, 1460 Value: 11, 1461 }, 1462 //// Summary 1463 { 1464 Labels: labels.Labels{ 1465 {Name: "__name__", Value: "test_summary_1_duration_microseconds"}, 1466 {Name: "label1", Value: "value1"}, 1467 {Name: "quantile", Value: "0.5"}, 1468 }, 1469 Value: 4931.921, 1470 }, 1471 { 1472 Labels: labels.Labels{ 1473 {Name: "__name__", Value: "test_summary_1_duration_microseconds"}, 1474 {Name: "label1", Value: "value1"}, 1475 {Name: "quantile", Value: "0.9"}, 1476 }, 1477 Value: 4932.921, 1478 }, 1479 { 1480 Labels: labels.Labels{ 1481 {Name: "__name__", Value: "test_summary_1_duration_microseconds"}, 1482 {Name: "label1", Value: "value1"}, 1483 {Name: "quantile", Value: "0.99"}, 1484 }, 1485 Value: 4933.921, 1486 }, 1487 { 1488 Labels: labels.Labels{ 1489 {Name: "__name__", Value: "test_summary_1_duration_microseconds_sum"}, 1490 {Name: "label1", Value: "value1"}, 1491 }, 1492 Value: 283201.29, 1493 }, 1494 { 1495 Labels: labels.Labels{ 1496 {Name: "__name__", Value: "test_summary_1_duration_microseconds_count"}, 1497 {Name: "label1", Value: "value1"}, 1498 }, 1499 Value: 31, 1500 }, 1501 { 1502 Labels: labels.Labels{ 1503 {Name: "__name__", Value: "test_summary_no_meta_1_duration_microseconds"}, 1504 {Name: "label1", Value: "value1"}, 1505 {Name: "quantile", Value: "0.5"}, 1506 }, 1507 Value: 4931.921, 1508 }, 1509 { 1510 Labels: labels.Labels{ 1511 {Name: "__name__", Value: "test_summary_no_meta_1_duration_microseconds"}, 1512 {Name: "label1", Value: "value1"}, 1513 {Name: "quantile", Value: "0.9"}, 1514 }, 1515 Value: 4932.921, 1516 }, 1517 { 1518 Labels: labels.Labels{ 1519 {Name: "__name__", Value: "test_summary_no_meta_1_duration_microseconds"}, 1520 {Name: "label1", Value: "value1"}, 1521 {Name: "quantile", Value: "0.99"}, 1522 }, 1523 Value: 4933.921, 1524 }, 1525 { 1526 Labels: labels.Labels{ 1527 {Name: "__name__", Value: "test_summary_no_meta_1_duration_microseconds_sum"}, 1528 {Name: "label1", Value: "value1"}, 1529 }, 1530 Value: 283201.29, 1531 }, 1532 { 1533 Labels: labels.Labels{ 1534 {Name: "__name__", Value: "test_summary_no_meta_1_duration_microseconds_count"}, 1535 {Name: "label1", Value: "value1"}, 1536 }, 1537 Value: 31, 1538 }, 1539 // Histogram 1540 { 1541 Labels: labels.Labels{ 1542 {Name: "__name__", Value: "test_histogram_1_duration_seconds_bucket"}, 1543 {Name: "label1", Value: "value1"}, 1544 {Name: "le", Value: "0.1"}, 1545 }, 1546 Value: 4, 1547 }, 1548 { 1549 Labels: labels.Labels{ 1550 {Name: "__name__", Value: "test_histogram_1_duration_seconds_bucket"}, 1551 {Name: "label1", Value: "value1"}, 1552 {Name: "le", Value: "0.5"}, 1553 }, 1554 Value: 5, 1555 }, 1556 { 1557 Labels: labels.Labels{ 1558 {Name: "__name__", Value: "test_histogram_1_duration_seconds_bucket"}, 1559 {Name: "label1", Value: "value1"}, 1560 {Name: "le", Value: "+Inf"}, 1561 }, 1562 Value: 6, 1563 }, 1564 { 1565 Labels: labels.Labels{ 1566 {Name: "__name__", Value: "test_histogram_1_duration_seconds_sum"}, 1567 {Name: "label1", Value: "value1"}, 1568 }, 1569 Value: 0.00147889, 1570 }, 1571 { 1572 Labels: labels.Labels{ 1573 {Name: "__name__", Value: "test_histogram_1_duration_seconds_count"}, 1574 {Name: "label1", Value: "value1"}, 1575 }, 1576 Value: 6, 1577 }, 1578 1579 { 1580 Labels: labels.Labels{ 1581 {Name: "__name__", Value: "test_histogram_no_meta_1_duration_seconds_bucket"}, 1582 {Name: "label1", Value: "value1"}, 1583 {Name: "le", Value: "0.1"}, 1584 }, 1585 Value: 4, 1586 }, 1587 { 1588 Labels: labels.Labels{ 1589 {Name: "__name__", Value: "test_histogram_no_meta_1_duration_seconds_bucket"}, 1590 {Name: "label1", Value: "value1"}, 1591 {Name: "le", Value: "0.5"}, 1592 }, 1593 Value: 5, 1594 }, 1595 { 1596 Labels: labels.Labels{ 1597 {Name: "__name__", Value: "test_histogram_no_meta_1_duration_seconds_bucket"}, 1598 {Name: "label1", Value: "value1"}, 1599 {Name: "le", Value: "+Inf"}, 1600 }, 1601 Value: 6, 1602 }, 1603 { 1604 Labels: labels.Labels{ 1605 {Name: "__name__", Value: "test_histogram_no_meta_1_duration_seconds_sum"}, 1606 {Name: "label1", Value: "value1"}, 1607 }, 1608 Value: 0.00147889, 1609 }, 1610 { 1611 Labels: labels.Labels{ 1612 {Name: "__name__", Value: "test_histogram_no_meta_1_duration_seconds_count"}, 1613 {Name: "label1", Value: "value1"}, 1614 }, 1615 Value: 6, 1616 }, 1617 }, 1618 }, 1619 } 1620 1621 for name, test := range tests { 1622 t.Run(name, func(t *testing.T) { 1623 var p promTextParser 1624 1625 for i := 0; i < 10; i++ { 1626 t.Run(fmt.Sprintf("parse num %d", i+1), func(t *testing.T) { 1627 series, err := p.parseToSeries(test.input) 1628 1629 if len(test.want) > 0 { 1630 test.want.Sort() 1631 assert.Equal(t, test.want, series) 1632 } else { 1633 assert.Error(t, err) 1634 } 1635 }) 1636 } 1637 }) 1638 } 1639 } 1640 1641 func TestPromTextParser_parseToSeriesWithSelector(t *testing.T) { 1642 sr, err := selector.Parse(`test_gauge_metric_1{label1="value2"}`) 1643 require.NoError(t, err) 1644 1645 p := promTextParser{sr: sr} 1646 1647 txt := []byte(` 1648 test_gauge_metric_1{label1="value1"} 1 1649 test_gauge_metric_1{label1="value2"} 1 1650 test_gauge_metric_2{label1="value1"} 1 1651 test_gauge_metric_2{label1="value2"} 1 1652 `) 1653 1654 want := Series{SeriesSample{ 1655 Labels: labels.Labels{ 1656 {Name: "__name__", Value: "test_gauge_metric_1"}, 1657 {Name: "label1", Value: "value2"}, 1658 }, 1659 Value: 1, 1660 }} 1661 1662 series, err := p.parseToSeries(txt) 1663 1664 require.NoError(t, err) 1665 assert.Equal(t, want, series) 1666 } 1667 1668 func joinData(data ...[]byte) []byte { 1669 var buf bytes.Buffer 1670 for _, v := range data { 1671 _, _ = buf.Write(v) 1672 _ = buf.WriteByte('\n') 1673 } 1674 return buf.Bytes() 1675 }