-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphite_udp.go
131 lines (112 loc) · 2.63 KB
/
graphite_udp.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
package graphite
import (
"fmt"
"net"
"sync"
"time"
)
// NewGraphiteUDP is a factory method that's used to create a new GraphiteUDP struct
func NewGraphiteUDP(conf *Config) (*GraphiteUDP, error) {
server := GraphiteUDP{
address: conf.Address,
prefix: conf.Prefix,
}
err := server.Connect()
if err != nil {
return nil, err
}
return &server, nil
}
// GraphiteUDP is a struct that defines UDP graphite connection
type GraphiteUDP struct {
address string
prefix string
conn net.Conn
lock sync.Mutex
}
// Connect UDP to metric server
func (graphite *GraphiteUDP) Connect() error {
graphite.lock.Lock()
defer graphite.lock.Unlock()
if graphite.conn != nil {
if err := graphite.conn.Close(); err != nil {
return err
}
}
udpAddr, err := net.ResolveUDPAddr("udp", graphite.address)
if err != nil {
return err
}
conn, err := net.DialUDP("udp",
nil,
udpAddr)
if err != nil {
if conn != nil {
if err = conn.Close(); err != nil {
return err
}
}
return err
}
graphite.conn = conn
return nil
}
// Disconnect closes the GraphiteUDP.conn field
func (graphite *GraphiteUDP) Disconnect() error {
graphite.lock.Lock()
defer graphite.lock.Unlock()
err := graphite.conn.Close()
graphite.conn = nil
return err
}
// SendMetric send one metric to metric server
func (graphite *GraphiteUDP) SendMetric(metric *Metric) error {
graphite.lock.Lock()
defer graphite.lock.Unlock()
sendingMetric := Metric{}
if metric.Name == "" {
return nil
}
if metric.Timestamp == 0 {
sendingMetric.Timestamp = time.Now().Unix()
} else {
sendingMetric.Timestamp = metric.Timestamp
}
if graphite.prefix == "" {
sendingMetric.Name = metric.Name
} else {
sendingMetric.Name = fmt.Sprintf("%s.%s", graphite.prefix, metric.Name)
}
sendingMetric.Value = metric.Value
_, err := graphite.conn.Write(sendingMetric.ToByte())
if err != nil {
return err
}
return nil
}
// SendMetrics method sends the many metrics to metric server
func (graphite *GraphiteUDP) SendMetrics(metrics *[]Metric) error {
for _, metric := range *metrics {
err := graphite.SendMetric(&metric)
if err != nil {
return err
}
}
return nil
}
// SimpleSend method can be used to just pass a metric name and value and
// have it be sent to the GraphiteUDP host with the current timestamp
func (graphite *GraphiteUDP) SimpleSend(name string, value interface{}) error {
var metricName string
if graphite.prefix == "" {
metricName = name
} else {
metricName = fmt.Sprintf("%s.%s", graphite.prefix, name)
}
metric := NewMetric(metricName, value, time.Now().Unix())
err := graphite.SendMetric(&metric)
if err != nil {
return err
}
return nil
}