/**
* Submits events to the LuminAR Tracer. Disabled by default: use
* {@link Lens.Tracer.enable} to turn it on.
*
* @name Lens.Tracer
* @namespace
*/
define(["lib/jquery", "common/util/log", "common/util/id_generator"],
function($, Log, IDGenerator ) {
"use strict";
var enabled = false;
var POST_INTERVAL = 1000; // ms
var POST_URL = "http://localhost:3000/traces/submit";
var logger = Log("Tracer");
// objects with uuid, group, name, time
var queue = [];
// posts the queue to the Tracer server
var postQueue = function() {
if(!enabled || (queue.length === 0)) { return; }
$.ajax({
type: "POST",
url: POST_URL,
data: "json=" + encodeURIComponent(JSON.stringify(queue)),
error: function(xhr, status, error) {
logger.error("Tracer post failed. status:", status, "error:", error, "xhr:", xhr);
}
});
queue = [];
};
var Tracer = /** @lends Lens.Tracer */ {
/**
* Posts a tracing event to the LuminAR Tracer.
* @param {String} name Name of the event.
* @param {String} [trace] Trace ID to associate the event with. If not
* given, a trace id is randomly generated.
* @param {String} [tag] Tag name, if desired.
* @param {String} [group] Group name. Defaults to "Lens"
*
* @return {String} The Trace ID of the event.
*/
post: function(name, trace, tag, group) {
if(!enabled) { return; }
if(!group) {
group = "Lens";
}
if(!trace) {
trace = IDGenerator.traceId();
}
var time = Math.round((performance.timing.navigationStart + performance.now()) * 1000);
var obj = {
uuid: trace,
group: group,
name: name,
time: time
};
if(tag) {
obj.tag = tag;
}
queue.push(obj);
return trace;
},
/**
* Enables tracing.
*/
enable: function() {
enabled = true;
},
/**
* Disables tracing.
*/
disable: function() {
enabled = false;
}
};
setInterval(postQueue, POST_INTERVAL);
Lens.Tracer = Tracer;
return Tracer;
});