| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
var htrace = htrace || {}; |
| 21 |
|
| 22 |
// The invalid span ID, which is all zeroes. |
| 23 |
htrace.INVALID_SPAN_ID = "00000000000000000000000000000000"; |
| 24 |
|
| 25 |
// Convert an array of htrace.Span models into a comma-separated string. |
| 26 |
htrace.spanModelsToString = function(spans) { |
| 27 |
var ret = ""; |
| 28 |
var prefix = ""; |
| 29 |
for (var i = 0; i < spans.length; i++) { |
| 30 |
ret += prefix + JSON.stringify(spans[i].unparse()); |
| 31 |
prefix = ", "; |
| 32 |
} |
| 33 |
return ret; |
| 34 |
}; |
| 35 |
|
| 36 |
// Convert an array of return results from ajax calls into an array of |
| 37 |
// htrace.Span models. |
| 38 |
htrace.parseMultiSpanAjaxQueryResults = function(ajaxCalls) { |
| 39 |
var parsedSpans = []; |
| 40 |
for (var i = 0; i < ajaxCalls.length; i++) { |
| 41 |
var text = ajaxCalls[i][0]; |
| 42 |
var result = ajaxCalls[i][1]; |
| 43 |
if (ajaxCalls[i]["status"] != "200") { |
| 44 |
throw "ajax error: " + ajaxCalls[i].statusText; |
| 45 |
} |
| 46 |
var parsedSpan = new htrace.Span({}); |
| 47 |
try { |
| 48 |
parsedSpan.parse(ajaxCalls[i].responseJSON, {}); |
| 49 |
} catch (e) { |
| 50 |
throw "span parse error: " + e; |
| 51 |
} |
| 52 |
parsedSpans.push(parsedSpan); |
| 53 |
} |
| 54 |
return parsedSpans; |
| 55 |
}; |
| 56 |
|
| 57 |
htrace.sortSpansByBeginTime = function(spans) { |
| 58 |
return spans.sort(function(a, b) { |
| 59 |
if (a.get("begin") < b.get("begin")) { |
| 60 |
return -1; |
| 61 |
} else if (a.get("begin") > b.get("begin")) { |
| 62 |
return 1; |
| 63 |
} else { |
| 64 |
return 0; |
| 65 |
} |
| 66 |
}); |
| 67 |
}; |
| 68 |
|
| 69 |
htrace.getReifiedParents = function(span) { |
| 70 |
return span.get("reifiedParents") || []; |
| 71 |
}; |
| 72 |
|
| 73 |
htrace.getReifiedChildren = function(span) { |
| 74 |
return span.get("reifiedChildren") || []; |
| 75 |
}; |
| 76 |
|
| 77 |
htrace.Span = Backbone.Model.extend({ |
| 78 |
// Parse a span sent from htraced. |
| 79 |
// We use more verbose names for some attributes. |
| 80 |
// Missing attributes are treated as zero or empty. Numerical attributes are |
| 81 |
// forced to be numbers. |
| 82 |
parse: function(response, options) { |
| 83 |
var span = {}; |
| 84 |
this.set("spanId", response.a ? response.a : htrace.INVALID_SPAN_ID); |
| 85 |
this.set("tracerId", response.r ? response.r : ""); |
| 86 |
this.set("parents", response.p ? response.p : []); |
| 87 |
this.set("description", response.d ? response.d : ""); |
| 88 |
this.set("begin", response.b ? parseInt(response.b, 10) : 0); |
| 89 |
this.set("end", response.e ? parseInt(response.e, 10) : 0); |
| 90 |
if (response.t) { |
| 91 |
var t = response.t.sort(function(a, b) { |
| 92 |
if (a.t < b.t) { |
| 93 |
return -1; |
| 94 |
} else if (a.t > b.t) { |
| 95 |
return 1; |
| 96 |
} else { |
| 97 |
return 0; |
| 98 |
} |
| 99 |
}); |
| 100 |
this.set("timeAnnotations", t); |
| 101 |
} else { |
| 102 |
this.set("timeAnnotations", []); |
| 103 |
} |
| 104 |
this.set("infoAnnotations", response.n ? response.n : {}); |
| 105 |
this.set("selected", false); |
| 106 |
|
| 107 |
// reifiedChildren starts off as null and will be filled in as needed. |
| 108 |
this.set("reifiedChildren", null); |
| 109 |
|
| 110 |
// If there are parents, reifiedParents starts off as null. Otherwise, we |
| 111 |
// know it is the empty array. |
| 112 |
this.set("reifiedParents", (this.get("parents").length == 0) ? [] : null); |
| 113 |
|
| 114 |
return span; |
| 115 |
}, |
| 116 |
|
| 117 |
// Transform a span model back into a JSON string suitable for sending over |
| 118 |
// the wire. |
| 119 |
unparse: function() { |
| 120 |
var obj = { }; |
| 121 |
if (!(this.get("spanId") === htrace.INVALID_SPAN_ID)) { |
| 122 |
obj.a = this.get("spanId"); |
| 123 |
} |
| 124 |
if (!(this.get("tracerId") === "")) { |
| 125 |
obj.r = this.get("tracerId"); |
| 126 |
} |
| 127 |
if (this.get("parents").length > 0) { |
| 128 |
obj.p = this.get("parents"); |
| 129 |
} |
| 130 |
if (this.get("description").length > 0) { |
| 131 |
obj.d = this.get("description"); |
| 132 |
} |
| 133 |
if (this.get("begin") > 0) { |
| 134 |
obj.b = this.get("begin"); |
| 135 |
} |
| 136 |
if (this.get("end") > 0) { |
| 137 |
obj.e = this.get("end"); |
| 138 |
} |
| 139 |
if (this.get("timeAnnotations").length > 0) { |
| 140 |
obj.t = this.get("timeAnnotations"); |
| 141 |
} |
| 142 |
if (_.size(this.get("infoAnnotations")) > 0) { |
| 143 |
obj.n = this.get("infoAnnotations"); |
| 144 |
} |
| 145 |
return obj; |
| 146 |
}, |
| 147 |
|
| 148 |
reifyParentsRecursive: function(depth) { |
| 149 |
var rootDeferred = jQuery.Deferred(); |
| 150 |
var span = this; |
| 151 |
span.reifyParents().done(function(err) { |
| 152 |
if ((err != "") || (depth <= 0)) { |
| 153 |
rootDeferred.resolve(err); |
| 154 |
return; |
| 155 |
} |
| 156 |
var recursivePromises = []; |
| 157 |
var reifiedParents = span.get("reifiedParents"); |
| 158 |
for (var j = 0; j < reifiedParents.length; j++) { |
| 159 |
recursivePromises.push(reifiedParents[j].reifyParentsRecursive(depth - 1)); |
| 160 |
} |
| 161 |
$.when.apply($, recursivePromises).then(function() { |
| 162 |
for (var i = 0; i < arguments.length; i++) { |
| 163 |
if (arguments[i] != "") { |
| 164 |
rootDeferred.resolve(arguments[i]); |
| 165 |
return; |
| 166 |
} |
| 167 |
} |
| 168 |
rootDeferred.resolve(""); |
| 169 |
}); |
| 170 |
}); |
| 171 |
return rootDeferred.promise(); |
| 172 |
}, |
| 173 |
|
| 174 |
// |
| 175 |
// Although the parent IDs are always present in the 'parents' field of the |
| 176 |
// span, sometimes we need the actual parent span models. In that case we |
| 177 |
// must "reify" them (make them real). |
| 178 |
// |
| 179 |
// This functionReturns a jquery promise which reifies all the parents of this |
| 180 |
// span and stores them into reifiedParents. The promise returns the empty |
| 181 |
// string on success, or an error string on failure. |
| 182 |
// |
| 183 |
reifyParents: function() { |
| 184 |
var span = this; |
| 185 |
var numParents = span.get("parents").length; |
| 186 |
var ajaxCalls = []; |
| 187 |
// Set up AJAX queries to reify the parents. |
| 188 |
for (var i = 0; i < numParents; i++) { |
| 189 |
ajaxCalls.push($.ajax({ |
| 190 |
url: "span/" + span.get("parents")[i], |
| 191 |
data: {}, |
| 192 |
contentType: "application/json; charset=utf-8", |
| 193 |
dataType: "json" |
| 194 |
})); |
| 195 |
} |
| 196 |
var rootDeferred = jQuery.Deferred(); |
| 197 |
$.when.apply($, ajaxCalls).then(function() { |
| 198 |
var reifiedParents = []; |
| 199 |
try { |
| 200 |
reifiedParents = htrace.parseMultiSpanAjaxQueryResults(ajaxCalls); |
| 201 |
} catch (e) { |
| 202 |
rootDeferred.resolve("Error reifying parents for " + |
| 203 |
span.get("spanId") + ": " + e); |
| 204 |
return; |
| 205 |
} |
| 206 |
reifiedParents = htrace.sortSpansByBeginTime(reifiedParents); |
| 207 |
// The current span is a child of the reified parents. There may be other |
| 208 |
// children of those parents, but we are ignoring that here. By making |
| 209 |
// this non-null, the "expand children" button will not appear for these |
| 210 |
// paren spans. |
| 211 |
for (var j = 0; j < reifiedParents.length; j++) { |
| 212 |
reifiedParents[j].set("reifiedChildren", [span]); |
| 213 |
} |
| 214 |
console.log("Setting reified parents for " + span.get("spanId") + |
| 215 |
" to " + htrace.spanModelsToString (reifiedParents)); |
| 216 |
span.set("reifiedParents", reifiedParents); |
| 217 |
rootDeferred.resolve(""); |
| 218 |
}); |
| 219 |
return rootDeferred.promise(); |
| 220 |
}, |
| 221 |
|
| 222 |
reifyChildrenRecursive: function(depth, lim) { |
| 223 |
var rootDeferred = jQuery.Deferred(); |
| 224 |
var span = this; |
| 225 |
span.reifyChildren(lim).done(function(err) { |
| 226 |
var reifiedChildren = span.get("reifiedChildren"); |
| 227 |
lim = lim - reifiedChildren.length; |
| 228 |
if ((err != "") || (depth <= 0) || (lim <= 0)) { |
| 229 |
rootDeferred.resolve(err); |
| 230 |
return; |
| 231 |
} |
| 232 |
var recursivePromises = []; |
| 233 |
for (var j = 0; j < reifiedChildren.length; j++) { |
| 234 |
recursivePromises.push(reifiedChildren[j].reifyChildrenRecursive(depth - 1, lim)); |
| 235 |
} |
| 236 |
$.when.apply($, recursivePromises).then(function() { |
| 237 |
for (var i = 0; i < arguments.length; i++) { |
| 238 |
if (arguments[i] != "") { |
| 239 |
rootDeferred.resolve(arguments[i]); |
| 240 |
return; |
| 241 |
} |
| 242 |
} |
| 243 |
rootDeferred.resolve(""); |
| 244 |
}); |
| 245 |
}); |
| 246 |
return rootDeferred.promise(); |
| 247 |
}, |
| 248 |
|
| 249 |
// |
| 250 |
// The span itself does not contain its children. However, the server has an |
| 251 |
// index which can be used to easily find the children of a particular span. |
| 252 |
// |
| 253 |
// This function returns a jquery promise which reifies all the children of |
| 254 |
// this span and stores them into reifiedChildren. The promise returns the |
| 255 |
// empty string on success, or an error string on failure. |
| 256 |
// |
| 257 |
reifyChildren: function(lim) { |
| 258 |
var rootDeferred = jQuery.Deferred(); |
| 259 |
var span = this; |
| 260 |
$.ajax({ |
| 261 |
url: "span/" + span.get("spanId") + "/children?lim=" + lim.toString(), |
| 262 |
data: {}, |
| 263 |
contentType: "application/json; charset=utf-8", |
| 264 |
dataType: "json" |
| 265 |
}).done(function(childIds) { |
| 266 |
var ajaxCalls = []; |
| 267 |
for (var i = 0; i < childIds.length; i++) { |
| 268 |
ajaxCalls.push($.ajax({ |
| 269 |
url: "span/" + childIds[i], |
| 270 |
data: {}, |
| 271 |
contentType: "application/json; charset=utf-8", |
| 272 |
dataType: "json" |
| 273 |
})); |
| 274 |
}; |
| 275 |
$.when.apply($, ajaxCalls).then(function() { |
| 276 |
var reifiedChildren; |
| 277 |
try { |
| 278 |
reifiedChildren = htrace.parseMultiSpanAjaxQueryResults(ajaxCalls); |
| 279 |
} catch (e) { |
| 280 |
reifiedChildren = rootDeferred.resolve("Error reifying children " + |
| 281 |
"for " + span.get("spanId") + ": " + e); |
| 282 |
return; |
| 283 |
} |
| 284 |
reifiedChildren = htrace.sortSpansByBeginTime(reifiedChildren); |
| 285 |
// The current span is a parent of the new child. |
| 286 |
// There may be other parents, but we are ignoring that here. |
| 287 |
// By making this non-null, the "expand parents" button will not |
| 288 |
// appear for these child spans. |
| 289 |
for (var j = 0; j < reifiedChildren.length; j++) { |
| 290 |
reifiedChildren[j].set("reifiedParents", [span]); |
| 291 |
} |
| 292 |
//console.log("Setting reified children for " + span.get("spanId") + |
| 293 |
// " to " + htrace.spanModelsToString (reifiedChildren)); |
| 294 |
span.set("reifiedChildren", reifiedChildren); |
| 295 |
rootDeferred.resolve(""); |
| 296 |
}); |
| 297 |
}).fail(function(statusData) { |
| 298 |
// Check if the /children query failed. |
| 299 |
rootDeferred.resolve("Error querying children of " + |
| 300 |
span.get("spanId") + ": got " + statusData); |
| 301 |
return; |
| 302 |
}); |
| 303 |
return rootDeferred.promise(); |
| 304 |
}, |
| 305 |
|
| 306 |
// Get the earliest begin time of this span or any of its reified parents or |
| 307 |
// children. |
| 308 |
getEarliestBegin: function() { |
| 309 |
var earliestBegin = this.get("begin"); |
| 310 |
htrace.treeTraverseDepthFirstPre(this, htrace.getReifiedParents, 0, |
| 311 |
function(span, depth) { |
| 312 |
earliestBegin = Math.min(earliestBegin, span.get("begin")); |
| 313 |
}); |
| 314 |
htrace.treeTraverseDepthFirstPre(this, htrace.getReifiedChildren, 0, |
| 315 |
function(span, depth) { |
| 316 |
earliestBegin = Math.min(earliestBegin, span.get("begin")); |
| 317 |
}); |
| 318 |
return earliestBegin; |
| 319 |
}, |
| 320 |
|
| 321 |
// Get the earliest begin time of this span or any of its reified parents or |
| 322 |
// children. |
| 323 |
getLatestEnd: function() { |
| 324 |
var latestEnd = this.get("end"); |
| 325 |
htrace.treeTraverseDepthFirstPre(this, htrace.getReifiedParents, 0, |
| 326 |
function(span, depth) { |
| 327 |
latestEnd = Math.max(latestEnd, span.get("end")); |
| 328 |
}); |
| 329 |
htrace.treeTraverseDepthFirstPre(this, htrace.getReifiedChildren, 0, |
| 330 |
function(span, depth) { |
| 331 |
latestEnd = Math.max(latestEnd, span.get("end")); |
| 332 |
}); |
| 333 |
return latestEnd; |
| 334 |
}, |
| 335 |
}); |