MCPcopy Create free account
hub / github.com/cortexproject/cortex / GetLabels

Function GetLabels

pkg/util/metrics_helper.go:772–810  ·  view source on GitHub ↗

GetLabels returns list of label combinations used by this collector at the time of call. This can be used to find and delete unused metrics.

(c prometheus.Collector, filter map[string]string)

Source from the content-addressed store, hash-verified

770// GetLabels returns list of label combinations used by this collector at the time of call.
771// This can be used to find and delete unused metrics.
772func GetLabels(c prometheus.Collector, filter map[string]string) ([]labels.Labels, error) {
773 ch := make(chan prometheus.Metric, 16)
774
775 go func() {
776 defer close(ch)
777 c.Collect(ch)
778 }()
779
780 errs := tsdb_errors.NewMulti()
781 var result []labels.Labels
782 dtoMetric := &dto.Metric{}
783 lbls := labels.NewBuilder(labels.EmptyLabels())
784
785nextMetric:
786 for m := range ch {
787 err := m.Write(dtoMetric)
788 if err != nil {
789 errs.Add(err)
790 // We cannot return here, to avoid blocking goroutine calling c.Collect()
791 continue
792 }
793
794 lbls.Reset(labels.EmptyLabels())
795 for _, lp := range dtoMetric.Label {
796 n := lp.GetName()
797 v := lp.GetValue()
798
799 filterValue, ok := filter[n]
800 if ok && filterValue != v {
801 continue nextMetric
802 }
803
804 lbls.Set(lp.GetName(), lp.GetValue())
805 }
806 result = append(result, lbls.Labels())
807 }
808
809 return result, errs.Err()
810}
811
812// DeleteMatchingLabels removes metric with labels matching the filter.
813func DeleteMatchingLabels(c CollectorVec, filter map[string]string) error {

Callers 5

DeleteMatchingLabelsFunction · 0.85
verifyLabelsFunction · 0.85

Calls 9

SetMethod · 0.65
ErrMethod · 0.65
CollectMethod · 0.45
WriteMethod · 0.45
AddMethod · 0.45
ResetMethod · 0.45
GetNameMethod · 0.45
GetValueMethod · 0.45
LabelsMethod · 0.45

Tested by 3

verifyLabelsFunction · 0.68