forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
132 lines (116 loc) · 2.98 KB
/
handler.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
package influx
import (
"bytes"
"errors"
"strconv"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
)
// MetricHandler implements the Handler interface and produces telegraf.Metric.
type MetricHandler struct {
err error
timePrecision time.Duration
timeFunc TimeFunc
metric telegraf.Metric
}
func NewMetricHandler() *MetricHandler {
return &MetricHandler{
timePrecision: time.Nanosecond,
timeFunc: time.Now,
}
}
func (h *MetricHandler) SetTimePrecision(p time.Duration) {
h.timePrecision = p
// When the timestamp is omitted from the metric, the timestamp
// comes from the server clock, truncated to the nearest unit of
// measurement provided in precision.
//
// When a timestamp is provided in the metric, precision is
// overloaded to hold the unit of measurement of the timestamp.
}
func (h *MetricHandler) SetTimeFunc(f TimeFunc) {
h.timeFunc = f
}
func (h *MetricHandler) Metric() (telegraf.Metric, error) {
if h.metric.Time().IsZero() {
h.metric.SetTime(h.timeFunc().Truncate(h.timePrecision))
}
return h.metric, nil
}
func (h *MetricHandler) SetMeasurement(name []byte) error {
var err error
h.metric, err = metric.New(nameUnescape(name),
nil, nil, time.Time{})
return err
}
func (h *MetricHandler) AddTag(key []byte, value []byte) error {
tk := unescape(key)
tv := unescape(value)
h.metric.AddTag(tk, tv)
return nil
}
func (h *MetricHandler) AddInt(key []byte, value []byte) error {
fk := unescape(key)
fv, err := parseIntBytes(bytes.TrimSuffix(value, []byte("i")), 10, 64)
if err != nil {
if numerr, ok := err.(*strconv.NumError); ok {
return numerr.Err
}
return err
}
h.metric.AddField(fk, fv)
return nil
}
func (h *MetricHandler) AddUint(key []byte, value []byte) error {
fk := unescape(key)
fv, err := parseUintBytes(bytes.TrimSuffix(value, []byte("u")), 10, 64)
if err != nil {
if numerr, ok := err.(*strconv.NumError); ok {
return numerr.Err
}
return err
}
h.metric.AddField(fk, fv)
return nil
}
func (h *MetricHandler) AddFloat(key []byte, value []byte) error {
fk := unescape(key)
fv, err := parseFloatBytes(value, 64)
if err != nil {
if numerr, ok := err.(*strconv.NumError); ok {
return numerr.Err
}
return err
}
h.metric.AddField(fk, fv)
return nil
}
func (h *MetricHandler) AddString(key []byte, value []byte) error {
fk := unescape(key)
fv := stringFieldUnescape(value)
h.metric.AddField(fk, fv)
return nil
}
func (h *MetricHandler) AddBool(key []byte, value []byte) error {
fk := unescape(key)
fv, err := parseBoolBytes(value)
if err != nil {
return errors.New("unparseable bool")
}
h.metric.AddField(fk, fv)
return nil
}
func (h *MetricHandler) SetTimestamp(tm []byte) error {
v, err := parseIntBytes(tm, 10, 64)
if err != nil {
if numerr, ok := err.(*strconv.NumError); ok {
return numerr.Err
}
return err
}
//time precision is overloaded to mean time unit here
ns := v * int64(h.timePrecision)
h.metric.SetTime(time.Unix(0, ns))
return nil
}