@@ -16,6 +16,7 @@ package exporter
1616import (
1717 "bytes"
1818 "encoding/json"
19+ "strings"
1920 "time"
2021
2122 "github.com/go-kit/log"
@@ -38,6 +39,7 @@ type JSONMetric struct {
3839 ValueJSONPath string
3940 LabelsJSONPaths []string
4041 ValueType prometheus.ValueType
42+ ValueConverter config.ValueConverterType
4143 EpochTimestampJSONPath string
4244}
4345
@@ -86,11 +88,14 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
8688 continue
8789 }
8890 value , err := extractValue (mc .Logger , jdata , m .ValueJSONPath , false )
91+
8992 if err != nil {
9093 level .Error (mc .Logger ).Log ("msg" , "Failed to extract value for metric" , "path" , m .ValueJSONPath , "err" , err , "metric" , m .Desc )
9194 continue
9295 }
9396
97+ value = convertValueIfNeeded (m , value )
98+
9499 if floatValue , err := SanitizeValue (value ); err == nil {
95100 metric := prometheus .MustNewConstMetric (
96101 m .Desc ,
@@ -161,6 +166,19 @@ func extractLabels(logger log.Logger, data []byte, paths []string) []string {
161166 return labels
162167}
163168
169+ // Returns the conversion of the dynamic value- if it exists in the ValueConverter configuration
170+ func convertValueIfNeeded (m JSONMetric , value string ) string {
171+ if m .ValueConverter != nil {
172+ if valueMappings , hasPathKey := m .ValueConverter [m .ValueJSONPath ]; hasPathKey {
173+ value = strings .ToLower (value )
174+
175+ if _ , hasValueKey := valueMappings [value ]; hasValueKey {
176+ value = valueMappings [value ]
177+ }
178+ }
179+ }
180+ return value
181+ }
164182func timestampMetric (logger log.Logger , m JSONMetric , data []byte , pm prometheus.Metric ) prometheus.Metric {
165183 if m .EpochTimestampJSONPath == "" {
166184 return pm
0 commit comments