Skip to content

Commit 32d75cf

Browse files
committed
Remove redundant cacheMut lock from prometheus.relabel
1 parent 90a9504 commit 32d75cf

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

internal/component/prometheus/relabel/relabel.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ type Component struct {
9090

9191
debugDataPublisher livedebugging.DebugDataPublisher
9292

93-
cacheMut sync.RWMutex
94-
cache *lru.Cache[uint64, labels.Labels]
93+
cache *lru.Cache[uint64, labels.Labels]
9594
}
9695

9796
var (
@@ -299,35 +298,23 @@ func (c *Component) relabel(val float64, lbls labels.Labels) labels.Labels {
299298
}
300299

301300
func (c *Component) getFromCache(lbls labels.Labels) (labels.Labels, bool) {
302-
c.cacheMut.RLock()
303-
defer c.cacheMut.RUnlock()
304-
305301
hash := lbls.Hash()
306-
fm, found := c.cache.Get(hash)
307-
308-
return fm, found
302+
return c.cache.Get(hash)
309303
}
310304

311305
func (c *Component) deleteFromCache(lbls labels.Labels) {
312-
c.cacheMut.Lock()
313-
defer c.cacheMut.Unlock()
314306
c.cacheDeletes.Inc()
315307

316308
hash := lbls.Hash()
317309
c.cache.Remove(hash)
318310
}
319311

320312
func (c *Component) clearCache(cacheSize int) {
321-
c.cacheMut.Lock()
322-
defer c.cacheMut.Unlock()
323313
cache, _ := lru.New[uint64, labels.Labels](cacheSize)
324314
c.cache = cache
325315
}
326316

327317
func (c *Component) addToCache(lbls labels.Labels, relabeled labels.Labels, keep bool) {
328-
c.cacheMut.Lock()
329-
defer c.cacheMut.Unlock()
330-
331318
hash := lbls.Hash()
332319
if !keep {
333320
c.cache.Add(hash, labels.EmptyLabels())

internal/component/prometheus/relabel/relabel_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,45 @@ func TestMetrics(t *testing.T) {
113113
require.True(t, *(m.Counter.Value) == 1)
114114
}
115115

116+
func BenchmarkCacheParallel(b *testing.B) {
117+
fanout := prometheus.NewInterceptor(nil, prometheus.WithAppendHook(func(ref storage.SeriesRef, l labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) {
118+
return ref, nil
119+
}))
120+
var entry storage.Appendable
121+
_, err := New(component.Options{
122+
ID: "1",
123+
Logger: util.TestAlloyLogger(b),
124+
OnStateChange: func(e component.Exports) {
125+
newE := e.(Exports)
126+
entry = newE.Receiver
127+
},
128+
Registerer: prom.NewRegistry(),
129+
GetServiceData: getServiceData,
130+
}, Arguments{
131+
ForwardTo: []storage.Appendable{fanout},
132+
MetricRelabelConfigs: []*alloy_relabel.Config{
133+
{
134+
SourceLabels: []string{"__address__"},
135+
Regex: alloy_relabel.Regexp(relabel.MustNewRegexp("(.+)")),
136+
TargetLabel: "new_label",
137+
Replacement: "new_value",
138+
Action: "replace",
139+
},
140+
},
141+
CacheSize: 100_000,
142+
})
143+
require.NoError(b, err)
144+
145+
lbls := labels.FromStrings("__address__", "localhost")
146+
b.RunParallel(func(pb *testing.PB) {
147+
app := entry.Appender(b.Context())
148+
for pb.Next() {
149+
app.Append(0, lbls, time.Now().UnixMilli(), 0)
150+
}
151+
app.Commit()
152+
})
153+
}
154+
116155
func BenchmarkCache(b *testing.B) {
117156
fanout := prometheus.NewInterceptor(nil, prometheus.WithAppendHook(func(ref storage.SeriesRef, l labels.Labels, _ int64, _ float64, _ storage.Appender) (storage.SeriesRef, error) {
118157
require.True(b, l.Has("new_label"))

0 commit comments

Comments
 (0)