forked from prometheus-community/json_exporter
/
scraper.go
219 lines (195 loc) · 6 KB
/
scraper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package jsonexporter
import (
"fmt"
"math"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/kawamuray/jsonpath" // Originally: "github.com/NickSardo/jsonpath"
"github.com/kawamuray/prometheus-exporter-harness/harness"
"github.com/prometheus/client_golang/prometheus"
)
type JsonScraper interface {
Scrape(data []byte, reg *harness.MetricRegistry) error
}
type ValueScraper struct {
*Config
valueJsonPath *jsonpath.Path
}
func NewValueScraper(config *Config) (JsonScraper, error) {
valuepath, err := compilePath(config.Path)
if err != nil {
return nil, fmt.Errorf("failed to parse path;path:<%s>,err:<%s>", config.Path, err)
}
scraper := &ValueScraper{
Config: config,
valueJsonPath: valuepath,
}
return scraper, nil
}
func (vs *ValueScraper) parseValue(bytes []byte) (float64, error) {
value, err := strconv.ParseFloat(string(bytes), 64)
if err != nil {
return -1.0, fmt.Errorf("failed to parse value as float;value:<%s>", bytes)
}
return value, nil
}
func (vs *ValueScraper) forTargetValue(data []byte, handle func(*jsonpath.Result)) error {
eval, err := jsonpath.EvalPathsInBytes(data, []*jsonpath.Path{vs.valueJsonPath})
if err != nil {
return fmt.Errorf("failed to eval jsonpath;path:<%s>,json:<%s>", vs.valueJsonPath, data)
}
for {
result, ok := eval.Next()
if !ok {
break
}
handle(result)
}
return nil
}
func (vs *ValueScraper) Scrape(data []byte, reg *harness.MetricRegistry) error {
isFirst := true
return vs.forTargetValue(data, func(result *jsonpath.Result) {
if !isFirst {
log.Infof("ignoring non-first value;path:<%s>", vs.valueJsonPath)
return
}
isFirst = false
var value float64
var err error
switch result.Type {
case jsonpath.JsonNumber:
value, err = vs.parseValue(result.Value)
case jsonpath.JsonString:
// If it is a string, lets pull off the quotes and attempt to parse it as a number
value, err = vs.parseValue(result.Value[1 : len(result.Value)-1])
case jsonpath.JsonNull:
value = math.NaN()
default:
log.Warnf("skipping not numerical result;path:<%s>,value:<%s>",
vs.valueJsonPath, result.Value)
return
}
if err != nil {
// Should never happen.
log.Errorf("could not parse numerical value as float;path:<%s>,value:<%s>",
vs.valueJsonPath, result.Value)
return
}
log.Debugf("metric updated;name:<%s>,labels:<%s>,value:<%.2f>", vs.Name, vs.Labels, value)
reg.Get(vs.Name).(*prometheus.GaugeVec).With(vs.Labels).Set(value)
})
}
type ObjectScraper struct {
*ValueScraper
labelJsonPaths map[string]*jsonpath.Path
valueJsonPaths map[string]*jsonpath.Path
}
func NewObjectScraper(config *Config) (JsonScraper, error) {
valueScraper, err := NewValueScraper(config)
if err != nil {
return nil, err
}
labelPaths, err := compilePaths(config.Labels)
if err != nil {
return nil, err
}
valuePaths, err := compilePaths(config.Values)
if err != nil {
return nil, err
}
scraper := &ObjectScraper{
ValueScraper: valueScraper.(*ValueScraper),
labelJsonPaths: labelPaths,
valueJsonPaths: valuePaths,
}
return scraper, nil
}
func (obsc *ObjectScraper) newLabels() map[string]string {
labels := make(map[string]string)
for name, value := range obsc.Labels {
if _, ok := obsc.labelJsonPaths[name]; !ok {
// Static label value.
labels[name] = value
}
}
return labels
}
func (obsc *ObjectScraper) extractFirstValue(data []byte, path *jsonpath.Path) (*jsonpath.Result, error) {
eval, err := jsonpath.EvalPathsInBytes(data, []*jsonpath.Path{path})
if err != nil {
return nil, fmt.Errorf("failed to eval jsonpath;err:<%s>", err)
}
result, ok := eval.Next()
if !ok {
return nil, fmt.Errorf("no value found for path")
}
return result, nil
}
func (obsc *ObjectScraper) Scrape(data []byte, reg *harness.MetricRegistry) error {
return obsc.forTargetValue(data, func(result *jsonpath.Result) {
if result.Type != jsonpath.JsonObject && result.Type != jsonpath.JsonArray {
log.Warnf("skipping not structual result;path:<%s>,value:<%s>",
obsc.valueJsonPath, result.Value)
return
}
labels := obsc.newLabels()
for name, path := range obsc.labelJsonPaths {
firstResult, err := obsc.extractFirstValue(result.Value, path)
if err != nil {
log.Warnf("could not find value for label path;path:<%s>,json:<%s>,err:<%s>", path, result.Value, err)
continue
}
value := firstResult.Value
if firstResult.Type == jsonpath.JsonString {
// Strip quotes
value = value[1 : len(value)-1]
}
labels[name] = string(value)
}
for name, configValue := range obsc.Values {
var metricValue float64
path := obsc.valueJsonPaths[name]
if path == nil {
// Static value
value, err := obsc.parseValue([]byte(configValue))
if err != nil {
log.Errorf("could not use configured value as float number;name:<%s>,err:<%s>", err)
continue
}
metricValue = value
} else {
// Dynamic value
firstResult, err := obsc.extractFirstValue(result.Value, path)
if err != nil {
log.Warnf("could not find value for value path;path:<%s>,json:<%s>,err:<%s>", path, result.Value, err)
continue
}
var value float64
switch firstResult.Type {
case jsonpath.JsonNumber:
value, err = obsc.parseValue(firstResult.Value)
case jsonpath.JsonString:
// If it is a string, lets pull off the quotes and attempt to parse it as a number
value, err = obsc.parseValue(firstResult.Value[1 : len(firstResult.Value)-1])
case jsonpath.JsonNull:
value = math.NaN()
default:
log.Warnf("skipping not numerical result;path:<%s>,value:<%s>",
obsc.valueJsonPath, result.Value)
continue
}
if err != nil {
// Should never happen.
log.Errorf("could not parse numerical value as float;path:<%s>,value:<%s>",
obsc.valueJsonPath, firstResult.Value)
continue
}
metricValue = value
}
fqn := harness.MakeMetricName(obsc.Name, name)
log.Debugf("metric updated;name:<%s>,labels:<%s>,value:<%.2f>", fqn, labels, metricValue)
reg.Get(fqn).(*prometheus.GaugeVec).With(labels).Set(metricValue)
}
})
}