@@ -34,9 +34,9 @@ type Metric interface {
3434 Collector () prometheus.Collector
3535
3636 // Returns the match if the line matched, and nil if the line didn't match.
37- ProcessMatch (line string ) (* Match , error )
37+ ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error )
3838 // Returns the match if the delete pattern matched, nil otherwise.
39- ProcessDeleteMatch (line string ) (* Match , error )
39+ ProcessDeleteMatch (line string , additionalFields map [ string ] string ) (* Match , error )
4040 // Remove old metrics
4141 ProcessRetention () error
4242}
@@ -171,7 +171,7 @@ func (m *observeMetric) processMatch(line string, cb func(value float64)) (*Matc
171171 }
172172 defer searchResult .Free ()
173173 if searchResult .IsMatch () {
174- floatVal , err := floatValue (m .Name (), searchResult , m .valueTemplate )
174+ floatVal , err := floatValue (m .Name (), searchResult , m .valueTemplate , nil )
175175 if err != nil {
176176 return nil , err
177177 }
@@ -184,19 +184,19 @@ func (m *observeMetric) processMatch(line string, cb func(value float64)) (*Matc
184184 }
185185}
186186
187- func (m * metricWithLabels ) processMatch (line string , cb func (labels map [string ]string )) (* Match , error ) {
187+ func (m * metricWithLabels ) processMatch (line string , additionalFields map [ string ] string , callback func (labels map [string ]string )) (* Match , error ) {
188188 searchResult , err := m .regex .Search (line )
189189 if err != nil {
190190 return nil , fmt .Errorf ("error while processing metric %v: %v" , m .Name (), err .Error ())
191191 }
192192 defer searchResult .Free ()
193193 if searchResult .IsMatch () {
194- labels , err := labelValues (m .Name (), searchResult , m .labelTemplates )
194+ labels , err := labelValues (m .Name (), searchResult , m .labelTemplates , additionalFields )
195195 if err != nil {
196196 return nil , err
197197 }
198198 m .labelValueTracker .Observe (labels )
199- cb (labels )
199+ callback (labels )
200200 return & Match {
201201 Value : 1.0 ,
202202 Labels : labels ,
@@ -206,23 +206,23 @@ func (m *metricWithLabels) processMatch(line string, cb func(labels map[string]s
206206 }
207207}
208208
209- func (m * observeMetricWithLabels ) processMatch (line string , cb func (value float64 , labels map [string ]string )) (* Match , error ) {
209+ func (m * observeMetricWithLabels ) processMatch (line string , additionalFields map [ string ] string , callback func (value float64 , labels map [string ]string )) (* Match , error ) {
210210 searchResult , err := m .regex .Search (line )
211211 if err != nil {
212212 return nil , fmt .Errorf ("error processing metric %v: %v" , m .Name (), err .Error ())
213213 }
214214 defer searchResult .Free ()
215215 if searchResult .IsMatch () {
216- floatVal , err := floatValue (m .Name (), searchResult , m .valueTemplate )
216+ floatVal , err := floatValue (m .Name (), searchResult , m .valueTemplate , additionalFields )
217217 if err != nil {
218218 return nil , err
219219 }
220- labels , err := labelValues (m .Name (), searchResult , m .labelTemplates )
220+ labels , err := labelValues (m .Name (), searchResult , m .labelTemplates , additionalFields )
221221 if err != nil {
222222 return nil , err
223223 }
224224 m .labelValueTracker .Observe (labels )
225- cb (floatVal , labels )
225+ callback (floatVal , labels )
226226 return & Match {
227227 Value : floatVal ,
228228 Labels : labels ,
@@ -232,7 +232,7 @@ func (m *observeMetricWithLabels) processMatch(line string, cb func(value float6
232232 }
233233}
234234
235- func (m * metric ) ProcessDeleteMatch (line string ) (* Match , error ) {
235+ func (m * metric ) ProcessDeleteMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
236236 if m .deleteRegex == nil {
237237 return nil , nil
238238 }
@@ -246,7 +246,7 @@ func (m *metric) ProcessRetention() error {
246246 return fmt .Errorf ("error processing metric %v: retention is currently only supported for metrics with labels." , m .Name ())
247247}
248248
249- func (m * metricWithLabels ) processDeleteMatch (line string , vec deleterMetric ) (* Match , error ) {
249+ func (m * metricWithLabels ) processDeleteMatch (line string , vec deleterMetric , additionalFields map [ string ] string ) (* Match , error ) {
250250 if m .deleteRegex == nil {
251251 return nil , nil
252252 }
@@ -256,7 +256,7 @@ func (m *metricWithLabels) processDeleteMatch(line string, vec deleterMetric) (*
256256 }
257257 defer searchResult .Free ()
258258 if searchResult .IsMatch () {
259- deleteLabels , err := labelValues (m .Name (), searchResult , m .deleteLabelTemplates )
259+ deleteLabels , err := labelValues (m .Name (), searchResult , m .deleteLabelTemplates , additionalFields )
260260 if err != nil {
261261 return nil , err
262262 }
@@ -284,27 +284,27 @@ func (m *metricWithLabels) processRetention(vec deleterMetric) error {
284284 return nil
285285}
286286
287- func (m * counterMetric ) ProcessMatch (line string ) (* Match , error ) {
287+ func (m * counterMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
288288 return m .processMatch (line , func () {
289289 m .counter .Inc ()
290290 })
291291}
292292
293- func (m * counterVecMetric ) ProcessMatch (line string ) (* Match , error ) {
294- return m .processMatch (line , func (labels map [string ]string ) {
293+ func (m * counterVecMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
294+ return m .processMatch (line , additionalFields , func (labels map [string ]string ) {
295295 m .counterVec .With (labels ).Inc ()
296296 })
297297}
298298
299- func (m * counterVecMetric ) ProcessDeleteMatch (line string ) (* Match , error ) {
300- return m .processDeleteMatch (line , m .counterVec )
299+ func (m * counterVecMetric ) ProcessDeleteMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
300+ return m .processDeleteMatch (line , m .counterVec , additionalFields )
301301}
302302
303303func (m * counterVecMetric ) ProcessRetention () error {
304304 return m .processRetention (m .counterVec )
305305}
306306
307- func (m * gaugeMetric ) ProcessMatch (line string ) (* Match , error ) {
307+ func (m * gaugeMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
308308 return m .processMatch (line , func (value float64 ) {
309309 if m .cumulative {
310310 m .gauge .Add (value )
@@ -314,8 +314,8 @@ func (m *gaugeMetric) ProcessMatch(line string) (*Match, error) {
314314 })
315315}
316316
317- func (m * gaugeVecMetric ) ProcessMatch (line string ) (* Match , error ) {
318- return m .processMatch (line , func (value float64 , labels map [string ]string ) {
317+ func (m * gaugeVecMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
318+ return m .processMatch (line , additionalFields , func (value float64 , labels map [string ]string ) {
319319 if m .cumulative {
320320 m .gaugeVec .With (labels ).Add (value )
321321 } else {
@@ -324,48 +324,48 @@ func (m *gaugeVecMetric) ProcessMatch(line string) (*Match, error) {
324324 })
325325}
326326
327- func (m * gaugeVecMetric ) ProcessDeleteMatch (line string ) (* Match , error ) {
328- return m .processDeleteMatch (line , m .gaugeVec )
327+ func (m * gaugeVecMetric ) ProcessDeleteMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
328+ return m .processDeleteMatch (line , m .gaugeVec , additionalFields )
329329}
330330
331331func (m * gaugeVecMetric ) ProcessRetention () error {
332332 return m .processRetention (m .gaugeVec )
333333}
334334
335- func (m * histogramMetric ) ProcessMatch (line string ) (* Match , error ) {
335+ func (m * histogramMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
336336 return m .processMatch (line , func (value float64 ) {
337337 m .histogram .Observe (value )
338338 })
339339}
340340
341- func (m * histogramVecMetric ) ProcessMatch (line string ) (* Match , error ) {
342- return m .processMatch (line , func (value float64 , labels map [string ]string ) {
341+ func (m * histogramVecMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
342+ return m .processMatch (line , additionalFields , func (value float64 , labels map [string ]string ) {
343343 m .histogramVec .With (labels ).Observe (value )
344344 })
345345}
346346
347- func (m * histogramVecMetric ) ProcessDeleteMatch (line string ) (* Match , error ) {
348- return m .processDeleteMatch (line , m .histogramVec )
347+ func (m * histogramVecMetric ) ProcessDeleteMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
348+ return m .processDeleteMatch (line , m .histogramVec , additionalFields )
349349}
350350
351351func (m * histogramVecMetric ) ProcessRetention () error {
352352 return m .processRetention (m .histogramVec )
353353}
354354
355- func (m * summaryMetric ) ProcessMatch (line string ) (* Match , error ) {
355+ func (m * summaryMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
356356 return m .processMatch (line , func (value float64 ) {
357357 m .summary .Observe (value )
358358 })
359359}
360360
361- func (m * summaryVecMetric ) ProcessMatch (line string ) (* Match , error ) {
362- return m .processMatch (line , func (value float64 , labels map [string ]string ) {
361+ func (m * summaryVecMetric ) ProcessMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
362+ return m .processMatch (line , additionalFields , func (value float64 , labels map [string ]string ) {
363363 m .summaryVec .With (labels ).Observe (value )
364364 })
365365}
366366
367- func (m * summaryVecMetric ) ProcessDeleteMatch (line string ) (* Match , error ) {
368- return m .processDeleteMatch (line , m .summaryVec )
367+ func (m * summaryVecMetric ) ProcessDeleteMatch (line string , additionalFields map [ string ] string ) (* Match , error ) {
368+ return m .processDeleteMatch (line , m .summaryVec , additionalFields )
369369}
370370
371371func (m * summaryVecMetric ) ProcessRetention () error {
@@ -484,10 +484,10 @@ func NewSummaryMetric(cfg *configuration.MetricConfig, regex *oniguruma.Regex, d
484484 }
485485}
486486
487- func labelValues (metricName string , searchResult * oniguruma.SearchResult , templates []template.Template ) (map [string ]string , error ) {
487+ func labelValues (metricName string , searchResult * oniguruma.SearchResult , templates []template.Template , additionalFields map [ string ] string ) (map [string ]string , error ) {
488488 result := make (map [string ]string , len (templates ))
489489 for _ , t := range templates {
490- value , err := evalTemplate (searchResult , t )
490+ value , err := evalTemplate (searchResult , t , additionalFields )
491491 if err != nil {
492492 return nil , fmt .Errorf ("error processing metric %v: %v" , metricName , err .Error ())
493493 }
@@ -496,28 +496,36 @@ func labelValues(metricName string, searchResult *oniguruma.SearchResult, templa
496496 return result , nil
497497}
498498
499- func floatValue (metricName string , searchResult * oniguruma.SearchResult , valueTemplate template.Template ) (float64 , error ) {
500- stringVal , err := evalTemplate (searchResult , valueTemplate )
499+ func floatValue (metricName string , searchResult * oniguruma.SearchResult , valueTemplate template.Template , additionalFields map [ string ] string ) (float64 , error ) {
500+ stringVal , err := evalTemplate (searchResult , valueTemplate , additionalFields )
501501 if err != nil {
502502 return 0 , fmt .Errorf ("error processing metric %v: %v" , metricName , err .Error ())
503503 }
504504 floatVal , err := strconv .ParseFloat (stringVal , 64 )
505505 if err != nil {
506- return 0 , fmt .Errorf ("error processing metric %v: value matches '%v', which is not a valid number. " , metricName , stringVal )
506+ return 0 , fmt .Errorf ("error processing metric %v: value matches '%v', which is not a valid number" , metricName , stringVal )
507507 }
508508 return floatVal , nil
509509}
510510
511- func evalTemplate (searchResult * oniguruma.SearchResult , t template.Template ) (string , error ) {
512- grokValues := make (map [string ]string , len (t .ReferencedGrokFields ()))
513- for _ , field := range t .ReferencedGrokFields () {
514- value , err := searchResult .GetCaptureGroupByName (field )
515- if err != nil {
516- return "" , err
511+ func evalTemplate (searchResult * oniguruma.SearchResult , t template.Template , additionalFields map [string ]string ) (string , error ) {
512+ var (
513+ values = make (map [string ]string , len (t .ReferencedGrokFields ()))
514+ value string
515+ ok bool
516+ err error
517+ field string
518+ )
519+ for _ , field = range t .ReferencedGrokFields () {
520+ if value , ok = additionalFields [field ]; ! ok {
521+ value , err = searchResult .GetCaptureGroupByName (field )
522+ if err != nil {
523+ return "" , err
524+ }
517525 }
518- grokValues [field ] = value
526+ values [field ] = value
519527 }
520- return t .Execute (grokValues )
528+ return t .Execute (values )
521529}
522530
523531func prometheusLabels (templates []template.Template ) []string {
0 commit comments