@@ -42,7 +42,7 @@ type MetricsCommonStruct struct {
4242 histos map [string ]mInfoStruct
4343 aggHistos map [string ]mInfoStruct
4444 mCache * putils.TimedCache
45- mChacheLenMetric prometheus.Gauge
45+ mCacheLenMetric prometheus.Gauge
4646 metricsProcessed prometheus.Counter
4747 metricsDropped prometheus.Counter
4848 errorsCounter * prometheus.CounterVec
@@ -51,7 +51,7 @@ type MetricsCommonStruct struct {
5151}
5252
5353type MetricsCommonInterface interface {
54- GetChacheEntry (entryLabels map [string ]string , m interface {}) interface {}
54+ GetCacheEntry (entryLabels map [string ]string , m interface {}) interface {}
5555 ProcessCounter (m interface {}, labels map [string ]string , value float64 ) error
5656 ProcessGauge (m interface {}, labels map [string ]string , value float64 , key string ) error
5757 ProcessHist (m interface {}, labels map [string ]string , value float64 ) error
@@ -198,13 +198,14 @@ func (m *MetricsCommonStruct) prepareMetric(mci MetricsCommonInterface, flow con
198198 }
199199
200200 labelSets := extractLabels (flow , flatParts , info )
201- var lkms []labelsKeyAndMap
201+ lkms := make ( []labelsKeyAndMap , 0 , len ( labelSets ))
202202 for _ , ls := range labelSets {
203203 // Update entry for expiry mechanism (the entry itself is its own cleanup function)
204204 lkm := ls .toKeyAndMap (info )
205205 lkms = append (lkms , lkm )
206- cacheEntry := mci .GetChacheEntry (lkm .lMap , mv )
207- ok := m .mCache .UpdateCacheEntry (lkm .key , cacheEntry )
206+ ok := m .mCache .UpdateCacheEntry (lkm .key , func () interface {} {
207+ return mci .GetCacheEntry (lkm .lMap , mv )
208+ })
208209 if ! ok {
209210 m .metricsDropped .Inc ()
210211 return nil , 0
@@ -231,13 +232,14 @@ func (m *MetricsCommonStruct) prepareAggHisto(mci MetricsCommonInterface, flow c
231232 }
232233
233234 labelSets := extractLabels (flow , flatParts , info )
234- var lkms []labelsKeyAndMap
235+ lkms := make ( []labelsKeyAndMap , 0 , len ( labelSets ))
235236 for _ , ls := range labelSets {
236237 // Update entry for expiry mechanism (the entry itself is its own cleanup function)
237238 lkm := ls .toKeyAndMap (info )
238239 lkms = append (lkms , lkm )
239- cacheEntry := mci .GetChacheEntry (lkm .lMap , mc )
240- ok := m .mCache .UpdateCacheEntry (lkm .key , cacheEntry )
240+ ok := m .mCache .UpdateCacheEntry (lkm .key , func () interface {} {
241+ return mci .GetCacheEntry (lkm .lMap , mc )
242+ })
241243 if ! ok {
242244 m .metricsDropped .Inc ()
243245 return nil , nil
@@ -273,9 +275,10 @@ type labelsKeyAndMap struct {
273275
274276func (l labelSet ) toKeyAndMap (info * metrics.Preprocessed ) labelsKeyAndMap {
275277 key := strings.Builder {}
278+ key .Grow (256 ) // pre-allocate a decent buffer
276279 key .WriteString (info .Name )
277280 key .WriteRune ('|' )
278- m := map [string ]string {}
281+ m := make ( map [string ]string , len ( l ))
279282 for _ , kv := range l {
280283 key .WriteString (kv .value )
281284 key .WriteRune ('|' )
@@ -302,7 +305,7 @@ func extractLabels(flow config.GenericMap, flatParts []config.GenericMap, info *
302305}
303306
304307func newLabelSet (part config.GenericMap , labels []metrics.MappedLabel ) labelSet {
305- var ls labelSet
308+ ls := make ( labelSet , 0 , len ( labels ))
306309 for _ , t := range labels {
307310 label := label {key : t .Target , value : "" }
308311 if v , ok := part [t .Source ]; ok {
@@ -337,7 +340,7 @@ func NewMetricsCommonStruct(opMetrics *operational.Metrics, maxCacheEntries int,
337340 mChacheLenMetric := opMetrics .NewGauge (& mChacheLen , name )
338341 m := & MetricsCommonStruct {
339342 mCache : putils .NewTimedCache (maxCacheEntries , mChacheLenMetric ),
340- mChacheLenMetric : mChacheLenMetric ,
343+ mCacheLenMetric : mChacheLenMetric ,
341344 metricsProcessed : opMetrics .NewCounter (& metricsProcessed , name ),
342345 metricsDropped : opMetrics .NewCounter (& metricsDropped , name ),
343346 errorsCounter : opMetrics .NewCounterVec (& encodePromErrors ),
0 commit comments