@@ -20,9 +20,10 @@ import (
20
20
"net/http"
21
21
"strconv"
22
22
23
+ "github.com/go-kit/kit/log"
24
+ "github.com/go-kit/kit/log/level"
23
25
"github.com/kawamuray/jsonpath" // Originally: "github.com/NickSardo/jsonpath"
24
26
"github.com/prometheus/client_golang/prometheus"
25
- log "github.com/sirupsen/logrus"
26
27
)
27
28
28
29
type JsonGaugeCollector struct {
@@ -32,42 +33,42 @@ type JsonGaugeCollector struct {
32
33
LabelsJsonPath map [string ]string
33
34
}
34
35
35
- func Scrape (collectors []JsonGaugeCollector , json []byte ) {
36
+ func Scrape (logger log. Logger , collectors []JsonGaugeCollector , json []byte ) {
36
37
37
38
for _ , collector := range collectors {
38
39
if collector .ValueJsonPath == "" { // ScrapeType is 'value'
39
40
40
41
// Since this is a 'value' type metric, there should be exactly one element in results
41
42
// If there are more, just return the first one
42
43
// TODO: Better handling/logging for this scenario
43
- floatValue , err := extractValue (json , collector .KeyJsonPath )
44
+ floatValue , err := extractValue (logger , json , collector .KeyJsonPath )
44
45
if err != nil {
45
- log .Error (err )
46
+ level .Error (logger ). Log ( "msg" , "Failed to extract float value for metric" , "path" , collector . KeyJsonPath , "err" , err )
46
47
continue
47
48
}
48
49
49
- collector .With (extractLabels (json , collector .LabelsJsonPath )).Set (floatValue )
50
+ collector .With (extractLabels (logger , json , collector .LabelsJsonPath )).Set (floatValue )
50
51
} else { // ScrapeType is 'object'
51
52
path , err := compilePath (collector .KeyJsonPath )
52
53
if err != nil {
53
- log . Errorf ( " Failed to compile path: '%s', ERROR: '%s'" , collector .KeyJsonPath , err )
54
+ level . Error ( logger ). Log ( "msg" , " Failed to compile path" , "path" , collector .KeyJsonPath , "err" , err )
54
55
continue
55
56
}
56
57
57
58
eval , err := jsonpath .EvalPathsInBytes (json , []* jsonpath.Path {path })
58
59
if err != nil {
59
- log . Errorf ( " Failed to create evaluator for JSON Path: %s, ERROR: '%s'" , collector .KeyJsonPath , err )
60
+ level . Error ( logger ). Log ( "msg" , " Failed to create evaluator for json path" , "path" , collector .KeyJsonPath , "err" , err )
60
61
continue
61
62
}
62
63
for {
63
64
if result , ok := eval .Next (); ok {
64
- floatValue , err := extractValue (result .Value , collector .ValueJsonPath )
65
+ floatValue , err := extractValue (logger , result .Value , collector .ValueJsonPath )
65
66
if err != nil {
66
- log .Error (err )
67
+ level .Error (logger ). Log ( "msg" , "Failed to extract value" , "path" , collector . ValueJsonPath , "err" , err )
67
68
continue
68
69
}
69
70
70
- collector .With (extractLabels (result .Value , collector .LabelsJsonPath )).Set (floatValue )
71
+ collector .With (extractLabels (logger , result .Value , collector .LabelsJsonPath )).Set (floatValue )
71
72
} else {
72
73
break
73
74
}
@@ -91,7 +92,7 @@ func compilePath(path string) (*jsonpath.Path, error) {
91
92
}
92
93
93
94
// Returns the first matching float value at the given json path
94
- func extractValue (json []byte , path string ) (float64 , error ) {
95
+ func extractValue (logger log. Logger , json []byte , path string ) (float64 , error ) {
95
96
var floatValue = - 1.0
96
97
var result * jsonpath.Result
97
98
var err error
@@ -117,15 +118,15 @@ func extractValue(json []byte, path string) (float64, error) {
117
118
if eval .Error != nil {
118
119
return floatValue , fmt .Errorf ("Failed to evaluate json. ERROR: '%s', PATH: '%s', JSON: '%s'" , eval .Error , path , string (json ))
119
120
} else {
120
- log . Debugf ( "Could not find path. PATH: '%s', JSON: '%s'" , path , string (json ))
121
+ level . Debug ( logger ). Log ( "msg" , "Path not found" , "path" , path , "json" , string (json ))
121
122
return floatValue , fmt .Errorf ("Could not find path. PATH: '%s'" , path )
122
123
}
123
124
}
124
125
125
126
return SanitizeValue (result )
126
127
}
127
128
128
- func extractLabels (json []byte , l map [string ]string ) map [string ]string {
129
+ func extractLabels (logger log. Logger , json []byte , l map [string ]string ) map [string ]string {
129
130
labels := make (map [string ]string )
130
131
for label , path := range l {
131
132
@@ -138,25 +139,25 @@ func extractLabels(json []byte, l map[string]string) map[string]string {
138
139
// Dynamic value
139
140
p , err := compilePath (path )
140
141
if err != nil {
141
- log . Errorf ( " Failed to compile path for label: '%s', PATH: '%s', ERROR: '%s'" , label , path , err )
142
+ level . Error ( logger ). Log ( "msg" , " Failed to compile path for label" , "path" , path , "label" , label , "err" , err )
142
143
labels [label ] = ""
143
144
continue
144
145
}
145
146
146
147
eval , err := jsonpath .EvalPathsInBytes (json , []* jsonpath.Path {p })
147
148
if err != nil {
148
- log . Errorf ( " Failed to create evaluator for JSON Path: %s, ERROR: '%s'" , path , err )
149
+ level . Error ( logger ). Log ( "msg" , " Failed to create evaluator for json" , "path" , path , "err" , err )
149
150
labels [label ] = ""
150
151
continue
151
152
}
152
153
153
154
result , ok := eval .Next ()
154
155
if result == nil || ! ok {
155
156
if eval .Error != nil {
156
- log . Errorf ( " Failed to evaluate json for label: '%s', ERROR: '%s', PATH: '%s', JSON: '%s'" , label , eval .Error , path , string ( json ) )
157
+ level . Error ( logger ). Log ( "msg" , " Failed to evaluate" , " label" , label , "json" , string ( json ), "err" , eval .Error )
157
158
} else {
158
- log . Debugf ( "Could not find path in json for label: '%s', PATH: '%s', JSON: '%s'" , label , path , string ( json ) )
159
- log . Warnf ( "Could not find path in json for label: '%s', PATH: '%s'" , label , path )
159
+ level . Warn ( logger ). Log ( "msg" , "Label path not found in json" , "path" , path , " label" , label )
160
+ level . Debug ( logger ). Log ( "msg" , "Label path not found in json" , "path" , path , "label" , label , "json" , string ( json ) )
160
161
}
161
162
continue
162
163
}
@@ -171,12 +172,12 @@ func extractLabels(json []byte, l map[string]string) map[string]string {
171
172
return labels
172
173
}
173
174
174
- func FetchJson (ctx context.Context , endpoint string , headers map [string ]string ) ([]byte , error ) {
175
+ func FetchJson (ctx context.Context , logger log. Logger , endpoint string , headers map [string ]string ) ([]byte , error ) {
175
176
client := & http.Client {}
176
177
req , err := http .NewRequest ("GET" , endpoint , nil )
177
178
req = req .WithContext (ctx )
178
179
if err != nil {
179
- log . Errorf ( "Error creating request. ERROR: '%s' " , err )
180
+ level . Error ( logger ). Log ( "msg" , "Failed to create request" , "err " , err )
180
181
return nil , err
181
182
}
182
183
0 commit comments