Skip to content

Commit a2945c8

Browse files
craig[bot]aa-joshi
andcommitted
Merge #143728
143728: aggmetric: update hash key for cache child metric r=aa-joshi a=aa-joshi This patch updates hash key generation for child metric with cache storage. It relies on `fnv` package to generate hash for given label values. Epic: CRDB-43153 Part of: CRDB-48253 Release note: None Co-authored-by: Akshay Joshi <[email protected]>
2 parents 358fec6 + 6a34a42 commit a2945c8

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

pkg/util/metric/aggmetric/agg_metric.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package aggmetric
1010

1111
import (
12+
"hash/fnv"
1213
"strings"
1314

1415
"github.com/cockroachdb/cockroach/pkg/util/cache"
@@ -19,6 +20,8 @@ import (
1920
io_prometheus_client "github.com/prometheus/client_model/go"
2021
)
2122

23+
var delimiter = []byte{'_'}
24+
2225
// Builder is used to ease constructing metrics with the same labels.
2326
type Builder struct {
2427
labels []string
@@ -185,8 +188,13 @@ type labelValuesSlice []string
185188

186189
func (lv *labelValuesSlice) labelValues() []string { return []string(*lv) }
187190

188-
func metricKey(labels ...string) string {
189-
return strings.Join(labels, ",")
191+
func metricKey(labels ...string) uint64 {
192+
hash := fnv.New64a()
193+
for _, label := range labels {
194+
_, _ = hash.Write([]byte(label))
195+
_, _ = hash.Write(delimiter)
196+
}
197+
return hash.Sum64()
190198
}
191199

192200
type ChildrenStorage interface {
@@ -210,27 +218,27 @@ func (ucw *UnorderedCacheWrapper) GetChildMetric(e interface{}) ChildMetric {
210218
}
211219

212220
func (ucw *UnorderedCacheWrapper) Get(labelVals ...string) (ChildMetric, bool) {
213-
cacheKey := metricKey(labelVals...)
214-
value, ok := ucw.cache.Get(cacheKey)
221+
hashKey := metricKey(labelVals...)
222+
value, ok := ucw.cache.Get(hashKey)
215223
if !ok {
216224
return nil, false
217225
}
218226
return value.(ChildMetric), ok
219227
}
220228

221229
func (ucw *UnorderedCacheWrapper) Add(metric ChildMetric) {
222-
lvs := metric.labelValues()
223-
key := metricKey(lvs...)
224-
if _, ok := ucw.cache.Get(key); ok {
230+
labelValues := metric.labelValues()
231+
hashKey := metricKey(labelValues...)
232+
if _, ok := ucw.cache.Get(hashKey); ok {
225233
panic(errors.AssertionFailedf("child %v already exists", metric.labelValues()))
226234
}
227-
ucw.cache.Add(key, metric)
235+
ucw.cache.Add(hashKey, metric)
228236
}
229237

230238
func (ucw *UnorderedCacheWrapper) Del(metric ChildMetric) {
231-
cacheKey := metricKey(metric.labelValues()...)
232-
if _, ok := ucw.Get(cacheKey); ok {
233-
ucw.cache.Del(cacheKey)
239+
hashKey := metricKey(metric.labelValues()...)
240+
if _, ok := ucw.cache.Get(hashKey); ok {
241+
ucw.cache.Del(hashKey)
234242
}
235243
}
236244

pkg/util/metric/aggmetric/agg_metric_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,38 @@ func TestAggMetricClear(t *testing.T) {
318318
})
319319
}
320320

321+
func TestMetricKey(t *testing.T) {
322+
defer leaktest.AfterTest(t)()
323+
324+
defer leaktest.AfterTest(t)()
325+
326+
for _, tc := range []struct {
327+
name string
328+
labelValues []string
329+
expectedHashValue uint64
330+
}{
331+
{
332+
name: "empty label values",
333+
labelValues: []string{},
334+
expectedHashValue: 0xcbf29ce484222325,
335+
},
336+
{
337+
name: "single label value",
338+
labelValues: []string{"test_db"},
339+
expectedHashValue: 0x7b629443ea81c091,
340+
},
341+
{
342+
name: "multiple label values",
343+
labelValues: []string{"test_db", "test_app", "test_tenant"},
344+
expectedHashValue: 0xa1aaab8437836050,
345+
},
346+
} {
347+
t.Run(tc.name, func(t *testing.T) {
348+
require.Equal(t, tc.expectedHashValue, metricKey(tc.labelValues...))
349+
})
350+
}
351+
}
352+
321353
func WritePrometheusMetricsFunc(r *metric.Registry) func(t *testing.T) string {
322354
writePrometheusMetrics := func(t *testing.T) string {
323355
var in bytes.Buffer

0 commit comments

Comments
 (0)