forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
239 lines (210 loc) · 6.45 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
package dropwizard
import (
"encoding/json"
"fmt"
"time"
"github.com/tidwall/gjson"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/templating"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/influx"
)
// Parser parses json inputs containing dropwizard metrics,
// either top-level or embedded inside a json field.
// This parser is using gjson for retrieving paths within the json file.
type Parser struct {
MetricRegistryPath string `toml:"dropwizard_metric_registry_path"`
TimePath string `toml:"dropwizard_time_path"`
TimeFormat string `toml:"dropwizard_time_format"`
TagsPath string `toml:"dropwizard_tags_path"`
TagPathsMap map[string]string `toml:"dropwizard_tag_paths_map"`
Separator string `toml:"separator"`
Templates []string `toml:"templates"`
DefaultTags map[string]string `toml:"-"`
Log telegraf.Logger `toml:"-"`
templateEngine *templating.Engine
// seriesParser parses line protocol measurement + tags
seriesParser *influx.Parser
}
// Parse parses the input bytes to an array of metrics
func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
metrics := make([]telegraf.Metric, 0)
metricTime, err := p.parseTime(buf)
if err != nil {
return nil, err
}
dwr, err := p.unmarshalMetrics(buf)
if err != nil {
return nil, err
}
metrics = p.readDWMetrics("counter", dwr["counters"], metrics, metricTime)
metrics = p.readDWMetrics("meter", dwr["meters"], metrics, metricTime)
metrics = p.readDWMetrics("gauge", dwr["gauges"], metrics, metricTime)
metrics = p.readDWMetrics("histogram", dwr["histograms"], metrics, metricTime)
metrics = p.readDWMetrics("timer", dwr["timers"], metrics, metricTime)
jsonTags := p.readTags(buf)
// fill json tags first
if len(jsonTags) > 0 {
for _, m := range metrics {
for k, v := range jsonTags {
// only set the tag if it doesn't already exist:
if !m.HasTag(k) {
m.AddTag(k, v)
}
}
}
}
// fill default tags last
if len(p.DefaultTags) > 0 {
for _, m := range metrics {
for k, v := range p.DefaultTags {
// only set the default tag if it doesn't already exist:
if !m.HasTag(k) {
m.AddTag(k, v)
}
}
}
}
return metrics, nil
}
// ParseLine is not supported by the dropwizard format
func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
return nil, fmt.Errorf("ParseLine not supported: %s, for data format: dropwizard", line)
}
// SetDefaultTags sets the default tags
func (p *Parser) SetDefaultTags(tags map[string]string) {
p.DefaultTags = tags
}
func (p *Parser) readTags(buf []byte) map[string]string {
if p.TagsPath != "" {
var tagsBytes []byte
tagsResult := gjson.GetBytes(buf, p.TagsPath)
if tagsResult.Index > 0 {
tagsBytes = buf[tagsResult.Index : tagsResult.Index+len(tagsResult.Raw)]
} else {
tagsBytes = []byte(tagsResult.Raw)
}
var tags map[string]string
err := json.Unmarshal(tagsBytes, &tags)
if err != nil {
p.Log.Warnf("Failed to parse tags from JSON path %q: %s", p.TagsPath, err.Error())
} else if len(tags) > 0 {
return tags
}
}
tags := make(map[string]string)
for tagKey, jsonPath := range p.TagPathsMap {
tags[tagKey] = gjson.GetBytes(buf, jsonPath).String()
}
return tags
}
func (p *Parser) parseTime(buf []byte) (time.Time, error) {
if p.TimePath != "" {
timeFormat := p.TimeFormat
if timeFormat == "" {
timeFormat = time.RFC3339
}
timeString := gjson.GetBytes(buf, p.TimePath).String()
if timeString == "" {
return time.Time{}, fmt.Errorf("time not found in JSON path %s", p.TimePath)
}
t, err := time.Parse(timeFormat, timeString)
if err != nil {
return time.Time{}, fmt.Errorf("time %s cannot be parsed with format %s, %w", timeString, timeFormat, err)
}
return t.UTC(), nil
}
return time.Now(), nil
}
func (p *Parser) unmarshalMetrics(buf []byte) (map[string]interface{}, error) {
var registryBytes []byte
if p.MetricRegistryPath != "" {
regResult := gjson.GetBytes(buf, p.MetricRegistryPath)
if regResult.Index > 0 {
registryBytes = buf[regResult.Index : regResult.Index+len(regResult.Raw)]
} else {
registryBytes = []byte(regResult.Raw)
}
if len(registryBytes) == 0 {
err := fmt.Errorf("metric registry not found in JSON path %s", p.MetricRegistryPath)
return nil, err
}
} else {
registryBytes = buf
}
var jsonOut map[string]interface{}
err := json.Unmarshal(registryBytes, &jsonOut)
if err != nil {
err = fmt.Errorf("unable to parse dropwizard metric registry from JSON document, %w", err)
return nil, err
}
return jsonOut, nil
}
func (p *Parser) readDWMetrics(metricType string, dwms interface{}, metrics []telegraf.Metric, tm time.Time) []telegraf.Metric {
if dwmsTyped, ok := dwms.(map[string]interface{}); ok {
for dwmName, dwmFields := range dwmsTyped {
measurementName := dwmName
tags := make(map[string]string)
fieldPrefix := ""
if p.templateEngine != nil {
measurementName, tags, fieldPrefix, _ = p.templateEngine.Apply(dwmName)
if len(fieldPrefix) > 0 {
fieldPrefix = fmt.Sprintf("%s%s", fieldPrefix, p.Separator)
}
}
parsed, err := p.seriesParser.Parse([]byte(measurementName))
var m telegraf.Metric
if err != nil || len(parsed) != 1 {
m = metric.New(measurementName, map[string]string{}, map[string]interface{}{}, tm)
} else {
m = parsed[0]
m.SetTime(tm)
}
m.AddTag("metric_type", metricType)
for k, v := range tags {
m.AddTag(k, v)
}
if fields, ok := dwmFields.(map[string]interface{}); ok {
for k, v := range fields {
switch v := v.(type) {
case float64, string, bool:
m.AddField(fieldPrefix+k, v)
default:
// ignore
}
}
}
metrics = append(metrics, m)
}
}
return metrics
}
func (p *Parser) Init() error {
parser := &influx.Parser{
Type: "series",
}
err := parser.Init()
if err != nil {
return err
}
p.seriesParser = parser
if len(p.Templates) != 0 {
defaultTemplate, err := templating.NewDefaultTemplateWithPattern("measurement*")
if err != nil {
return err
}
templateEngine, err := templating.NewEngine(p.Separator, defaultTemplate, p.Templates)
if err != nil {
return err
}
p.templateEngine = templateEngine
}
return nil
}
func init() {
parsers.Add("dropwizard",
func(defaultMetricName string) telegraf.Parser {
return &Parser{}
})
}