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)
| 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. |
| 772 | func 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 | |
| 785 | nextMetric: |
| 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. |
| 813 | func DeleteMatchingLabels(c CollectorVec, filter map[string]string) error { |