MCPcopy
hub / github.com/prometheus/client_golang / ExampleGatherers

Function ExampleGatherers

prometheus/examples_test.go:575–686  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

573}
574
575func 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 %.
593humidity_percent{location="outside"} 45.4
594humidity_percent{location="inside"} 33.2
595# TYPE temperature_kelvin gauge
596# HELP temperature_kelvin Temperature in Kelvin.
597temperature_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

Callers

nothing calls this directly

Calls 11

MustRegisterMethod · 0.95
WithLabelValuesMethod · 0.95
GatherMethod · 0.95
NewRegistryFunction · 0.92
NewGaugeVecFunction · 0.92
GathererFuncFuncType · 0.92
SetMethod · 0.65
PrintlnMethod · 0.65
StringMethod · 0.45
ErrorMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected