1818package encode
1919
2020import (
21- "strings"
2221 "time"
2322
2423 "github.com/netobserv/flowlogs-pipeline/pkg/api"
@@ -53,7 +52,7 @@ type MetricsCommonStruct struct {
5352type MetricsCommonInterface interface {
5453 GetCacheEntry (entryLabels map [string ]string , m interface {}) interface {}
5554 ProcessCounter (m interface {}, labels map [string ]string , value float64 ) error
56- ProcessGauge (m interface {}, labels map [string ]string , value float64 , key string ) error
55+ ProcessGauge (m interface {}, name string , labels map [string ]string , value float64 , lvs [] string ) error
5756 ProcessHist (m interface {}, labels map [string ]string , value float64 ) error
5857 ProcessAggHist (m interface {}, labels map [string ]string , value []float64 ) error
5958}
@@ -132,7 +131,7 @@ func (m *MetricsCommonStruct) MetricCommonEncode(mci MetricsCommonInterface, met
132131 continue
133132 }
134133 for _ , labels := range labelSets {
135- err := mci .ProcessGauge (mInfo .genericMetric , labels .lMap , value , labels .key )
134+ err := mci .ProcessGauge (mInfo .genericMetric , mInfo . info . Name , labels .lMap , value , labels .values )
136135 if err != nil {
137136 log .Errorf ("labels registering error on %s: %v" , mInfo .info .Name , err )
138137 m .errorsCounter .WithLabelValues ("LabelsRegisteringError" , mInfo .info .Name , "" ).Inc ()
@@ -198,20 +197,17 @@ func (m *MetricsCommonStruct) prepareMetric(mci MetricsCommonInterface, flow con
198197 }
199198
200199 labelSets := extractLabels (flow , flatParts , info )
201- lkms := make ([]labelsKeyAndMap , 0 , len (labelSets ))
202200 for _ , ls := range labelSets {
203201 // Update entry for expiry mechanism (the entry itself is its own cleanup function)
204- lkm := ls .toKeyAndMap (info )
205- lkms = append (lkms , lkm )
206- ok := m .mCache .UpdateCacheEntry (lkm .key , func () interface {} {
207- return mci .GetCacheEntry (lkm .lMap , mv )
202+ ok := m .mCache .UpdateCacheEntry (ls .values , func () interface {} {
203+ return mci .GetCacheEntry (ls .lMap , mv )
208204 })
209205 if ! ok {
210206 m .metricsDropped .Inc ()
211207 return nil , 0
212208 }
213209 }
214- return lkms , floatVal
210+ return labelSets , floatVal
215211}
216212
217213func (m * MetricsCommonStruct ) prepareAggHisto (mci MetricsCommonInterface , flow config.GenericMap , info * metrics.Preprocessed , mc interface {}) ([]labelsKeyAndMap , []float64 ) {
@@ -232,20 +228,17 @@ func (m *MetricsCommonStruct) prepareAggHisto(mci MetricsCommonInterface, flow c
232228 }
233229
234230 labelSets := extractLabels (flow , flatParts , info )
235- lkms := make ([]labelsKeyAndMap , 0 , len (labelSets ))
236231 for _ , ls := range labelSets {
237232 // Update entry for expiry mechanism (the entry itself is its own cleanup function)
238- lkm := ls .toKeyAndMap (info )
239- lkms = append (lkms , lkm )
240- ok := m .mCache .UpdateCacheEntry (lkm .key , func () interface {} {
241- return mci .GetCacheEntry (lkm .lMap , mc )
233+ ok := m .mCache .UpdateCacheEntry (ls .values , func () interface {} {
234+ return mci .GetCacheEntry (ls .lMap , mc )
242235 })
243236 if ! ok {
244237 m .metricsDropped .Inc ()
245238 return nil , nil
246239 }
247240 }
248- return lkms , values
241+ return labelSets , values
249242}
250243
251244func (m * MetricsCommonStruct ) extractGenericValue (flow config.GenericMap , info * metrics.Preprocessed ) interface {} {
@@ -261,59 +254,44 @@ func (m *MetricsCommonStruct) extractGenericValue(flow config.GenericMap, info *
261254 return val
262255}
263256
264- type label struct {
265- key string
266- value string
267- }
268-
269- type labelSet []label
270-
271257type labelsKeyAndMap struct {
272- key string
273- lMap map [string ]string
274- }
275-
276- func (l labelSet ) toKeyAndMap (info * metrics.Preprocessed ) labelsKeyAndMap {
277- key := strings.Builder {}
278- key .Grow (256 ) // pre-allocate a decent buffer
279- key .WriteString (info .Name )
280- key .WriteRune ('|' )
281- m := make (map [string ]string , len (l ))
282- for _ , kv := range l {
283- key .WriteString (kv .value )
284- key .WriteRune ('|' )
285- m [kv .key ] = kv .value
286- }
287- return labelsKeyAndMap {key : key .String (), lMap : m }
258+ values []string
259+ lMap map [string ]string
288260}
289261
290262// extractLabels takes the flow and a single metric definition as input.
291263// It returns the flat labels maps (label names and values).
292264// Most of the time it will return a single map; it may return several of them when the parsed flow fields are lists (e.g. "interfaces").
293- func extractLabels (flow config.GenericMap , flatParts []config.GenericMap , info * metrics.Preprocessed ) []labelSet {
294- common := newLabelSet ( flow , info .MappedLabels )
265+ func extractLabels (flow config.GenericMap , flatParts []config.GenericMap , info * metrics.Preprocessed ) []labelsKeyAndMap {
266+ common := newLabelKeyAndMap ( info . Name , flow , info .MappedLabels )
295267 if len (flatParts ) == 0 {
296- return []labelSet {common }
268+ return []labelsKeyAndMap {common }
297269 }
298- var all [] labelSet
270+ all := make ([] labelsKeyAndMap , 0 , len ( flatParts ))
299271 for _ , fp := range flatParts {
300- ls := newLabelSet (fp , info .FlattenedLabels )
301- ls = append (ls , common ... )
272+ ls := newLabelKeyAndMap (info .Name , fp , info .FlattenedLabels )
273+ ls .values = append (ls .values , common .values ... )
274+ for k , v := range common .lMap {
275+ ls .lMap [k ] = v
276+ }
302277 all = append (all , ls )
303278 }
304279 return all
305280}
306281
307- func newLabelSet (part config.GenericMap , labels []metrics.MappedLabel ) labelSet {
308- ls := make (labelSet , 0 , len (labels ))
282+ func newLabelKeyAndMap (name string , part config.GenericMap , labels []metrics.MappedLabel ) labelsKeyAndMap {
283+ values := make ([]string , 0 , len (labels )+ 1 )
284+ values = append (values , name )
285+ m := make (map [string ]string , len (labels ))
309286 for _ , t := range labels {
310- label := label { key : t . Target , value : "" }
287+ value := ""
311288 if v , ok := part [t .Source ]; ok {
312- label . value = utils .ConvertToString (v )
289+ value = utils .ConvertToString (v )
313290 }
314- ls = append (ls , label )
291+ values = append (values , value )
292+ m [t .Target ] = value
315293 }
316- return ls
294+ return labelsKeyAndMap { values : values , lMap : m }
317295}
318296
319297func (m * MetricsCommonStruct ) cleanupExpiredEntriesLoop (callback putils.CacheCallback ) {
0 commit comments