99package aggmetric
1010
1111import (
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.
2326type Builder struct {
2427 labels []string
@@ -185,8 +188,13 @@ type labelValuesSlice []string
185188
186189func (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
192200type ChildrenStorage interface {
@@ -210,27 +218,27 @@ func (ucw *UnorderedCacheWrapper) GetChildMetric(e interface{}) ChildMetric {
210218}
211219
212220func (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
221229func (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
230238func (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
0 commit comments