mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-21 03:16:00 -08:00
Merge pull request #9518 from prometheus/beorn7/labels
scrape: Avoid creating a label map during conflict resolution
This commit is contained in:
commit
94a9156cc4
|
@ -641,16 +641,17 @@ func verifyLabelLimits(lset labels.Labels, limits *labelLimits) error {
|
||||||
|
|
||||||
func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*relabel.Config) labels.Labels {
|
func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*relabel.Config) labels.Labels {
|
||||||
lb := labels.NewBuilder(lset)
|
lb := labels.NewBuilder(lset)
|
||||||
|
targetLabels := target.Labels()
|
||||||
|
|
||||||
if honor {
|
if honor {
|
||||||
for _, l := range target.Labels() {
|
for _, l := range targetLabels {
|
||||||
if !lset.Has(l.Name) {
|
if !lset.Has(l.Name) {
|
||||||
lb.Set(l.Name, l.Value)
|
lb.Set(l.Name, l.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var conflictingExposedLabels labels.Labels
|
var conflictingExposedLabels labels.Labels
|
||||||
for _, l := range target.Labels() {
|
for _, l := range targetLabels {
|
||||||
existingValue := lset.Get(l.Name)
|
existingValue := lset.Get(l.Name)
|
||||||
if existingValue != "" {
|
if existingValue != "" {
|
||||||
conflictingExposedLabels = append(conflictingExposedLabels, labels.Label{Name: l.Name, Value: existingValue})
|
conflictingExposedLabels = append(conflictingExposedLabels, labels.Label{Name: l.Name, Value: existingValue})
|
||||||
|
@ -660,7 +661,7 @@ func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*re
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(conflictingExposedLabels) > 0 {
|
if len(conflictingExposedLabels) > 0 {
|
||||||
resolveConflictingExposedLabels(lb, conflictingExposedLabels)
|
resolveConflictingExposedLabels(lb, lset, targetLabels, conflictingExposedLabels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -673,36 +674,27 @@ func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*re
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveConflictingExposedLabels(lb *labels.Builder, conflictingExposedLabels labels.Labels) {
|
func resolveConflictingExposedLabels(lb *labels.Builder, exposedLabels, targetLabels, conflictingExposedLabels labels.Labels) {
|
||||||
sort.SliceStable(conflictingExposedLabels, func(i, j int) bool {
|
sort.SliceStable(conflictingExposedLabels, func(i, j int) bool {
|
||||||
return len(conflictingExposedLabels[i].Name) < len(conflictingExposedLabels[j].Name)
|
return len(conflictingExposedLabels[i].Name) < len(conflictingExposedLabels[j].Name)
|
||||||
})
|
})
|
||||||
|
|
||||||
allLabelNames := map[string]struct{}{}
|
for i, l := range conflictingExposedLabels {
|
||||||
for _, v := range lb.Labels() {
|
newName := l.Name
|
||||||
allLabelNames[v.Name] = struct{}{}
|
for {
|
||||||
}
|
newName = model.ExportedLabelPrefix + newName
|
||||||
|
if !exposedLabels.Has(newName) &&
|
||||||
resolved := createNewLabels(allLabelNames, conflictingExposedLabels, nil)
|
!targetLabels.Has(newName) &&
|
||||||
for _, l := range resolved {
|
!conflictingExposedLabels[:i].Has(newName) {
|
||||||
lb.Set(l.Name, l.Value)
|
conflictingExposedLabels[i].Name = newName
|
||||||
}
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNewLabels(existingNames map[string]struct{}, conflictingLabels, resolvedLabels labels.Labels) labels.Labels {
|
|
||||||
for i := 0; i < len(conflictingLabels); i++ {
|
|
||||||
newName := model.ExportedLabelPrefix + conflictingLabels[i].Name
|
|
||||||
if _, ok := existingNames[newName]; !ok {
|
|
||||||
resolvedLabels = append(resolvedLabels, labels.Label{Name: newName, Value: conflictingLabels[i].Value})
|
|
||||||
conflictingLabels = append(conflictingLabels[:i], conflictingLabels[i+1:]...)
|
|
||||||
i--
|
|
||||||
existingNames[newName] = struct{}{}
|
|
||||||
} else {
|
|
||||||
conflictingLabels[i] = labels.Label{Name: newName, Value: conflictingLabels[i].Value}
|
|
||||||
return createNewLabels(existingNames, conflictingLabels, resolvedLabels)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resolvedLabels
|
|
||||||
|
for _, l := range conflictingExposedLabels {
|
||||||
|
lb.Set(l.Name, l.Value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mutateReportSampleLabels(lset labels.Labels, target *Target) labels.Labels {
|
func mutateReportSampleLabels(lset labels.Labels, target *Target) labels.Labels {
|
||||||
|
|
Loading…
Reference in a new issue