forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriemann.go
192 lines (162 loc) · 4.39 KB
/
riemann.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//go:generate ../../../tools/readme_config_includer/generator
package riemann
import (
_ "embed"
"fmt"
"net/url"
"os"
"sort"
"strings"
"time"
"github.com/amir/raidman"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/outputs"
)
//go:embed sample.conf
var sampleConfig string
type Riemann struct {
URL string `toml:"url"`
TTL float32 `toml:"ttl"`
Separator string `toml:"separator"`
MeasurementAsAttribute bool `toml:"measurement_as_attribute"`
StringAsState bool `toml:"string_as_state"`
TagKeys []string `toml:"tag_keys"`
Tags []string `toml:"tags"`
DescriptionText string `toml:"description_text"`
Timeout config.Duration `toml:"timeout"`
Log telegraf.Logger `toml:"-"`
client *raidman.Client
}
func (*Riemann) SampleConfig() string {
return sampleConfig
}
func (r *Riemann) Connect() error {
parsedURL, err := url.Parse(r.URL)
if err != nil {
return err
}
client, err := raidman.DialWithTimeout(parsedURL.Scheme, parsedURL.Host, time.Duration(r.Timeout))
if err != nil {
r.client = nil
return err
}
r.client = client
return nil
}
func (r *Riemann) Close() (err error) {
if r.client != nil {
err = r.client.Close()
r.client = nil
}
return err
}
func (r *Riemann) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 {
return nil
}
if r.client == nil {
if err := r.Connect(); err != nil {
return fmt.Errorf("failed to (re)connect to Riemann: %w", err)
}
}
// build list of Riemann events to send
var events []*raidman.Event
for _, m := range metrics {
evs := r.buildRiemannEvents(m)
events = append(events, evs...)
}
if err := r.client.SendMulti(events); err != nil {
r.Close()
return fmt.Errorf("failed to send riemann message: %w", err)
}
return nil
}
func (r *Riemann) buildRiemannEvents(m telegraf.Metric) []*raidman.Event {
events := make([]*raidman.Event, 0, len(m.Fields()))
for fieldName, value := range m.Fields() {
// get host for Riemann event
host, ok := m.Tags()["host"]
if !ok {
if hostname, err := os.Hostname(); err == nil {
host = hostname
} else {
host = "unknown"
}
}
event := &raidman.Event{
Host: host,
Ttl: r.TTL,
Description: r.DescriptionText,
Time: m.Time().Unix(),
Attributes: r.attributes(m.Name(), m.Tags()),
Service: r.service(m.Name(), fieldName),
Tags: r.tags(m.Tags()),
}
switch value := value.(type) {
case string:
// only send string metrics if explicitly enabled, skip otherwise
if !r.StringAsState {
r.Log.Debugf("Riemann event states disabled, skipping metric value [%s]", value)
continue
}
event.State = value
case int, int64, uint64, float32, float64:
event.Metric = value
default:
r.Log.Debugf("Riemann does not support metric value [%s]", value)
continue
}
events = append(events, event)
}
return events
}
func (r *Riemann) attributes(name string, tags map[string]string) map[string]string {
if r.MeasurementAsAttribute {
tags["measurement"] = name
}
delete(tags, "host") // exclude 'host' tag
return tags
}
func (r *Riemann) service(name, field string) string {
var serviceStrings []string
// if measurement is not enabled as an attribute then prepend it to service name
if !r.MeasurementAsAttribute {
serviceStrings = append(serviceStrings, name)
}
serviceStrings = append(serviceStrings, field)
return strings.Join(serviceStrings, r.Separator)
}
func (r *Riemann) tags(tags map[string]string) []string {
// always add specified Riemann tags
values := r.Tags
// if tag_keys are specified, add those and return tag list
if len(r.TagKeys) > 0 {
for _, tagName := range r.TagKeys {
value, ok := tags[tagName]
if ok {
values = append(values, value)
}
}
return values
}
// otherwise add all values from telegraf tag key/value pairs
keys := make([]string, 0, len(tags))
for key := range tags {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
if key != "host" { // exclude 'host' tag
values = append(values, tags[key])
}
}
return values
}
func init() {
outputs.Add("riemann", func() telegraf.Output {
return &Riemann{
Timeout: config.Duration(time.Second * 5),
}
})
}