-
Notifications
You must be signed in to change notification settings - Fork 13
/
output.go
102 lines (90 loc) · 2.24 KB
/
output.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
package main
import (
"fmt"
"net"
"os"
"time"
)
type OutputHandler struct {
OpenTSDBAddress string
GraphiteAddress string
AmqpAddress string
InfluxDBAddress string
Hostname string
}
func (o *OutputHandler) WriteMetrics(all []*Metric) (e error) {
if o.Hostname == "" {
hn, e := os.Hostname()
if e != nil {
return e
}
o.Hostname = hn
}
sent := false
if o.OpenTSDBAddress != "" {
e = SendMetricsToOpenTSDB(o.OpenTSDBAddress, all, o.Hostname)
sent = true
}
if o.GraphiteAddress != "" {
e = SendMetricsToGraphite(o.GraphiteAddress, all, o.Hostname)
sent = true
}
if o.InfluxDBAddress != "" {
e = PublishMetricsWithInfluxDB(o.InfluxDBAddress, all, o.Hostname)
if e != nil {
logger.Printf("ERROR: %q", e)
} else {
sent = true
}
}
if o.AmqpAddress != "" {
e = PublishMetricsWithAMQP(o.AmqpAddress, all, o.Hostname)
if e != nil {
logger.Printf("ERROR: %q", e)
} else {
sent = true
}
}
if !sent {
SendMetricsToStdout(all, o.Hostname)
}
return
}
func SendMetricsToGraphite(address string, metrics []*Metric, hostname string) (e error) {
return SendMetricsWith(address, metrics, hostname, func(started time.Time, metric *Metric) string {
return metric.Graphite(started, hostname)
},
)
}
func SendMetricsToOpenTSDB(address string, metrics []*Metric, hostname string) (e error) {
return SendMetricsWith(address, metrics, hostname, func(started time.Time, metric *Metric) string {
return metric.OpenTSDB(started, hostname)
},
)
}
func SendMetricsWith(address string, metrics []*Metric, hostname string, serializer func(time.Time, *Metric) string) (e error) {
started := time.Now()
con, e := net.DialTimeout("tcp", address, 1*time.Second)
if e != nil {
return
}
defer con.Close()
fmt.Printf("connected in %.06f\n", time.Now().Sub(started).Seconds())
started = time.Now()
debug := os.Getenv("DEBUG") == "true"
for _, m := range metrics {
line := serializer(started, m)
if debug {
fmt.Println(line)
}
fmt.Fprintln(con, line)
}
fmt.Printf("sent %d metrics in %.06f\n", len(metrics), time.Now().Sub(started).Seconds())
return
}
func SendMetricsToStdout(metrics []*Metric, hostname string) {
now := time.Now()
for _, m := range metrics {
fmt.Println(m.Ascii(now, hostname))
}
}