/
graphite.go
144 lines (129 loc) · 2.86 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
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
137
138
139
140
141
142
143
144
package graphite
import (
"fmt"
"io"
"net"
"regexp"
"strings"
"sync"
"time"
"github.com/FZambia/eagle"
"github.com/prometheus/client_golang/prometheus"
)
var re = regexp.MustCompile("[[:^ascii:]]")
// PreparePathComponent cleans string to be used as Graphite metric path.
func PreparePathComponent(s string) string {
s = re.ReplaceAllLiteralString(s, "_")
return strings.Replace(s, ".", "_", -1)
}
// Exporter to Graphite.
type Exporter struct {
prefix string
address string
timeout time.Duration
tags bool
closeOnce sync.Once
closeCh chan struct{}
sink chan eagle.Metrics
eagle *eagle.Eagle
}
// Config for Graphite Exporter.
type Config struct {
Address string
Gatherer prometheus.Gatherer
Interval time.Duration
Prefix string
Tags bool
}
// New creates new Graphite Exporter.
func New(c Config) *Exporter {
exporter := &Exporter{
prefix: c.Prefix,
address: c.Address,
timeout: time.Second,
tags: c.Tags,
closeCh: make(chan struct{}),
sink: make(chan eagle.Metrics),
}
exporter.eagle = eagle.New(eagle.Config{
Gatherer: c.Gatherer,
Interval: c.Interval,
Sink: exporter.sink,
})
go exporter.run()
return exporter
}
func (e *Exporter) run() {
for {
select {
case <-e.closeCh:
return
case metrics := <-e.sink:
e.exportOnce(metrics)
}
}
}
// Close stops exporter.
func (e *Exporter) Close() error {
e.closeOnce.Do(func() {
close(e.closeCh)
e.eagle.Close()
})
return nil
}
func (e *Exporter) exportOnce(metrics eagle.Metrics) error {
conn, err := net.DialTimeout("tcp", e.address, e.timeout)
if err != nil {
return err
}
defer conn.Close()
e.write(conn, metrics)
return nil
}
func makeTags(labels []string) string {
if len(labels) == 0 {
return ""
}
tagParts := []string{}
for i := 0; i < len(labels); i += 2 {
tagParts = append(tagParts, fmt.Sprintf("%s=%s", labels[i], labels[i+1]))
}
return ";" + strings.Join(tagParts, ";")
}
func (e *Exporter) write(w io.Writer, metrics eagle.Metrics) error {
now := time.Now().Unix()
for _, item := range metrics.Items {
for _, metricValue := range item.Values {
parts := []string{e.prefix}
if item.Namespace != "" {
parts = append(parts, item.Namespace)
}
if item.Subsystem != "" {
parts = append(parts, item.Subsystem)
}
if item.Name != "" {
parts = append(parts, item.Name)
}
if metricValue.Name != "" {
parts = append(parts, metricValue.Name)
}
if !e.tags {
parts = append(parts, metricValue.Labels...)
}
key := strings.Join(parts, ".")
if e.tags {
key += makeTags(metricValue.Labels)
}
var err error
if item.Type == eagle.MetricTypeCounter {
_, err = fmt.Fprintf(w, "%s %d %d\n", key, int64(metricValue.Value), now)
} else {
_, err = fmt.Fprintf(w, "%s %f %d\n", key, metricValue.Value, now)
}
if err != nil {
return err
}
}
}
return nil
}