-
Notifications
You must be signed in to change notification settings - Fork 150
/
flume.go
229 lines (213 loc) · 8.25 KB
/
flume.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package mpflume
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"strconv"
"strings"
mp "github.com/mackerelio/go-mackerel-plugin"
)
const (
// Channel is Flume Channel Type
Channel = "CHANNEL"
// Sink is Flume Sink Type
Sink = "SINK"
// Source is Flume Source Type
Source = "SOURCE"
)
// FlumePlugin mackerel plugin
type FlumePlugin struct {
URI string
Prefix string
}
// MetricKeyPrefix interface for PluginWithPrefix
func (p *FlumePlugin) MetricKeyPrefix() string {
if p.Prefix == "" {
p.Prefix = "flume"
}
return p.Prefix
}
// GraphDefinition interface for mackerelplugin
func (p *FlumePlugin) GraphDefinition() map[string]mp.Graphs {
labelPrefix := strings.Title(p.Prefix)
return map[string]mp.Graphs{
"channel.capacity.#": {
Label: labelPrefix + " Channel Capacity",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "ChannelCapacity", Label: "capacity"},
{Name: "ChannelSize", Label: "size"},
},
},
"channel.use_rate.#": {
Label: labelPrefix + " Channel Use Rate",
Unit: "percentage",
Metrics: []mp.Metrics{
{Name: "ChannelFillPercentage", Label: "fill percentage"},
},
},
"channel.event_put_num.#": {
Label: labelPrefix + " Channel Event Put Num",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "EventPutAttemptCount", Label: "attempt", Diff: true},
{Name: "EventPutSuccessCount", Label: "success", Diff: true},
},
},
"channel.event_take_num.#": {
Label: labelPrefix + " Channel Event Take Num",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "EventTakeAttemptCount", Label: "attempt", Diff: true},
{Name: "EventTakeSuccessCount", Label: "success", Diff: true},
},
},
"sink.batch_num.#": {
Label: labelPrefix + " Sink Batch Num",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "BatchCompleteCount", Label: "complete", Diff: true},
{Name: "BatchEmptyCount", Label: "empty", Diff: true},
{Name: "BatchUnderflowCount", Label: "underflow", Diff: true},
},
},
"sink.connection.#": {
Label: labelPrefix + " Sink Connection",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "ConnectionCreatedCount", Label: "created", Diff: true},
{Name: "ConnectionClosedCount", Label: "closed", Diff: true},
{Name: "ConnectionFailedCount", Label: "failed", Diff: true},
},
},
"sink.event_drain_num.#": {
Label: labelPrefix + " Sink Event Drain Num",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "EventDrainAttemptCount", Label: "attempt", Diff: true},
{Name: "EventDrainSuccessCount", Label: "success", Diff: true},
},
},
"source.append_num.#": {
Label: labelPrefix + " Source Append Num",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "AppendAcceptedCount", Label: "accepted", Diff: true},
{Name: "AppendReceivedCount", Label: "received", Diff: true},
},
},
"source.append_batch_num.#": {
Label: labelPrefix + " Source Append Batch Num",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "AppendBatchAcceptedCount", Label: "accepted", Diff: true},
{Name: "AppendBatchReceivedCount", Label: "received", Diff: true},
},
},
"source.event_num.#": {
Label: labelPrefix + " Source Event Num",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "EventAcceptedCount", Label: "accepted", Diff: true},
{Name: "EventReceivedCount", Label: "received", Diff: true},
},
},
"source.connection.#": {
Label: labelPrefix + " Source Connection",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "OpenConnectionCount", Label: "open"},
},
},
}
}
// FetchMetrics interface for mackerelplugin
func (p *FlumePlugin) FetchMetrics() (map[string]float64, error) {
m, err := p.getMetrics()
if err != nil {
return nil, err
}
return p.parseMetrics(m), nil
}
func (p *FlumePlugin) getMetrics() (map[string]interface{}, error) {
req, err := http.NewRequest(http.MethodGet, p.URI, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "mackerel-plugin-flume")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var m map[string]interface{}
dec := json.NewDecoder(res.Body)
if err := dec.Decode(&m); err != nil {
return nil, err
}
return m, nil
}
func (p *FlumePlugin) parseMetrics(metrics map[string]interface{}) map[string]float64 {
ret := make(map[string]float64)
for k, v := range metrics {
array := strings.Split(k, ".")
typeName := array[0]
componentName := array[1]
switch typeName {
case Channel:
p.parseChannel(ret, componentName, v.(map[string]interface{}))
case Sink:
p.parseSink(ret, componentName, v.(map[string]interface{}))
case Source:
p.parseSource(ret, componentName, v.(map[string]interface{}))
}
}
return ret
}
func (p *FlumePlugin) convertFloat64(value string) float64 {
f, _ := strconv.ParseFloat(value, 64)
return f
}
func (p *FlumePlugin) parseChannel(ret map[string]float64, componentName string, value map[string]interface{}) {
ret["channel.capacity."+componentName+".ChannelCapacity"] = p.convertFloat64(value["ChannelCapacity"].(string))
ret["channel.capacity."+componentName+".ChannelSize"] = p.convertFloat64(value["ChannelSize"].(string))
ret["channel.use_rate."+componentName+".ChannelFillPercentage"] = p.convertFloat64(value["ChannelFillPercentage"].(string))
ret["channel.event_put_num."+componentName+".EventPutAttemptCount"] = p.convertFloat64(value["EventPutAttemptCount"].(string))
ret["channel.event_put_num."+componentName+".EventPutSuccessCount"] = p.convertFloat64(value["EventPutSuccessCount"].(string))
ret["channel.event_take_num."+componentName+".EventTakeAttemptCount"] = p.convertFloat64(value["EventTakeAttemptCount"].(string))
ret["channel.event_take_num."+componentName+".EventTakeSuccessCount"] = p.convertFloat64(value["EventTakeSuccessCount"].(string))
}
func (p *FlumePlugin) parseSink(ret map[string]float64, componentName string, value map[string]interface{}) {
ret["sink.batch_num."+componentName+".BatchCompleteCount"] = p.convertFloat64(value["BatchCompleteCount"].(string))
ret["sink.batch_num."+componentName+".BatchEmptyCount"] = p.convertFloat64(value["BatchEmptyCount"].(string))
ret["sink.batch_num."+componentName+".BatchUnderflowCount"] = p.convertFloat64(value["BatchUnderflowCount"].(string))
ret["sink.connection."+componentName+".ConnectionCreatedCount"] = p.convertFloat64(value["ConnectionCreatedCount"].(string))
ret["sink.connection."+componentName+".ConnectionClosedCount"] = p.convertFloat64(value["ConnectionClosedCount"].(string))
ret["sink.connection."+componentName+".ConnectionFailedCount"] = p.convertFloat64(value["ConnectionFailedCount"].(string))
ret["sink.event_drain_num."+componentName+".EventDrainAttemptCount"] = p.convertFloat64(value["EventDrainAttemptCount"].(string))
ret["sink.event_drain_num."+componentName+".EventDrainSuccessCount"] = p.convertFloat64(value["EventDrainSuccessCount"].(string))
}
func (p *FlumePlugin) parseSource(ret map[string]float64, componentName string, value map[string]interface{}) {
ret["source.append_num."+componentName+".AppendAcceptedCount"] = p.convertFloat64(value["AppendAcceptedCount"].(string))
ret["source.append_num."+componentName+".AppendReceivedCount"] = p.convertFloat64(value["AppendReceivedCount"].(string))
ret["source.append_batch_num."+componentName+".AppendBatchAcceptedCount"] = p.convertFloat64(value["AppendBatchAcceptedCount"].(string))
ret["source.append_batch_num."+componentName+".AppendBatchReceivedCount"] = p.convertFloat64(value["AppendBatchReceivedCount"].(string))
ret["source.event_num."+componentName+".EventAcceptedCount"] = p.convertFloat64(value["EventAcceptedCount"].(string))
ret["source.event_num."+componentName+".EventReceivedCount"] = p.convertFloat64(value["EventReceivedCount"].(string))
ret["source.connection."+componentName+".OpenConnectionCount"] = p.convertFloat64(value["OpenConnectionCount"].(string))
}
// Do the plugin
func Do() {
optHost := flag.String("host", "localhost", "Host Name")
optPort := flag.String("port", "41414", "Port")
optPrefix := flag.String("metric-key-prefix", "", "Metric key prefix")
optTempfile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
plugin := mp.NewMackerelPlugin(&FlumePlugin{
URI: fmt.Sprintf("http://%s:%s/metrics", *optHost, *optPort),
Prefix: *optPrefix,
})
plugin.Tempfile = *optTempfile
plugin.Run()
}