-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.lua
263 lines (223 loc) · 9.01 KB
/
init.lua
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
-- Copyright 2015 BMC Software, Inc.
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
local framework = require('framework')
local Plugin = framework.Plugin
local WebRequestDataSource = framework.WebRequestDataSource
local DataSourcePoller = framework.DataSourcePoller
local PollerCollection = framework.PollerCollection
local auth = framework.util.auth
local ipack = framework.util.ipack
local parseJson = framework.util.parseJson
local isHttpSuccess = framework.util.isHttpSuccess
local clone = framework.table.clone
local notEmpty = framework.string.notEmpty
--Changes done for Securing parameters.
--local params = framework.params
local json = require('json')
local env = require('env')
local params = env.get("TSP_PLUGIN_PARAMS")
if(params == nil or params == '') then
params = framework.params
else
params = json.parse(params)
end
-- End changes for Security parameters.
local CLUSTER_SUMMARY_KEY = 'cluster_summary'
local TOPOLOGY_SUMMARY_KEY = 'topology_summary'
local TOPOLOGY_DETAIL_KEY = 'topology_detail'
local function createOptions(config)
local options = {}
options.host = config.host
options.port = config.port
options.auth = auth(config.username, config.password)
options.path = '/api/v1'
options.wait_for_end = true
return options
end
local function createClusterSummaryDataSource(item)
local options = createOptions(item)
options.path = options.path .. '/cluster/summary'
options.meta = {CLUSTER_SUMMARY_KEY, item}
return WebRequestDataSource:new(options)
end
local function createTopologyDetailDataSource(item, topology_id)
local options = createOptions(item)
--Modified the window from 1 to 600. 1 was not valid value.
options.path = options.path .. ('/topology/%s?window=600'):format(topology_id)
options.meta = {TOPOLOGY_DETAIL_KEY, item}
return WebRequestDataSource:new(options)
end
local function createTopologySummaryDataSource(item)
local options = createOptions(item)
options.path = options.path .. '/topology/summary'
options.meta = {TOPOLOGY_SUMMARY_KEY, item}
local ds = WebRequestDataSource:new(options)
ds:chain(function (context, callback, data, extra)
if not isHttpSuccess(extra.status_code) then
return nil
end
local success, parsed = parseJson(data)
if not success then
return nil
end
-- First emit some metrics.
callback(data, extra)
local datasources = {}
for _, topology in ipairs(parsed.topologies) do
if not item.topologies_filter or item.topologies_filter[topology.name] or item.topologies_filter[topology.id] then
local ds_detail = createTopologyDetailDataSource(item, topology.id)
ds_detail:propagate('error', context)
table.insert(datasources, ds_detail)
end
end
return datasources
end)
return ds
end
--Function to get a single line error string as event does not support multiline strings.
--Usually the lastError will contain exception. We are replacing the \n characters with ->.
--We will only be collecting 250 characters.
local function getLastError (lastError)
if(string.len(lastError) > 250) then
lastError = string.sub(lastError, 1, 250)
end
lastError, count = string.gsub(lastError, '\n', '->')
return lastError
end
local function topologySummaryExtractor (data, item, self)
local result = {}
local metric = function (...) ipack(result, ...) end
metric('STORM_CLUSTER_TOPOLOGIES', #data.topologies, nil, item.source)
return result
end
local function clusterSummaryExtractor (data, item, self)
local result = {}
local metric = function (...) ipack(result, ...) end
metric('STORM_CLUSTER_EXECUTORS', data.executorsTotal, nil, item.source)
metric('STORM_CLUSTER_SLOTS_TOTAL', data.slotsTotal, nil, item.source)
metric('STORM_CLUSTER_SLOTS_USED', data.slotsUsed, nil, item.source)
metric('STORM_CLUSTER_TASKS_TOTAL', data.tasksTotal, nil, item.source)
metric('STORM_CLUSTER_SUPERVISORS', data.supervisors, nil, item.source)
return result
end
local function topologyDetailExtractor(topology, item, self)
local result = {}
local metric = function (...) ipack(result, ...) end
-- Topology-level metrics.
local tsrc = item.source .. '.' .. topology.name
metric('STORM_TOPOLOGY_TASKS_TOTAL', topology.tasksTotal, nil, tsrc)
metric('STORM_TOPOLOGY_WORKERS_TOTAL', topology.workersTotal, nil, tsrc)
metric('STORM_TOPOLOGY_EXECUTORS_TOTAL', topology.executorsTotal, nil, tsrc)
-- Spout-level metrics.
if item.show_spouts then
for _, spout in ipairs(topology.spouts) do
local ssrc = tsrc .. ".spout-" .. spout.spoutId
metric('STORM_SPOUT_EXECUTORS', spout.executors, nil, ssrc)
metric('STORM_SPOUT_TASKS', spout.tasks, nil, ssrc)
metric('STORM_SPOUT_EMITTED', spout.emitted, nil, ssrc)
metric('STORM_SPOUT_ACKED', spout.acked, nil, ssrc)
metric('STORM_SPOUT_FAILED', spout.failed, nil, ssrc)
metric('STORM_SPOUT_COMPLETELATENCY', tonumber(spout.completeLatency), nil, ssrc)
--Generating metrics and Event for lastError.
if(spout.lastError ~= '') then
local spoutErrorString = getLastError(spout.lastError)
self:emitEvent('error', ('Spout Error- %s'):format(spoutErrorString), item.source, ssrc)
end
local spoutErrorLapsedSecs = spout.errorLapsedSecs
if(spoutErrorLapsedSecs ~= null and ((spoutErrorLapsedSecs * 1000) < item.pollInterval)) then
metric('STORM_SPOUT_LASTERROR', 1, nil, ssrc)
else
metric('STORM_SPOUT_LASTERROR', 0, nil, ssrc)
end
end
end
-- Bolt-level metrics.
if item.show_bolts then
for _, bolt in ipairs(topology.bolts) do
local bsrc = tsrc .. ".bolt-" .. bolt.boltId
metric('STORM_BOLT_EXECUTORS', bolt.executors, nil, bsrc)
metric('STORM_BOLT_TASKS', bolt.tasks, nil, bsrc)
metric('STORM_BOLT_EMITTED', bolt.emitted, nil, bsrc)
metric('STORM_BOLT_ACKED', bolt.acked, nil, bsrc)
metric('STORM_BOLT_FAILED', bolt.failed, nil, bsrc)
metric('STORM_BOLT_CAPACITY', tonumber(bolt.capacity), nil, bsrc)
metric('STORM_BOLT_EXECUTELATENCY', tonumber(bolt.executeLatency), nil, bsrc)
metric('STORM_BOLT_PROCESSLATENCY', tonumber(bolt.processLatency), nil, bsrc)
--Generating metrics and Event for lastError.
if(bolt.lastError ~= '') then
local boltErrorString = getLastError(bolt.lastError)
self:emitEvent('error', ('Bolt Error- %s'):format(boltErrorString), item.source, bsrc)
end
local boltErrorLapsedSecs = bolt.errorLapsedSecs
if(boltErrorLapsedSecs ~= null and ((boltErrorLapsedSecs * 1000) < item.pollInterval)) then
metric('STORM_BOLT_LASTERROR', 1, nil, bsrc)
else
metric('STORM_BOLT_LASTERROR', 0, nil, bsrc)
end
end
end
return result
end
local extractors_map = {}
extractors_map[CLUSTER_SUMMARY_KEY] = clusterSummaryExtractor
extractors_map[TOPOLOGY_SUMMARY_KEY] = topologySummaryExtractor
extractors_map[TOPOLOGY_DETAIL_KEY] = topologyDetailExtractor
local function createPollers(params)
local pollers = PollerCollection:new()
for _, item in pairs(params.items) do
local topologies_map = {}
local count = 0
for i, v in ipairs(item.topologies_filter) do
if notEmpty(v) then
topologies_map[v] = true;
count = count + 1
end
end
item.topologies_filter = (count > 0) and topologies_map or nil
local ds = createClusterSummaryDataSource(item)
ds:chain(function (context, callback, data, extra)
if not isHttpSuccess(extra.status_code) then
return nil
end
--Code fix: Storm cluster metrics are not shown- Start.
local success, parsed = parseJson(data)
if not success then
return nil
end
callback(data, extra)
--Code fix - End.
return { createTopologySummaryDataSource(item) }
end)
local poller = DataSourcePoller:new(item.pollInterval, ds)
pollers:add(poller)
end
return pollers
end
local pollers = createPollers(params)
local plugin = Plugin:new(params, pollers)
function plugin:onParseValues(data, extra)
if not isHttpSuccess(extra.status_code) then
self:emitEvent('error', ('Http request returned status code %s instead of OK. Please verify configuration.'):format(extra.status_code))
return
end
local success, parsed = parseJson(data)
if not success then
self:emitEvent('error', 'Cannot parse metrics. Please verify configuration.')
return
end
local key, item = unpack(extra.info)
local extractor = extractors_map[key]
return extractor(parsed, item, self)
end
plugin:run()