Skip to content

Commit

Permalink
Plugin API: Additional comments, logging, handling no namespace (#366)
Browse files Browse the repository at this point in the history
* Additional comments, logging, handling no namespace

* activation flag in plugin loader

* remove multiple close checks
  • Loading branch information
kjin authored Feb 7, 2017
1 parent c999103 commit b17394d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/trace-plugin-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,17 @@ PluginAPI.prototype.getTransaction = function() {
* @param {function(?Transaction)} fn A function that will be called exactly
* once. If the incoming request should be traced, a root span will be created,
* and this function will be called with a Transaction object exposing functions
* operating on the root span; otherwise, it will be called without any
* arguments.
* operating on the root span; otherwise, it will be called with null as an
* argument.
* @returns The return value of calling fn.
*/
PluginAPI.prototype.runInRootSpan = function(options, fn) {
var that = this;
if (!this.agent_.namespace) {
this.logger_.warn('Trace agent: CLS namespace not present; not running in' +
'root span.');
return fn(null);
}
return this.agent_.namespace.runAndReturn(function() {
var skipFrames = options.skipFrames ? options.skipFrames + 2 : 2;
var transaction = createTransaction_(that, options, skipFrames);
Expand All @@ -205,8 +210,8 @@ PluginAPI.prototype.runInRootSpan = function(options, fn) {
/**
* Convenience method which obtains a Transaction object with getTransaction()
* and calls its runInChildSpan function on the given arguments. If there is
* no current Transaction object, the provided function will be called without
* arguments.
* no current Transaction object, the provided function will be called with
* null as an argument.
* @param {object} options An object that specifies options for how the root
* span is created and propogated. @see Transaction.prototype.runInChildSpan
* @param {function(?Transaction)} fn A function that will be called exactly
Expand All @@ -223,7 +228,7 @@ PluginAPI.prototype.runInChildSpan = function(options, fn) {
} else {
this.logger_.warn(options.name + ': Attempted to run in child span ' +
'without root');
return fn();
return fn(null);
}
};

Expand All @@ -234,6 +239,10 @@ PluginAPI.prototype.runInChildSpan = function(options, fn) {
* @param {function} fn A function to which to bind the trace context.
*/
PluginAPI.prototype.wrap = function(fn) {
if (!this.agent_.namespace) {
this.logger_.warn('Trace agent: No CLS namespace to bind function to');
return fn;
}
return this.agent_.namespace.bind(fn);
};

Expand All @@ -244,6 +253,9 @@ PluginAPI.prototype.wrap = function(fn) {
* the trace context binded to them.
*/
PluginAPI.prototype.wrapEmitter = function(emitter) {
if (!this.agent_.namespace) {
this.logger_.warn('Trace agent: No CLS namespace to bind emitter to');
}
this.agent_.namespace.bindEmitter(emitter);
};

Expand Down
6 changes: 5 additions & 1 deletion src/trace-plugin-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var semver = require('semver');
var PluginAPI = require('./trace-plugin-interface.js');

var plugins = Object.create(null);
var activated = false;

var logger;

Expand Down Expand Up @@ -85,9 +86,11 @@ function checkLoadedModules() {
}

function activate(agent) {
if (logger) {
if (activated) {
logger.error('Plugins activated more than once.');
return;
}
activated = true;
logger = agent.logger;

// Create a new object exposing functions to create trace spans and propagate
Expand Down Expand Up @@ -207,6 +210,7 @@ function deactivate() {
}
}
}
activated = false;

// unhook module.load
shimmer.unwrap(Module, '_load');
Expand Down

0 comments on commit b17394d

Please sign in to comment.