()
| 573 | } |
| 574 | |
| 575 | func ExampleGatherers() { |
| 576 | reg := prometheus.NewRegistry() |
| 577 | temp := prometheus.NewGaugeVec( |
| 578 | prometheus.GaugeOpts{ |
| 579 | Name: "temperature_kelvin", |
| 580 | Help: "Temperature in Kelvin.", |
| 581 | }, |
| 582 | []string{"location"}, |
| 583 | ) |
| 584 | reg.MustRegister(temp) |
| 585 | temp.WithLabelValues("outside").Set(273.14) |
| 586 | temp.WithLabelValues("inside").Set(298.44) |
| 587 | |
| 588 | parser := expfmt.NewTextParser(model.UTF8Validation) |
| 589 | |
| 590 | text := ` |
| 591 | # TYPE humidity_percent gauge |
| 592 | # HELP humidity_percent Humidity in %. |
| 593 | humidity_percent{location="outside"} 45.4 |
| 594 | humidity_percent{location="inside"} 33.2 |
| 595 | # TYPE temperature_kelvin gauge |
| 596 | # HELP temperature_kelvin Temperature in Kelvin. |
| 597 | temperature_kelvin{location="somewhere else"} 4.5 |
| 598 | ` |
| 599 | |
| 600 | parseText := func() ([]*dto.MetricFamily, error) { |
| 601 | parsed, err := parser.TextToMetricFamilies(strings.NewReader(text)) |
| 602 | if err != nil { |
| 603 | return nil, err |
| 604 | } |
| 605 | var result []*dto.MetricFamily |
| 606 | for _, mf := range parsed { |
| 607 | result = append(result, mf) |
| 608 | } |
| 609 | return result, nil |
| 610 | } |
| 611 | |
| 612 | gatherers := prometheus.Gatherers{ |
| 613 | reg, |
| 614 | prometheus.GathererFunc(parseText), |
| 615 | } |
| 616 | |
| 617 | gathering, err := gatherers.Gather() |
| 618 | if err != nil { |
| 619 | fmt.Println(err) |
| 620 | } |
| 621 | |
| 622 | out := &bytes.Buffer{} |
| 623 | for _, mf := range gathering { |
| 624 | if _, err := expfmt.MetricFamilyToText(out, mf); err != nil { |
| 625 | panic(err) |
| 626 | } |
| 627 | } |
| 628 | fmt.Print(out.String()) |
| 629 | fmt.Println("----------") |
| 630 | |
| 631 | // Note how the temperature_kelvin metric family has been merged from |
| 632 | // different sources. Now try |
nothing calls this directly
no test coverage detected