-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphite_stdout.go
More file actions
92 lines (77 loc) · 2.09 KB
/
Copy pathgraphite_stdout.go
File metadata and controls
92 lines (77 loc) · 2.09 KB
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
package graphite
import (
"fmt"
"sync"
"time"
)
// NewGraphiteStdout is a factory method that's used to create a new GraphiteStdout struct
func NewGraphiteStdout(conf *Config) (*GraphiteStdout, error) {
server := GraphiteStdout{
prefix: conf.Prefix,
}
return &server, nil
}
// GraphiteStdout is a struct that write metric in stdout
type GraphiteStdout struct {
prefix string
lock sync.Mutex
}
// Connect dummy method for Graphite interface implement's
func (graphite *GraphiteStdout) Connect() error {
return nil
}
// Disconnect dummy method for Graphite interface implement's
func (graphite *GraphiteStdout) Disconnect() error {
return nil
}
// SendMetric send one metric to stdout
func (graphite *GraphiteStdout) 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
fmt.Printf("%s %s=%v",
time.Unix(sendingMetric.Timestamp, 0).Format("2006-01-02 15:04:05"),
sendingMetric.Name,
sendingMetric.Value)
return nil
}
// SendMetrics method sends the many metrics to metric server
func (graphite *GraphiteStdout) 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 GraphiteStdout host with the current timestamp
func (graphite *GraphiteStdout) 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
}