-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegraf.go
145 lines (118 loc) · 2.84 KB
/
telegraf.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
package telegraf
import (
"bytes"
"fmt"
"net"
"net/url"
"sort"
"strconv"
"time"
)
type Tag struct {
key string
value string
}
type tagsLexi []Tag
func (s tagsLexi) Len() int { return len(s) }
func (s tagsLexi) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s tagsLexi) Less(i, j int) bool {
return bytes.Compare([]byte(s[i].key), []byte(s[j].key)) < 0
}
type Field struct {
key string
value interface{}
}
type fieldsLexi []Field
func (s fieldsLexi) Len() int { return len(s) }
func (s fieldsLexi) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s fieldsLexi) Less(i, j int) bool {
return bytes.Compare([]byte(s[i].key), []byte(s[j].key)) < 0
}
type Metric struct {
name string
tags []Tag
fields []Field
t time.Time
}
func fieldValueToString(val interface{}) string {
switch val := val.(type) {
case int8, int16, int32, int64, int:
return fmt.Sprintf("%di", val)
case float32:
return strconv.FormatFloat(float64(val), 'f', -1, 32)
case float64:
return strconv.FormatFloat(val, 'f', -1, 64)
case bool:
switch val {
case true:
return "T"
case false:
return "F"
}
}
s := Escape(fmt.Sprintf("%s", val), FieldVal)
return fmt.Sprintf("\"%s\"", s)
}
func (m *Metric) Serialize() []byte {
var buffer bytes.Buffer
buffer.WriteString(Escape(m.name, Name))
buffer.WriteString(",")
sort.Sort(tagsLexi(m.tags))
for i, tag := range m.tags {
buffer.WriteString(Escape(tag.key, TagKey))
buffer.WriteString("=")
buffer.WriteString(Escape(tag.value, TagVal))
if i < len(m.tags)-1 {
buffer.WriteString(",")
}
}
buffer.WriteString(" ")
sort.Sort(fieldsLexi(m.fields))
for i, field := range m.fields {
buffer.WriteString(Escape(field.key, FieldKey))
buffer.WriteString("=")
buffer.WriteString(fieldValueToString(field.value))
if i < len(m.fields)-1 {
buffer.WriteString(",")
}
}
buffer.WriteString(" ")
buffer.WriteString(strconv.FormatInt(m.t.UnixNano(), 10))
buffer.WriteString("\n")
return buffer.Bytes()
}
func NewMetric(name string, tags []Tag, fields []Field) Metric {
m := Metric{name, tags, fields, time.Now()}
return m
}
func (m *Metric) SetTime(t time.Time) {
m.t = t
}
type Client struct {
conn *net.UDPConn
}
func (c *Client) Send(m Metric) error {
// body := m.Serialize()
return nil
}
func (c *Client) Close() error {
c.conn.Close()
return nil
}
func NewClient(uri string) (*Client, error) {
parsedUrl, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("Error parsing UDP url [%s]: %s", uri, err)
}
udpAddr, err := net.ResolveUDPAddr("udp", parsedUrl.Host)
if err != nil {
return nil, fmt.Errorf("Error resolving UDP Address [%s]: %s",
parsedUrl.Host, err)
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
return nil, fmt.Errorf("Error dialing UDP address [%s]: %s",
udpAddr.String(), err)
}
return &Client{conn}, nil
}