Skip to content

Commit e03a841

Browse files
committed
fix(target-allocator): fix the potential issue of duplicate targets (#3617)
1 parent 0edaa4a commit e03a841

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.chloggen/item_hash_cal.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: target allocator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Compute hash based on the relabeled label set to maintain Prometheus compatibility and resolve duplication issues (#3617)
9+
10+
# One or more tracking issues related to the change
11+
issues: []
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

cmd/otel-allocator/internal/prehook/relabel.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package prehook
55

66
import (
7+
"github.com/prometheus/common/model"
78
"slices"
89

910
"github.com/go-logr/logr"
@@ -48,6 +49,14 @@ func (tf *relabelConfigTargetFilter) Apply(targets []*target.Item) []*target.Ite
4849
}
4950

5051
if keepTarget {
52+
// Only if the key model.AddressLabel remains after relabeling is the value considered valid.
53+
// For detail, see https://github.com/prometheus/prometheus/blob/e6cfa720fbe6280153fab13090a483dbd40bece3/scrape/target.go#L457
54+
if address := lset.Get(model.AddressLabel); len(address) != 0 {
55+
hash := lset.Hash()
56+
tItem.HashFunc = func() uint64 {
57+
return hash
58+
}
59+
}
5160
targets[writeIndex] = tItem
5261
writeIndex++
5362
}

cmd/otel-allocator/internal/target/target.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ type Item struct {
2929
Labels labels.Labels
3030
CollectorName string
3131
hash ItemHash
32+
HashFunc func() uint64
3233
}
3334

3435
func (t *Item) Hash() ItemHash {
3536
if t.hash == 0 {
36-
t.hash = ItemHash(t.Labels.Hash())
37+
hashFunc := t.Labels.Hash
38+
if t.HashFunc != nil {
39+
hashFunc = t.HashFunc
40+
// Release the reference to avoid unintentionally retaining large objects.
41+
t.HashFunc = nil
42+
}
43+
t.hash = ItemHash(hashFunc())
3744
}
3845
return t.hash
3946
}
@@ -55,7 +62,7 @@ func (t *Item) GetNodeName() string {
5562

5663
// NewItem Creates a new target item.
5764
// INVARIANTS:
58-
// * Item fields must not be modified after creation.
65+
// * Item fields initialized in NewItem must not be modified after creation.
5966
func NewItem(jobName string, targetURL string, labels labels.Labels, collectorName string) *Item {
6067
return &Item{
6168
JobName: jobName,

0 commit comments

Comments
 (0)