scraping: pass a Builder to get Target labels

This saves memory allocations from making a new Builder every time.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
pull/12304/head
Bryan Boreham 2 years ago
parent eff3a13e19
commit 9ba13de220

@ -169,8 +169,8 @@ func (t *Target) offset(interval time.Duration, offsetSeed uint64) time.Duration
} }
// Labels returns a copy of the set of all public labels of the target. // Labels returns a copy of the set of all public labels of the target.
func (t *Target) Labels() labels.Labels { func (t *Target) Labels(b *labels.ScratchBuilder) labels.Labels {
b := labels.NewScratchBuilder(t.labels.Len()) b.Reset()
t.labels.Range(func(l labels.Label) { t.labels.Range(func(l labels.Label) {
if !strings.HasPrefix(l.Name, model.ReservedLabelPrefix) { if !strings.HasPrefix(l.Name, model.ReservedLabelPrefix) {
b.Add(l.Name, l.Value) b.Add(l.Name, l.Value)

@ -42,7 +42,8 @@ const (
func TestTargetLabels(t *testing.T) { func TestTargetLabels(t *testing.T) {
target := newTestTarget("example.com:80", 0, labels.FromStrings("job", "some_job", "foo", "bar")) target := newTestTarget("example.com:80", 0, labels.FromStrings("job", "some_job", "foo", "bar"))
want := labels.FromStrings(model.JobLabel, "some_job", "foo", "bar") want := labels.FromStrings(model.JobLabel, "some_job", "foo", "bar")
got := target.Labels() b := labels.NewScratchBuilder(0)
got := target.Labels(&b)
require.Equal(t, want, got) require.Equal(t, want, got)
i := 0 i := 0
target.LabelsRange(func(l labels.Label) { target.LabelsRange(func(l labels.Label) {

@ -1009,6 +1009,7 @@ func (api *API) targets(r *http.Request) apiFuncResult {
targetsActive := api.targetRetriever(r.Context()).TargetsActive() targetsActive := api.targetRetriever(r.Context()).TargetsActive()
activeKeys, numTargets := sortKeys(targetsActive) activeKeys, numTargets := sortKeys(targetsActive)
res.ActiveTargets = make([]*Target, 0, numTargets) res.ActiveTargets = make([]*Target, 0, numTargets)
builder := labels.NewScratchBuilder(0)
for _, key := range activeKeys { for _, key := range activeKeys {
if scrapePool != "" && key != scrapePool { if scrapePool != "" && key != scrapePool {
@ -1025,7 +1026,7 @@ func (api *API) targets(r *http.Request) apiFuncResult {
res.ActiveTargets = append(res.ActiveTargets, &Target{ res.ActiveTargets = append(res.ActiveTargets, &Target{
DiscoveredLabels: target.DiscoveredLabels(), DiscoveredLabels: target.DiscoveredLabels(),
Labels: target.Labels(), Labels: target.Labels(&builder),
ScrapePool: key, ScrapePool: key,
ScrapeURL: target.URL().String(), ScrapeURL: target.URL().String(),
GlobalURL: globalURL.String(), GlobalURL: globalURL.String(),
@ -1101,6 +1102,7 @@ func (api *API) targetMetadata(r *http.Request) apiFuncResult {
} }
} }
builder := labels.NewScratchBuilder(0)
metric := r.FormValue("metric") metric := r.FormValue("metric")
res := []metricMetadata{} res := []metricMetadata{}
for _, tt := range api.targetRetriever(r.Context()).TargetsActive() { for _, tt := range api.targetRetriever(r.Context()).TargetsActive() {
@ -1108,15 +1110,16 @@ func (api *API) targetMetadata(r *http.Request) apiFuncResult {
if limit >= 0 && len(res) >= limit { if limit >= 0 && len(res) >= limit {
break break
} }
targetLabels := t.Labels(&builder)
// Filter targets that don't satisfy the label matchers. // Filter targets that don't satisfy the label matchers.
if matchTarget != "" && !matchLabels(t.Labels(), matchers) { if matchTarget != "" && !matchLabels(targetLabels, matchers) {
continue continue
} }
// If no metric is specified, get the full list for the target. // If no metric is specified, get the full list for the target.
if metric == "" { if metric == "" {
for _, md := range t.ListMetadata() { for _, md := range t.ListMetadata() {
res = append(res, metricMetadata{ res = append(res, metricMetadata{
Target: t.Labels(), Target: targetLabels,
Metric: md.Metric, Metric: md.Metric,
Type: md.Type, Type: md.Type,
Help: md.Help, Help: md.Help,
@ -1128,7 +1131,7 @@ func (api *API) targetMetadata(r *http.Request) apiFuncResult {
// Get metadata for the specified metric. // Get metadata for the specified metric.
if md, ok := t.GetMetadata(metric); ok { if md, ok := t.GetMetadata(metric); ok {
res = append(res, metricMetadata{ res = append(res, metricMetadata{
Target: t.Labels(), Target: targetLabels,
Type: md.Type, Type: md.Type,
Help: md.Help, Help: md.Help,
Unit: md.Unit, Unit: md.Unit,

Loading…
Cancel
Save