| 1 |
/* |
| 2 |
* Copyright 2012 Roland Huss |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/** |
| 18 |
* Jolokia integration into cubism (http://square.github.com/cubism/) |
| 19 |
* |
| 20 |
* This integration requires the following |
| 21 |
*/ |
| 22 |
|
| 23 |
(function () { |
| 24 |
var builder = function (cubism,Jolokia) { |
| 25 |
|
| 26 |
var VERSION = "1.1.1"; |
| 27 |
|
| 28 |
var ctx_jolokia = function (url, opts) { |
| 29 |
var source = {}, |
| 30 |
context = this, |
| 31 |
j4p = createAgent(url, opts), |
| 32 |
step = 5e3; // 5 seconds by default |
| 33 |
|
| 34 |
// Connecto to start and stop events |
| 35 |
context.on("start",function() { |
| 36 |
j4p.start(); |
| 37 |
}); |
| 38 |
|
| 39 |
context.on("stop",function() { |
| 40 |
j4p.stop(); |
| 41 |
}); |
| 42 |
|
| 43 |
/** |
| 44 |
* Factory method for create a metric objects which has various variants. |
| 45 |
* |
| 46 |
* If the first argument is a Jolokia request object (i.e. not a function), this request |
| 47 |
* is used for sending requests periodically. |
| 48 |
* |
| 49 |
* If the first argument is a function, this function is used for calculating the numeric value |
| 50 |
* to be plotted. The rest of the argumens can be one or more request objects, which are registered and their |
| 51 |
* responses are put as arguments to the given callback function. |
| 52 |
* |
| 53 |
* The last argument, if an object but not a Jolokia request (i.e. there is no <code>type</code> key), is |
| 54 |
* taken as an option object with the following possible keys: |
| 55 |
* |
| 56 |
* <ul> |
| 57 |
* <li><b>name</b>: name used in charts</li> |
| 58 |
* <li><b>delta</b>: delta value in milliseconds for creating delta (velocity) charts. This is done by |
| 59 |
* taking the value measured that many milliseconds ago and subtract them from each other.</li> |
| 60 |
* <li><b>keepDelay</b>: how many seconds back the fetched values should be kept.</li> |
| 61 |
* </ul> |
| 62 |
* |
| 63 |
* Finally, if the last argument is a pure string, then this string is used as name for the chart. |
| 64 |
* |
| 65 |
* @return the metric objects which can be used in Cubism for creating charts. |
| 66 |
*/ |
| 67 |
source.metric = function () { |
| 68 |
var values = []; |
| 69 |
// If the first argument is a function, this callback function is used for calculating the |
| 70 |
// value of a data point. The remaining arguments should be one or more Jolokia requests objects |
| 71 |
// and the callback function given will be called with as many response objects after each server query. |
| 72 |
// The callback function needs to return a single calculated numerical value |
| 73 |
var name; |
| 74 |
var argsLen = arguments.length; |
| 75 |
var options = {}; |
| 76 |
|
| 77 |
// Create metric upfront so that it can be used in extraction functions. The name defaults to the mbean name |
| 78 |
// but can be given as first argument |
| 79 |
var lastIdx = arguments.length - 1; |
| 80 |
var lastArg = arguments[lastIdx]; |
| 81 |
if (typeof lastArg == "string") { |
| 82 |
name = lastArg; |
| 83 |
argsLen = lastIdx; |
| 84 |
} |
| 85 |
// Options can be given as an object (but not a request with a 'type' property) |
| 86 |
if (typeof lastArg == "object" && !lastArg.type) { |
| 87 |
options = lastArg; |
| 88 |
name = options.name; |
| 89 |
argsLen = lastIdx; |
| 90 |
} |
| 91 |
if (!name && typeof arguments[0] != "function") { |
| 92 |
name = arguments[0].mbean; |
| 93 |
} |
| 94 |
|
| 95 |
// Metric which maps our previously locally stored values to the ones requested by cubism |
| 96 |
var metric = context.metric(mapValuesFunc(values, options.keepDelay, context.width), name); |
| 97 |
if (options.delta) { |
| 98 |
// Use cubism metric chaining for calculating the difference value and keep care that the |
| 99 |
// metric keeps old values up to the delta value |
| 100 |
var prevMetric = metric.shift(-options.delta); |
| 101 |
metric = metric.subtract(prevMetric); |
| 102 |
if (name) { |
| 103 |
metric.toString = function () { |
| 104 |
return name |
| 105 |
}; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// If an extraction function is given, this can be used for fine grained manipulations of |
| 110 |
// the answer |
| 111 |
if (typeof arguments[0] == "function") { |
| 112 |
var func = arguments[0]; |
| 113 |
var respFunc = function (resp) { |
| 114 |
var isError = false; |
| 115 |
for (var i = 0; i < arguments.length; i++) { |
| 116 |
if (j4p.isError(arguments[i])) { |
| 117 |
isError = true; |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
values.unshift( |
| 122 |
{ time:Date.now(), value:isError ? NaN : func.apply(metric, arguments) } |
| 123 |
); |
| 124 |
}; |
| 125 |
var args = [ respFunc ]; |
| 126 |
for (var i = 1; i < argsLen; i++) { |
| 127 |
args.push(arguments[i]); |
| 128 |
} |
| 129 |
j4p.register.apply(j4p, args); |
| 130 |
} else { |
| 131 |
// Register the argument given directly as a Jolokia request. The request must return a single |
| 132 |
// numerical value |
| 133 |
var request = arguments[0]; |
| 134 |
j4p.register(function (resp) { |
| 135 |
values.unshift({ |
| 136 |
time: Date.now(), |
| 137 |
value:j4p.isError(resp) ? NaN : Number(resp.value) |
| 138 |
}); |
| 139 |
}, request); |
| 140 |
} |
| 141 |
|
| 142 |
return metric; |
| 143 |
}; |
| 144 |
|
| 145 |
// Start up fetching of values in the background |
| 146 |
source.start = function (newStep) { |
| 147 |
newStep = newStep || step; |
| 148 |
j4p.start(newStep); |
| 149 |
}; |
| 150 |
|
| 151 |
// Stop fetching of values in the backgorund |
| 152 |
source.stop = j4p.stop; |
| 153 |
|
| 154 |
// Check whether the scheduler is running |
| 155 |
source.isRunning = j4p.isRunning; |
| 156 |
|
| 157 |
// Startup poller which will call the agent periodically |
| 158 |
return source; |
| 159 |
|
| 160 |
// ======================================================================================= |
| 161 |
// Private helper method |
| 162 |
|
| 163 |
// Create a new Jolokia agent or reuse a given one |
| 164 |
function createAgent(url, opts) { |
| 165 |
if (url instanceof Jolokia) { |
| 166 |
return url; |
| 167 |
} else { |
| 168 |
var args; |
| 169 |
if (typeof url == "string") { |
| 170 |
args = {url:url}; |
| 171 |
if (opts) { |
| 172 |
for (var key in opts) { |
| 173 |
if (opts.hasOwnProperty(key)) { |
| 174 |
args[key] = opts[key]; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} else { |
| 179 |
args = url; |
| 180 |
} |
| 181 |
return new Jolokia(args); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// Generate function which picks the requested values from the values |
| 186 |
// stored periodically by the Jolokia poller. |
| 187 |
function mapValuesFunc(values, keepDelay, width) { |
| 188 |
return function (cStart, cStop, cStep, callback) { |
| 189 |
cStart = +cStart; |
| 190 |
cStop = +cStop; |
| 191 |
var retVals = [], |
| 192 |
cTime = cStop, |
| 193 |
vLen = values.length, |
| 194 |
vIdx = 0, |
| 195 |
vStart = vLen > 0 ? values[vLen - 1].time : undefined; |
| 196 |
|
| 197 |
if (!vLen || cStop < vStart) { |
| 198 |
// Nothing fetched yet or seeked interval doesn't overlap with stored values --> return only NaNs |
| 199 |
for (var t = cStart; t <= cStop; t += cStep) { |
| 200 |
retVals.push(NaN); |
| 201 |
} |
| 202 |
return callback(null, retVals); |
| 203 |
} |
| 204 |
|
| 205 |
// Fill up wit NaN until we reach the first stored val |
| 206 |
while (cTime > values[0].time + cStep) { |
| 207 |
retVals.unshift(NaN); |
| 208 |
cTime -= cStep; |
| 209 |
} |
| 210 |
|
| 211 |
while (cTime >= cStart && cTime >= vStart) { |
| 212 |
// Count down stored values until we find the next best 'fit' |
| 213 |
// (equals or closest before the step-calculated ime) |
| 214 |
while (values[vIdx].time > cTime) { |
| 215 |
vIdx++; |
| 216 |
} |
| 217 |
retVals.unshift(values[vIdx].value); |
| 218 |
cTime -= cStep; |
| 219 |
} |
| 220 |
|
| 221 |
// Finally prepend with 'NaN' for data not yet fetched |
| 222 |
while (cTime >= cStart) { |
| 223 |
retVals.unshift(NaN); |
| 224 |
cTime -= cStep; |
| 225 |
} |
| 226 |
|
| 227 |
// Remove older values |
| 228 |
if (vLen > width) { |
| 229 |
if (!keepDelay) { |
| 230 |
values.length = width; |
| 231 |
} else { |
| 232 |
var keepUntil = values[width].time - keepDelay, |
| 233 |
i = width; |
| 234 |
while (i < vLen && values[i].time > keepUntil) { |
| 235 |
i++; |
| 236 |
} |
| 237 |
values.length = i; |
| 238 |
} |
| 239 |
} |
| 240 |
callback(null, retVals); |
| 241 |
} |
| 242 |
} |
| 243 |
}; |
| 244 |
ctx_jolokia.VERSION = VERSION; |
| 245 |
|
| 246 |
cubism.context.prototype.jolokia = ctx_jolokia; |
| 247 |
return ctx_jolokia; |
| 248 |
}; |
| 249 |
|
| 250 |
// ===================================================================================================== |
| 251 |
// Register either at the global Jolokia object global or as an AMD module |
| 252 |
(function (root) { |
| 253 |
if (typeof define === 'function' && define.amd) { |
| 254 |
// AMD. Register as a named module |
| 255 |
define(["cubism","jolokia"],function (cubism,Jolokia) { |
| 256 |
return builder(cubism,Jolokia); |
| 257 |
}); |
| 258 |
} else { |
| 259 |
if (root.Jolokia && root.cubism) { |
| 260 |
builder(root.cubism,root.Jolokia); |
| 261 |
} else { |
| 262 |
console.error("No " + (root.cubism ? "Cubism" : "Jolokia") + " definition found. " + |
| 263 |
"Please include jolokia.js and cubism.js before jolokia-cubism.js"); |
| 264 |
} |
| 265 |
} |
| 266 |
})(this); |
| 267 |
})(); |
| 268 |
|
| 269 |
|
| 270 |
|