-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
264 lines (250 loc) · 7.38 KB
/
plugin.go
File metadata and controls
264 lines (250 loc) · 7.38 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package qfilter_metrics
import (
"fmt"
"time"
"strings"
"strconv"
"github.com/zpatrick/go-config"
"github.com/qframe/types/messages"
"github.com/qframe/types/plugin"
"github.com/qframe/types/qchannel"
"github.com/qframe/types/constants"
"github.com/qframe/types/metrics"
"reflect"
"github.com/docker/docker/api/types"
)
const (
version = "0.2.7"
pluginTyp = qtypes_constants.FILTER
pluginPkg = "metric"
)
var (
containerStates = map[string]float64{
"create": 1.0,
"start": 2.0,
"healthy": 3.0,
"unhealthy": 4.0,
"kill": -1.0,
"die": -2.0,
"stop": -3.0,
"destroy": -4.0,
}
)
type Plugin struct {
*qtypes_plugin.Plugin
}
func New(qChan qtypes_qchannel.QChan, cfg *config.Config, name string) (p Plugin, err error) {
p = Plugin{
Plugin: qtypes_plugin.NewNamedPlugin(qChan, cfg, pluginTyp, pluginPkg, name, version),
}
return
}
// Run fetches everything from the Data channel and flushes it to stdout
func (p *Plugin) Run() {
p.Log("notice", fmt.Sprintf("Start plugin v%s", p.Version))
rewriteDims := strings.Split(p.CfgStringOr("rewrite-dimensions", ""), ",")
limitDims := strings.Split(p.CfgStringOr("limit-dimensions", ""), ",")
rwDims := map[string]string{}
for _, elem := range rewriteDims {
kv := strings.Split(elem, ":")
if len(kv) == 2 {
rwDims[kv[0]] = kv[1]
} else {
p.Log("warn", fmt.Sprintf("Could not split rwDim '%s' by ':'", elem))
}
}
ignoreContainerEvents := p.CfgBoolOr("ignore-container-events", true)
dc := p.QChan.Data.Join()
for {
select {
case val := <-dc.Read:
switch val.(type) {
case qtypes_messages.Message:
msg := val.(qtypes_messages.Message)
if msg.StopProcessing(p.Plugin, false) {
continue
}
name, nok := msg.Tags["name"]
tval, tok := msg.Tags["time"]
value, vok := msg.Tags["value"]
if nok && tok && vok {
mval, err := strconv.ParseFloat(value, 64)
if err != nil {
p.Log("error", fmt.Sprintf("Unable to parse value '%s' as float: %s", value, err.Error()))
continue
}
tint, err := strconv.Atoi(tval)
if err != nil {
p.Log("error", fmt.Sprintf("Unable to parse timestamp '%s' as int: %s", tint, err.Error()))
continue
}
dims := map[string]string{}
dims["source"] = msg.GetLastSource()
met := qtypes_metrics.NewExt(p.Name, name, qtypes_metrics.Gauge, mval, dims, time.Unix(int64(tint), 0), true)
tags, tagok := msg.Tags["tags"]
if tagok {
for _, item := range strings.Split(tags, ",") {
dim := strings.Split(item, "=")
if len(dim) == 2 {
met.Dimensions[dim[0]] = dim[1]
}
}
}
p.Log("trace", "send metric")
p.QChan.Data.Send(met)
}
case qtypes_messages.ContainerMessage:
msg := val.(qtypes_messages.ContainerMessage)
if msg.StopProcessing(p.Plugin, false) {
continue
}
name, nok := msg.Tags["name"]
tval, tok := msg.Tags["time"]
value, vok := msg.Tags["value"]
if nok && tok && vok {
mval, err := strconv.ParseFloat(value, 64)
if err != nil {
p.Log("error", fmt.Sprintf("Unable to parse value '%s' as float: %s", value, err.Error()))
continue
}
tint, err := strconv.Atoi(tval)
if err != nil {
p.Log("error", fmt.Sprintf("Unable to parse timestamp '%s' as int: %s", tint, err.Error()))
continue
}
dims := AssembleJSONDefaultDimensions(&msg.Container)
dims = AddEngineDims(dims, &msg.Engine)
dims = RewriteDims(rwDims, dims)
dims["source"] = msg.GetLastSource()
tags, tagok := msg.Tags["tags"]
if tagok {
for _, item := range strings.Split(tags, " ") {
dim := strings.Split(item, "=")
if len(dim) == 2 {
dims[dim[0]] = dim[1]
}
}
}
// TODO: Iterating yet again over the dimensions, not very efficient
p.Log("debug", fmt.Sprintf("Len(limitDims)=%d | %v", len(limitDims), limitDims))
endDims := map[string]string{}
if len(limitDims) > 0 {
for k, v := range dims {
for _, allowKey := range limitDims {
if allowKey == k {
endDims[k] = v
break
}
}
}
} else {
endDims = dims
}
met := qtypes_metrics.NewExt(p.Name, name, qtypes_metrics.Gauge, mval, endDims, time.Unix(int64(tint), 0), true)
p.Log("trace", "send metric")
p.QChan.Data.Send(met)
}
default:
if ignoreContainerEvents {
continue
}
p.Log("trace", fmt.Sprintf("Dunno how to handle type: %s", reflect.TypeOf(val)))
}
}
}
}
// AddEngineDims annotates engine information.
func AddEngineDims(dims map[string]string, eng *types.Info) map[string]string {
res := map[string]string{
"engine_name": eng.Name,
"engine_kernel": eng.KernelVersion,
"engine_address": eng.Swarm.NodeAddr,
"engine_version": eng.ServerVersion,
}
for k,v := range dims {
res[k] = v
}
return res
}
func RewriteDims(rw map[string]string, dims map[string]string) map[string]string {
res := map[string]string{}
for k,v := range dims {
rewritten := false
for oKey, nKey := range rw {
if oKey == k {
res[nKey] = v
rewritten = true
break
}
}
if ! rewritten {
res[k] = v
}
}
return res
}
// AssembleServiceSlot create {{.Service.Name}}.{{.Task.Slot}}
func AssembleJSONServiceSlot(cnt *types.ContainerJSON) string {
if tn, tnok := cnt.Config.Labels["com.docker.swarm.task.name"]; tnok {
arr := strings.Split(tn, ".")
if len(arr) != 3 {
return "<nil>"
}
return fmt.Sprintf("%s.%s", arr[0], arr[1])
}
return "<nil>"
}
// AssembleServiceSlot create {{.Service.Name}}.{{.Task.Slot}}
func AssembleJSONTaskSlot(cnt *types.ContainerJSON) string {
if tn, tnok := cnt.Config.Labels["com.docker.swarm.task.name"]; tnok {
arr := strings.Split(tn, ".")
if len(arr) != 3 {
return "<nil>"
}
return arr[1]
}
return "<nil>"
}
// AssembleDefaultDimensions derives a set of dimensions from the container information.
func AssembleJSONDefaultDimensions(cnt *types.ContainerJSON) map[string]string {
dims := map[string]string{
"service_namespace": cnt.Config.Labels["com.docker.stack.namespace"],
"container_id": cnt.ID,
"container_name": strings.Trim(cnt.Name, "/"),
"image_name": cnt.Image,
"service_slot": AssembleJSONServiceSlot(cnt),
"task_slot": AssembleJSONTaskSlot(cnt),
"command": strings.Replace(strings.Join(cnt.Config.Cmd, "#"), " ", "#", -1),
"created": cnt.Created,
}
//TODO: When exensivly using labels, this hurts
/*for k, v := range cnt.Config.Labels {
dv := strings.Replace(v, " ", "#", -1)
dv = strings.Replace(v, ".", "_", -1)
dims[k] = dv
}*/
return dims
}
/*func (p *Plugin) handleContainerEvent(ce qtypes_docker_events.ContainerEvent) {
if strings.HasPrefix(ce.Event.Action, "exec_") {
p.MsgCount["execEvent"]++
return
}
action := ce.Event.Action
if strings.HasPrefix(ce.Event.Action, "health_status") {
action = strings.Split(ce.Event.Action, ":")[1]
}
action = strings.Trim(action, " ")
dims := qtypes.AssembleJSONDefaultDimensions(&ce.Container)
dims["state-type"] = "container"
p.Log("info", fmt.Sprintf("Action '%s' by %v", action, dims))
if mval, ok := containerStates[action]; ok {
met := qtypes.NewExt(p.Name, "state", qtypes.Gauge, mval, dims, ce.Time, false)
p.QChan.Data.Send(met)
} else {
p.Log("warn", fmt.Sprintf("Could not fetch '%s' from containerState", action))
met := qtypes.NewExt(p.Name, "state", qtypes.Gauge, 0.0, dims, ce.Time, false)
p.QChan.Data.Send(met)
}
}
*/