sanitizeLabels ensures that all the labels in groupingLabels and the `instance` label are present in the MetricFamily. The label values from groupingLabels are set in each Metric, no matter what. After that, if the 'instance' label is not present at all in a Metric, it will be created (with an empty
(mf *dto.MetricFamily, groupingLabels map[string]string)
| 536 | // |
| 537 | // Finally, sanitizeLabels sorts the label pairs of all metrics. |
| 538 | func sanitizeLabels(mf *dto.MetricFamily, groupingLabels map[string]string) { |
| 539 | gLabelsNotYetDone := make(map[string]string, len(groupingLabels)) |
| 540 | |
| 541 | metric: |
| 542 | for _, m := range mf.GetMetric() { |
| 543 | maps.Copy(gLabelsNotYetDone, groupingLabels) |
| 544 | hasInstanceLabel := false |
| 545 | for _, lp := range m.GetLabel() { |
| 546 | ln := lp.GetName() |
| 547 | if lv, ok := gLabelsNotYetDone[ln]; ok { |
| 548 | lp.Value = proto.String(lv) |
| 549 | delete(gLabelsNotYetDone, ln) |
| 550 | } |
| 551 | if ln == string(model.InstanceLabel) { |
| 552 | hasInstanceLabel = true |
| 553 | } |
| 554 | if len(gLabelsNotYetDone) == 0 && hasInstanceLabel { |
| 555 | sort.Sort(labelPairs(m.Label)) |
| 556 | continue metric |
| 557 | } |
| 558 | } |
| 559 | for ln, lv := range gLabelsNotYetDone { |
| 560 | m.Label = append(m.Label, &dto.LabelPair{ |
| 561 | Name: proto.String(ln), |
| 562 | Value: proto.String(lv), |
| 563 | }) |
| 564 | if ln == string(model.InstanceLabel) { |
| 565 | hasInstanceLabel = true |
| 566 | } |
| 567 | delete(gLabelsNotYetDone, ln) // To prepare map for next metric. |
| 568 | } |
| 569 | if !hasInstanceLabel { |
| 570 | m.Label = append(m.Label, &dto.LabelPair{ |
| 571 | Name: proto.String(string(model.InstanceLabel)), |
| 572 | Value: proto.String(""), |
| 573 | }) |
| 574 | } |
| 575 | sort.Sort(labelPairs(m.Label)) |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // Checks if any timestamps have been specified. |
| 580 | func timestampsPresent(metricFamilies map[string]*dto.MetricFamily) bool { |
no test coverage detected
searching dependent graphs…