-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert plugin config to scraper config
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/prometheus/prometheus/model/labels" | ||
) | ||
|
||
var ( | ||
sampleMutator func(labels.Labels) labels.Labels //nolint:unused | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/prometheus/common/model" | ||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/model/relabel" | ||
"github.com/prometheus/prometheus/scrape" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
func mutateSampleLabels(lset labels.Labels, target *scrape.Target, honor bool, rc []*relabel.Config) labels.Labels { | ||
lb := labels.NewBuilder(lset) | ||
|
||
if honor { | ||
target.LabelsRange(func(l labels.Label) { | ||
if !lset.Has(l.Name) { | ||
lb.Set(l.Name, l.Value) | ||
} | ||
}) | ||
} else { | ||
var conflictingExposedLabels []labels.Label | ||
target.LabelsRange(func(l labels.Label) { | ||
existingValue := lset.Get(l.Name) | ||
if existingValue != "" { | ||
conflictingExposedLabels = append(conflictingExposedLabels, labels.Label{Name: l.Name, Value: existingValue}) | ||
} | ||
// It is now safe to set the target label. | ||
lb.Set(l.Name, l.Value) | ||
}) | ||
|
||
if len(conflictingExposedLabels) > 0 { | ||
resolveConflictingExposedLabels(lb, conflictingExposedLabels) | ||
} | ||
} | ||
|
||
res := lb.Labels() | ||
|
||
if len(rc) > 0 { | ||
res, _ = relabel.Process(res, rc...) | ||
} | ||
|
||
return res | ||
} | ||
|
||
func resolveConflictingExposedLabels(lb *labels.Builder, conflictingExposedLabels []labels.Label) { | ||
slices.SortStableFunc(conflictingExposedLabels, func(a, b labels.Label) bool { | ||
return len(a.Name) < len(b.Name) | ||
}) | ||
|
||
for _, l := range conflictingExposedLabels { | ||
newName := l.Name | ||
for { | ||
newName = model.ExportedLabelPrefix + newName | ||
if lb.Get(newName) == "" { | ||
lb.Set(newName, l.Value) | ||
break | ||
} | ||
} | ||
} | ||
} |