Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opentracing integration #860

Merged
merged 7 commits into from
Jun 2, 2020
6 changes: 5 additions & 1 deletion src/browser/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ function Rollbar(options, client) {
this.options = _.handleOptions(defaultOptions, options);
this.options._configuredOptions = options;
var api = new API(this.options, transport, urllib);
this.client = client || new Client(this.options, api, logger, 'browser');

// remove tracer object from options so we don't try to parse it
var tracer = options.tracer || null;
delete options.tracer;
austenLacy marked this conversation as resolved.
Show resolved Hide resolved
this.client = client || new Client(this.options, api, logger, 'browser', tracer);

var gWindow = _gWindow();
var gDocument = (typeof document != 'undefined') && document;
Expand Down
7 changes: 6 additions & 1 deletion src/react-native/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ function Rollbar(options, client) {
delete this.options.maxItems;
this.options.environment = this.options.environment || 'unspecified';
var api = new API(this.options, transport, urllib);
this.client = client || new Client(this.options, api, logger, 'react-native');

// remove tracer object from options so we don't try to parse it
var tracer = options.tracer || null;
delete options.tracer;
austenLacy marked this conversation as resolved.
Show resolved Hide resolved
this.client = client || new Client(this.options, api, logger, 'react-native', tracer);
austenLacy marked this conversation as resolved.
Show resolved Hide resolved

addTransformsToNotifier(this.client.notifier);
addPredicatesToQueue(this.client.queue);
}
Expand Down
117 changes: 97 additions & 20 deletions src/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var _ = require('./utility');
* @param api
* @param logger
*/
function Rollbar(options, api, logger, platform) {
function Rollbar(options, api, logger, platform, tracer) {
this.options = _.merge(options);
this.logger = logger;
Rollbar.rateLimiter.configureGlobal(this.options);
Expand All @@ -23,6 +23,13 @@ function Rollbar(options, api, logger, platform) {
setStackTraceLimit(options);
this.lastError = null;
this.lastErrorHash = 'none';

if (validateTracer(tracer)) {
this.tracer = tracer;
} else {
this.tracer = null;
}

}

var defaultOptions = {
Expand All @@ -32,81 +39,86 @@ var defaultOptions = {

Rollbar.rateLimiter = new RateLimiter(defaultOptions);

Rollbar.prototype.global = function(options) {
Rollbar.prototype.global = function (options) {
Rollbar.rateLimiter.configureGlobal(options);
return this;
};

Rollbar.prototype.configure = function(options, payloadData) {
Rollbar.prototype.configure = function (options, payloadData) {
var oldOptions = this.options;
var payload = {};
if (payloadData) {
payload = {payload: payloadData};
payload = { payload: payloadData };
}
this.options = _.merge(oldOptions, options, payload);
this.notifier && this.notifier.configure(this.options);
this.telemeter && this.telemeter.configure(this.options);
setStackTraceLimit(options);
this.global(this.options);

if (validateTracer(options.tracer)) {
this.tracer = options.tracer
}

return this;
};

Rollbar.prototype.log = function(item) {
Rollbar.prototype.log = function (item) {
var level = this._defaultLogLevel();
return this._log(level, item);
};

Rollbar.prototype.debug = function(item) {
Rollbar.prototype.debug = function (item) {
this._log('debug', item);
};

Rollbar.prototype.info = function(item) {
Rollbar.prototype.info = function (item) {
this._log('info', item);
};

Rollbar.prototype.warn = function(item) {
Rollbar.prototype.warn = function (item) {
this._log('warning', item);
};

Rollbar.prototype.warning = function(item) {
Rollbar.prototype.warning = function (item) {
this._log('warning', item);
};

Rollbar.prototype.error = function(item) {
Rollbar.prototype.error = function (item) {
this._log('error', item);
};

Rollbar.prototype.critical = function(item) {
Rollbar.prototype.critical = function (item) {
this._log('critical', item);
};

Rollbar.prototype.wait = function(callback) {
Rollbar.prototype.wait = function (callback) {
this.queue.wait(callback);
};

Rollbar.prototype.captureEvent = function(type, metadata, level) {
Rollbar.prototype.captureEvent = function (type, metadata, level) {
return this.telemeter.captureEvent(type, metadata, level);
};

Rollbar.prototype.captureDomContentLoaded = function(ts) {
Rollbar.prototype.captureDomContentLoaded = function (ts) {
return this.telemeter.captureDomContentLoaded(ts);
};

Rollbar.prototype.captureLoad = function(ts) {
Rollbar.prototype.captureLoad = function (ts) {
return this.telemeter.captureLoad(ts);
};

Rollbar.prototype.buildJsonPayload = function(item) {
Rollbar.prototype.buildJsonPayload = function (item) {
return this.api.buildJsonPayload(item);
};

Rollbar.prototype.sendJsonPayload = function(jsonPayload) {
Rollbar.prototype.sendJsonPayload = function (jsonPayload) {
this.api.postJsonPayload(jsonPayload);
};

/* Internal */

Rollbar.prototype._log = function(defaultLevel, item) {
Rollbar.prototype._log = function (defaultLevel, item) {
var callback;
if (item.callback) {
callback = item.callback;
Expand All @@ -121,6 +133,7 @@ Rollbar.prototype._log = function(defaultLevel, item) {
return;
}
try {
this._addTracingInfo(item);
item.level = item.level || defaultLevel;
this.telemeter._captureRollbarItem(item);
item.telemetryEvents = this.telemeter.copyEvents();
Expand All @@ -130,11 +143,11 @@ Rollbar.prototype._log = function(defaultLevel, item) {
}
};

Rollbar.prototype._defaultLogLevel = function() {
Rollbar.prototype._defaultLogLevel = function () {
return this.options.logLevel || 'debug';
};

Rollbar.prototype._sameAsLastError = function(item) {
Rollbar.prototype._sameAsLastError = function (item) {
if (!item._isUncaught) {
return false;
}
Expand All @@ -147,6 +160,31 @@ Rollbar.prototype._sameAsLastError = function(item) {
return false;
};

Rollbar.prototype._addTracingInfo = function (item) {
// Tracer validation occurs in the constructor
// or in the Rollbar.prototype.configure methods
if (this.tracer) {
// add rollbar occurrence uuid to span
var span = this.tracer.scope().active();
span.setTag('rollbar_uuid', item.uuid);
span.setTag('has_rollbar_error', true);

// add span ID & trace ID to occurrence
var opentracingSpanId = span.context().toSpanId();
var opentracingTraceId = span.context().toTraceId();

if (item.custom) {
item.custom.opentracing_span_id = opentracingSpanId;
item.custom.opentracing_trace_id = opentracingTraceId;
} else {
item.custom = {
opentracing_span_id: opentracingSpanId,
opentracing_trace_id: opentracingTraceId
};
}
}
}

function generateItemHash(item) {
var message = item.message || '';
var stack = (item.err || {}).stack || String(item.err);
Expand All @@ -162,4 +200,43 @@ function setStackTraceLimit(options) {
}
}

/**
* Validate the tracer object provided to the Client
* is valid for our Opentracing use case.
* @param {ls-trace-js.Tracer} tracer
*/
function validateTracer(tracer) {
if (!tracer) {
return false;
}

if (!tracer.scope || typeof tracer.scope !== 'function') {
return false;
}

const scope = tracer.scope();

if (!scope || !scope.active || typeof scope.active !== 'function') {
return false;
}

const activeSpan = scope.active();

if (!activeSpan || !activeSpan.context || typeof activeSpan.context !== 'function') {
return false;
}

const spanContext = activeSpan.context();

if (!spanContext
|| !spanContext.toSpanId
|| !spanContext.toTraceId
|| typeof spanContext.toSpanId !== 'function'
|| typeof spanContext.toTraceId !== 'function') {
return false
}

return true;
}

module.exports = Rollbar;
Loading