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

Method Gather

prometheus/registry.go:746–803  ·  view source on GitHub ↗

Gather implements Gatherer.

()

Source from the content-addressed store, hash-verified

744
745// Gather implements Gatherer.
746func (gs Gatherers) Gather() ([]*dto.MetricFamily, error) {
747 var (
748 metricFamiliesByName = map[string]*dto.MetricFamily{}
749 metricHashes = map[uint64]struct{}{}
750 errs MultiError // The collected errors to return in the end.
751 )
752
753 for i, g := range gs {
754 mfs, err := g.Gather()
755 if err != nil {
756 multiErr := MultiError{}
757 if errors.As(err, &multiErr) {
758 for _, err := range multiErr {
759 errs = append(errs, fmt.Errorf("[from Gatherer #%d] %w", i+1, err))
760 }
761 } else {
762 errs = append(errs, fmt.Errorf("[from Gatherer #%d] %w", i+1, err))
763 }
764 }
765 for _, mf := range mfs {
766 existingMF, exists := metricFamiliesByName[mf.GetName()]
767 if exists {
768 if existingMF.GetHelp() != mf.GetHelp() {
769 errs = append(errs, fmt.Errorf(
770 "gathered metric family %s has help %q but should have %q",
771 mf.GetName(), mf.GetHelp(), existingMF.GetHelp(),
772 ))
773 continue
774 }
775 if existingMF.GetType() != mf.GetType() {
776 errs = append(errs, fmt.Errorf(
777 "gathered metric family %s has type %s but should have %s",
778 mf.GetName(), mf.GetType(), existingMF.GetType(),
779 ))
780 continue
781 }
782 } else {
783 existingMF = &dto.MetricFamily{}
784 existingMF.Name = mf.Name
785 existingMF.Help = mf.Help
786 existingMF.Type = mf.Type
787 if err := checkSuffixCollisions(existingMF, metricFamiliesByName); err != nil {
788 errs = append(errs, err)
789 continue
790 }
791 metricFamiliesByName[mf.GetName()] = existingMF
792 }
793 for _, m := range mf.Metric {
794 if err := checkMetricConsistency(existingMF, m, metricHashes); err != nil {
795 errs = append(errs, err)
796 continue
797 }
798 existingMF.Metric = append(existingMF.Metric, m)
799 }
800 }
801 }
802 return internal.NormalizeMetricFamilies(metricFamiliesByName), errs.MaybeUnwrap()
803}

Callers 1

ExampleGatherersFunction · 0.95

Calls 6

NormalizeMetricFamiliesFunction · 0.92
checkSuffixCollisionsFunction · 0.85
checkMetricConsistencyFunction · 0.85
GetTypeMethod · 0.80
MaybeUnwrapMethod · 0.80
GatherMethod · 0.65

Tested by 1

ExampleGatherersFunction · 0.76