-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
188 lines (163 loc) · 3.79 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
package main
//import "fmt"
import "strconv"
import "strings"
import "reflect"
import "encoding/json"
import influx "github.com/influxdata/influxdb/client/v2"
import "github.com/influxdata/influxdb/models"
import "github.com/hashicorp/errwrap"
const (
BOOLEAN MetricValueType = iota
STRING
NUMERIC
)
type Point struct {
Timestamp int64
Name string
Tags map[string]string
Fields map[string]MetricValue
}
type MetricValueType int
type MetricValue struct {
number float64
boolean bool
str string
typ MetricValueType
}
type Parser interface {
Parse(message []byte) (*influx.Point, error)
}
func NewParser(format string, precision string) Parser {
if format == "json" {
return JsonParser{precision: precision}
} else {
return LineProtocolParser{precision: precision}
}
}
type JsonParser struct {
precision string
}
func (p JsonParser) Parse(message []byte) (*influx.Point, error) {
var point Point
err := json.Unmarshal(message, &point)
if err != nil {
return nil, errwrap.Wrapf("Failed to parse the JSON encoded metric from Kafka: {{err}}", err)
}
t, err := models.SafeCalcTime(point.Timestamp, p.precision)
if err != nil {
return nil, errwrap.Wrapf("The metric timestamp is out of range", err)
}
influxPoint, err := influx.NewPoint(point.Name, point.Tags, point.getFields(), t)
if err != nil {
return nil, errwrap.Wrapf("The metric from Kafka is not valid: {{err}}", err)
}
return influxPoint, nil
}
type LineProtocolParser struct {
precision string
}
func (p LineProtocolParser) Parse(message []byte) (*influx.Point, error) {
points, err := models.ParsePoints(message)
if err != nil {
return nil, errwrap.Wrapf("Failed to parse line protocol metric from Kafka: {{err}}", err)
}
if len(points) >= 1 {
return influx.NewPointFrom(points[0]), nil
}
return nil, nil
}
func (p *Point) getFields() map[string]interface{} {
m := map[string]interface{}{}
for k, v := range p.Fields {
m[k] = v.getValue()
}
return m
}
func (p Point) String() string {
res := p.Name
res += "\n"
res += strconv.FormatInt(p.Timestamp, 10)
res += "\n"
res += "Tags: "
tags := []string{}
for tagname, tagvalue := range p.Tags {
tags = append(tags, tagname+": "+tagvalue)
}
res += strings.Join(tags, ", ")
res += "\n"
res += "Fields: "
fields := []string{}
for fieldname, fieldvalue := range p.Fields {
fields = append(fields, fieldname+": "+fieldvalue.String())
}
res += strings.Join(fields, ", ")
res += "\n"
return res
}
func (p MetricValue) String() string {
if p.typ == BOOLEAN {
if p.boolean {
return "true"
} else {
return "false"
}
} else if p.typ == NUMERIC {
return strconv.FormatFloat(p.number, 'f', -1, 64)
} else {
return p.str
}
}
func (p *MetricValue) getValue() interface{} {
if p.typ == BOOLEAN {
return p.boolean
} else if p.typ == NUMERIC {
return p.number
} else {
return p.str
}
}
func (p *MetricValue) UnmarshalJSON(b []byte) (err error) {
var num_val float64 = 0.0
boolean_val := false
str_val := ""
p.number = 0
p.boolean = false
p.str = ""
p.typ = 0
// check if the slice represents a boolean
err = json.Unmarshal(b, &boolean_val)
if err == nil {
p.boolean = boolean_val
p.typ = BOOLEAN
return nil
}
if _, ok := err.(*json.SyntaxError); ok {
return err
}
// check if the slice represents a float
err = json.Unmarshal(b, &num_val)
if err == nil {
p.number = num_val
p.typ = NUMERIC
return nil
}
if _, ok := err.(*json.SyntaxError); ok {
return err
}
// check if the slice represents a string
err = json.Unmarshal(b, &str_val)
if err == nil {
p.str = str_val
p.typ = STRING
return nil
}
if _, ok := err.(*json.SyntaxError); ok {
return err
}
if unmarshal_err, ok := err.(*json.UnmarshalTypeError); ok {
unmarshal_err.Type = reflect.TypeOf(*p)
return unmarshal_err
}
return err
}