-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphite.go
executable file
·97 lines (87 loc) · 2.94 KB
/
graphite.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
// Package graphite provides simple Graphite client
package graphite
import (
"fmt"
"net"
"strconv"
"time"
)
var timeNow = time.Now
const defaultTimeout = 3 * time.Second
// Client - struct with Graphite connection settings.
type Client struct {
host string
port int
prefix string
protocol string
timeOut time.Duration
}
// NewClient - returns new Client with default connection timeout set to 3s.
func NewClient(Host string, Port int, Prefix string, Protocol string) *Client {
return &Client{
host: Host,
port: Port,
prefix: Prefix,
protocol: Protocol,
timeOut: defaultTimeout,
}
}
// SendData - creates new connection to Graphite server and pushes
// provided batch of metrics in this single connection, thread-safe.
//
// Returns error in case of problems establishing, sending data or closing the connection
// (which should not be a problem with such short-lived connections).
//
// SendData receives as argument map[string]int64 where string is metric name,
// float64 is metric value, example:
// map[string]float64{"test": 1234.1234, "test": 1234.1234}
func (g *Client) SendData(data map[string]float64) error {
conn, err := net.DialTimeout(g.protocol, g.host+":"+strconv.Itoa(g.port), g.timeOut)
if err != nil {
return err
}
dataToSent := g.prepareData(data, timeNow().Unix())
for _, str := range dataToSent {
_, err = conn.Write([]byte(str))
if err != nil {
return err
}
}
// it's safe to close connection here because
// we are not exiting the function elsewhere after connection is open
return conn.Close()
}
// SendDataWithTimeStamp - creates new connection to Graphite server and pushes
// provided batch of metrics in this single connection, thread-safe.
//
// Returns error in case of problems establishing, sending data or closing the connection
// (which should not be a problem with such short-lived connections).
//
// SendData receives as first argument map[string]int64 where string is metric name,
// float64 is metric value, example:
// map[string]float64{"test": 1234.1234, "test": 1234.1234}
// and as a second argument Unix timestamp with which the metrics will be sent, example:
// timeNow().Unix()
func (g *Client) SendDataWithTimeStamp(data map[string]float64, timestamp int64) error {
conn, err := net.DialTimeout(g.protocol, g.host+":"+strconv.Itoa(g.port), g.timeOut)
if err != nil {
return err
}
dataToSent := g.prepareData(data, timestamp)
for _, str := range dataToSent {
_, err = conn.Write([]byte(str))
if err != nil {
return err
}
}
// it's safe to close connection here because
// we are not exiting the function elsewhere after connection is open
return conn.Close()
}
func (g *Client) prepareData(data map[string]float64, timestamp int64) []string {
dataToGraphite := make([]string, 0)
for metricName, metricVal := range data {
dataToGraphite = append(dataToGraphite, fmt.Sprintf("%s.%s %f %d\n", g.prefix, metricName, metricVal, timestamp))
}
return dataToGraphite
}