mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
K8s: Merge metric registries (#79256)
This commit is contained in:
parent
1833539c32
commit
89934b0884
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"sort"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
@ -68,8 +69,10 @@ func ProvideRegisterer(cfg *setting.Cfg) prometheus.Registerer {
|
||||
|
||||
func ProvideGatherer(cfg *setting.Cfg) prometheus.Gatherer {
|
||||
if cfg.IsFeatureToggleEnabled(featuremgmt.FlagGrafanaAPIServer) {
|
||||
return newAddPrefixWrapper(legacyregistry.DefaultGatherer)
|
||||
k8sGatherer := newAddPrefixWrapper(legacyregistry.DefaultGatherer)
|
||||
return newMultiRegistry(k8sGatherer, prometheus.DefaultGatherer)
|
||||
}
|
||||
|
||||
return prometheus.DefaultGatherer
|
||||
}
|
||||
|
||||
@ -122,3 +125,40 @@ func (g *addPrefixWrapper) Gather() ([]*dto.MetricFamily, error) {
|
||||
|
||||
return mf, nil
|
||||
}
|
||||
|
||||
var _ prometheus.Gatherer = (*multiRegistry)(nil)
|
||||
|
||||
type multiRegistry struct {
|
||||
gatherers []prometheus.Gatherer
|
||||
}
|
||||
|
||||
func newMultiRegistry(gatherers ...prometheus.Gatherer) *multiRegistry {
|
||||
return &multiRegistry{
|
||||
gatherers: gatherers,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *multiRegistry) Gather() (mfs []*dto.MetricFamily, err error) {
|
||||
errs := prometheus.MultiError{}
|
||||
|
||||
names := make(map[string]struct{})
|
||||
for _, g := range r.gatherers {
|
||||
mf, err := g.Gather()
|
||||
errs.Append(err)
|
||||
|
||||
for i := 0; i < len(mf); i++ {
|
||||
m := mf[i]
|
||||
// prevent duplicate metric names
|
||||
if _, exists := names[*m.Name]; !exists {
|
||||
names[*m.Name] = struct{}{}
|
||||
mfs = append(mfs, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(mfs, func(i, j int) bool {
|
||||
return *mfs[i].Name < *mfs[j].Name
|
||||
})
|
||||
|
||||
return mfs, errs.MaybeUnwrap()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user