forked from chrissnell/crabby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_graphite.go
136 lines (114 loc) · 3.87 KB
/
storage_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
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
package main
import (
"context"
"fmt"
"log"
"strconv"
"sync"
"github.com/marpaia/graphite-golang"
)
// GraphiteConfig describes the YAML-provided configuration for a Graphite
// storage backend
type GraphiteConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Protocol string `yaml:"protocol,omitempty"`
Namespace string `yaml:"metric-namespace,omitempty"`
}
// GraphiteStorage holds the configuration for a Graphite storage backend
type GraphiteStorage struct {
GraphiteConn *graphite.Graphite
Namespace string
}
// StartStorageEngine creates a goroutine loop to receive metrics and send
// them off to Graphite
func (g GraphiteStorage) StartStorageEngine(ctx context.Context, wg *sync.WaitGroup) (chan<- Metric, chan<- Event) {
// We're going to declare eventChan here but not initialize the channel because Graphite
// storage doesn't support Events, only Metrics. We should never receive an Event on this
// channel and if something mistakenly sends one, the program will panic.
var eventChan chan<- Event
// We *do* support Metrics, so we'll initialize this as a buffered channel
metricChan := make(chan Metric, 10)
// Start processing the metrics we receive
go g.processMetrics(ctx, wg, metricChan)
return metricChan, eventChan
}
func (g GraphiteStorage) processMetrics(ctx context.Context, wg *sync.WaitGroup, mchan <-chan Metric) {
wg.Add(1)
defer wg.Done()
for {
select {
case m := <-mchan:
err := g.sendMetric(m)
if err != nil {
log.Println(err)
}
case <-ctx.Done():
log.Println("Cancellation request recieved. Cancelling metrics processor.")
return
}
}
}
// sendMetric sends a metric value to Graphite
func (g GraphiteStorage) sendMetric(m Metric) error {
var metricName string
valStr := strconv.FormatFloat(m.Value, 'f', 3, 64)
if g.Namespace == "" {
metricName = fmt.Sprintf("crabby.%v.%v", m.Job, m.Timing)
} else {
metricName = fmt.Sprintf("%v.%v.%v", g.Namespace, m.Job, m.Timing)
}
if m.Timestamp.IsZero() {
err := g.GraphiteConn.SimpleSend(metricName, valStr)
if err != nil {
log.Printf("Could not send metric %v: %v\n", metricName, err)
return err
}
return nil
}
gm := graphite.Metric{
Name: metricName,
Value: valStr,
Timestamp: m.Timestamp.Unix(),
}
err := g.GraphiteConn.SendMetric(gm)
if err != nil {
log.Printf("Could not send metric %v: %v\n", metricName, err)
return err
}
return nil
}
// sendEvent is necessary to implement the StorageEngine interface.
func (g GraphiteStorage) sendEvent(e Event) error {
var err error
return err
}
// NewGraphiteStorage sets up a new Graphite storage backend
func NewGraphiteStorage(c *Config) GraphiteStorage {
var err error
g := GraphiteStorage{}
g.Namespace = c.Storage.Graphite.Namespace
switch c.Storage.Graphite.Protocol {
case "tcp":
g.GraphiteConn, err = graphite.NewGraphite(c.Storage.Graphite.Host, c.Storage.Graphite.Port)
if err != nil {
log.Println("Warning: could not create Graphite connection. Using no-op dummy driver instead.", err)
g.GraphiteConn = graphite.NewGraphiteNop(c.Storage.Graphite.Host, c.Storage.Graphite.Port)
}
case "udp":
g.GraphiteConn, err = graphite.NewGraphiteUDP(c.Storage.Graphite.Host, c.Storage.Graphite.Port)
if err != nil {
log.Println("Warning: could not create Graphite connection. Using no-op dummy driver instead.", err)
g.GraphiteConn = graphite.NewGraphiteNop(c.Storage.Graphite.Host, c.Storage.Graphite.Port)
}
case "nop":
g.GraphiteConn = graphite.NewGraphiteNop(c.Storage.Graphite.Host, c.Storage.Graphite.Port)
default:
g.GraphiteConn, err = graphite.NewGraphite(c.Storage.Graphite.Host, c.Storage.Graphite.Port)
if err != nil {
log.Println("Warning: could not create Graphite connection. Using no-op dummy driver instead.", err)
g.GraphiteConn = graphite.NewGraphiteNop(c.Storage.Graphite.Host, c.Storage.Graphite.Port)
}
}
return g
}