forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
430 lines (378 loc) · 11.5 KB
/
parser.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package xml
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/antchfx/xmlquery"
"github.com/antchfx/xpath"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
)
type Parser struct {
Configs []Config
DefaultTags map[string]string
Log telegraf.Logger
}
type Config struct {
MetricName string
MetricQuery string `toml:"metric_name"`
Selection string `toml:"metric_selection"`
Timestamp string `toml:"timestamp"`
TimestampFmt string `toml:"timestamp_format"`
Tags map[string]string `toml:"tags"`
Fields map[string]string `toml:"fields"`
FieldsInt map[string]string `toml:"fields_int"`
FieldSelection string `toml:"field_selection"`
FieldNameQuery string `toml:"field_name"`
FieldValueQuery string `toml:"field_value"`
FieldNameExpand bool `toml:"field_name_expansion"`
}
func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
t := time.Now()
// Parse the XML
doc, err := xmlquery.Parse(strings.NewReader(string(buf)))
if err != nil {
return nil, err
}
// Queries
metrics := make([]telegraf.Metric, 0)
for _, config := range p.Configs {
if len(config.Selection) == 0 {
config.Selection = "/"
}
selectedNodes, err := xmlquery.QueryAll(doc, config.Selection)
if err != nil {
return nil, err
}
if len(selectedNodes) < 1 || selectedNodes[0] == nil {
p.debugEmptyQuery("metric selection", doc, config.Selection)
return nil, fmt.Errorf("cannot parse with empty selection node")
}
p.Log.Debugf("Number of selected metric nodes: %d", len(selectedNodes))
for _, selected := range selectedNodes {
m, err := p.parseQuery(t, doc, selected, config)
if err != nil {
return metrics, err
}
metrics = append(metrics, m)
}
}
return metrics, nil
}
func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
t := time.Now()
switch len(p.Configs) {
case 0:
return nil, nil
case 1:
config := p.Configs[0]
doc, err := xmlquery.Parse(strings.NewReader(line))
if err != nil {
return nil, err
}
selected := doc
if len(config.Selection) > 0 {
selectedNodes, err := xmlquery.QueryAll(doc, config.Selection)
if err != nil {
return nil, err
}
if len(selectedNodes) < 1 || selectedNodes[0] == nil {
p.debugEmptyQuery("metric selection", doc, config.Selection)
return nil, fmt.Errorf("cannot parse line with empty selection")
} else if len(selectedNodes) != 1 {
return nil, fmt.Errorf("cannot parse line with multiple selected nodes (%d)", len(selectedNodes))
}
selected = selectedNodes[0]
}
return p.parseQuery(t, doc, selected, config)
}
return nil, fmt.Errorf("cannot parse line with multiple (%d) configurations", len(p.Configs))
}
func (p *Parser) SetDefaultTags(tags map[string]string) {
p.DefaultTags = tags
}
func (p *Parser) parseQuery(starttime time.Time, doc, selected *xmlquery.Node, config Config) (telegraf.Metric, error) {
var timestamp time.Time
var metricname string
// Determine the metric name. If a query was specified, use the result of this query and the default metric name
// otherwise.
metricname = config.MetricName
if len(config.MetricQuery) > 0 {
v, err := executeQuery(doc, selected, config.MetricQuery)
if err != nil {
return nil, fmt.Errorf("failed to query metric name: %v", err)
}
metricname = v.(string)
}
// By default take the time the parser was invoked and override the value
// with the queried timestamp if an expresion was specified.
timestamp = starttime
if len(config.Timestamp) > 0 {
v, err := executeQuery(doc, selected, config.Timestamp)
if err != nil {
return nil, fmt.Errorf("failed to query timestamp: %v", err)
}
switch v := v.(type) {
case string:
// Parse the string with the given format or assume the string to contain
// a unix timestamp in seconds if no format is given.
if len(config.TimestampFmt) < 1 || strings.HasPrefix(config.TimestampFmt, "unix") {
var nanoseconds int64
t, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse unix timestamp: %v", err)
}
switch config.TimestampFmt {
case "unix_ns":
nanoseconds = int64(t)
case "unix_us":
nanoseconds = int64(t * 1e3)
case "unix_ms":
nanoseconds = int64(t * 1e6)
default:
nanoseconds = int64(t * 1e9)
}
timestamp = time.Unix(0, nanoseconds)
} else {
timestamp, err = time.Parse(config.TimestampFmt, v)
if err != nil {
return nil, fmt.Errorf("failed to query timestamp format: %v", err)
}
}
case float64:
// Assume the value to contain a timestamp in seconds and fractions thereof.
timestamp = time.Unix(0, int64(v*1e9))
case nil:
// No timestamp found. Just ignore the time and use "starttime"
default:
return nil, fmt.Errorf("unknown format '%T' for timestamp query '%v'", v, config.Timestamp)
}
}
// Query tags and add default ones
tags := make(map[string]string)
for name, query := range config.Tags {
// Execute the query and cast the returned values into strings
v, err := executeQuery(doc, selected, query)
if err != nil {
return nil, fmt.Errorf("failed to query tag '%s': %v", name, err)
}
switch v := v.(type) {
case string:
tags[name] = v
case bool:
tags[name] = strconv.FormatBool(v)
case float64:
tags[name] = strconv.FormatFloat(v, 'G', -1, 64)
case nil:
continue
default:
return nil, fmt.Errorf("unknown format '%T' for tag '%s'", v, name)
}
}
for name, v := range p.DefaultTags {
tags[name] = v
}
// Query fields
fields := make(map[string]interface{})
for name, query := range config.FieldsInt {
// Execute the query and cast the returned values into integers
v, err := executeQuery(doc, selected, query)
if err != nil {
return nil, fmt.Errorf("failed to query field (int) '%s': %v", name, err)
}
switch v := v.(type) {
case string:
fields[name], err = strconv.ParseInt(v, 10, 54)
if err != nil {
return nil, fmt.Errorf("failed to parse field (int) '%s': %v", name, err)
}
case bool:
fields[name] = int64(0)
if v {
fields[name] = int64(1)
}
case float64:
fields[name] = int64(v)
case nil:
continue
default:
return nil, fmt.Errorf("unknown format '%T' for field (int) '%s'", v, name)
}
}
for name, query := range config.Fields {
// Execute the query and store the result in fields
v, err := executeQuery(doc, selected, query)
if err != nil {
return nil, fmt.Errorf("failed to query field '%s': %v", name, err)
}
fields[name] = v
}
// Handle the field batch definitions if any.
if len(config.FieldSelection) > 0 {
fieldnamequery := "name()"
fieldvaluequery := "."
if len(config.FieldNameQuery) > 0 {
fieldnamequery = config.FieldNameQuery
}
if len(config.FieldValueQuery) > 0 {
fieldvaluequery = config.FieldValueQuery
}
// Query all fields
selectedFieldNodes, err := xmlquery.QueryAll(selected, config.FieldSelection)
if err != nil {
return nil, err
}
p.Log.Debugf("Number of selected field nodes: %d", len(selectedFieldNodes))
if len(selectedFieldNodes) > 0 && selectedFieldNodes[0] != nil {
for _, selectedfield := range selectedFieldNodes {
n, err := executeQuery(doc, selectedfield, fieldnamequery)
if err != nil {
return nil, fmt.Errorf("failed to query field name with query '%s': %v", fieldnamequery, err)
}
name, ok := n.(string)
if !ok {
return nil, fmt.Errorf("failed to query field name with query '%s': result is not a string (%v)", fieldnamequery, n)
}
v, err := executeQuery(doc, selectedfield, fieldvaluequery)
if err != nil {
return nil, fmt.Errorf("failed to query field value for '%s': %v", name, err)
}
path := name
if config.FieldNameExpand {
p := getNodePath(selectedfield, selected, "_")
if len(p) > 0 {
path = p + "_" + name
}
}
// Check if field name already exists and if so, append an index number.
if _, ok := fields[path]; ok {
for i := 1; ; i++ {
p := path + "_" + strconv.Itoa(i)
if _, ok := fields[p]; !ok {
path = p
break
}
}
}
fields[path] = v
}
} else {
p.debugEmptyQuery("field selection", selected, config.FieldSelection)
}
}
return metric.New(metricname, tags, fields, timestamp), nil
}
func getNodePath(node, relativeTo *xmlquery.Node, sep string) string {
names := make([]string, 0)
// Climb up the tree and collect the node names
n := node.Parent
for n != nil && n != relativeTo {
names = append(names, n.Data)
n = n.Parent
}
if len(names) < 1 {
return ""
}
// Construct the nodes
path := ""
for _, name := range names {
path = name + sep + path
}
return path[:len(path)-1]
}
func executeQuery(doc, selected *xmlquery.Node, query string) (r interface{}, err error) {
// Check if the query is relative or absolute and set the root for the query
root := selected
if strings.HasPrefix(query, "/") {
root = doc
}
// Compile the query
expr, err := xpath.Compile(query)
if err != nil {
return nil, fmt.Errorf("failed to compile query '%s': %v", query, err)
}
// Evaluate the compiled expression and handle returned node-iterators
// separately. Those iterators will be returned for queries directly
// referencing a node (value or attribute).
n := expr.Evaluate(xmlquery.CreateXPathNavigator(root))
if iter, ok := n.(*xpath.NodeIterator); ok {
// We got an iterator, so take the first match and get the referenced
// property. This will always be a string.
if iter.MoveNext() {
r = iter.Current().Value()
}
} else {
r = n
}
return r, nil
}
func splitLastPathElement(query string) []string {
// This is a rudimentary xpath-parser that splits the path
// into the last path element and the remaining path-part.
// The last path element is then further splitted into
// parts such as attributes or selectors. Each returned
// element is a full path!
// Nothing left
if query == "" || query == "/" || query == "//" || query == "." {
return []string{}
}
seperatorIdx := strings.LastIndex(query, "/")
if seperatorIdx < 0 {
query = "./" + query
seperatorIdx = 1
}
// For double slash we want to split at the first slash
if seperatorIdx > 0 && query[seperatorIdx-1] == byte('/') {
seperatorIdx--
}
base := query[:seperatorIdx]
if base == "" {
base = "/"
}
elements := make([]string, 1)
elements[0] = base
offset := seperatorIdx
if i := strings.Index(query[offset:], "::"); i >= 0 {
// Check for axis operator
offset += i
elements = append(elements, query[:offset]+"::*")
}
if i := strings.Index(query[offset:], "["); i >= 0 {
// Check for predicates
offset += i
elements = append(elements, query[:offset])
} else if i := strings.Index(query[offset:], "@"); i >= 0 {
// Check for attributes
offset += i
elements = append(elements, query[:offset])
}
return elements
}
func (p *Parser) debugEmptyQuery(operation string, root *xmlquery.Node, initialquery string) {
if p.Log == nil {
return
}
query := initialquery
// We already know that the
p.Log.Debugf("got 0 nodes for query %q in %s", query, operation)
for {
parts := splitLastPathElement(query)
if len(parts) < 1 {
return
}
for i := len(parts) - 1; i >= 0; i-- {
q := parts[i]
nodes, err := xmlquery.QueryAll(root, q)
if err != nil {
p.Log.Debugf("executing query %q in %s failed: %v", q, operation, err)
return
}
p.Log.Debugf("got %d nodes for query %q in %s", len(nodes), q, operation)
if len(nodes) > 0 && nodes[0] != nil {
return
}
query = parts[0]
}
}
}