From 7a61849f54a429eca15a4f809b7b6c7292cbc9b3 Mon Sep 17 00:00:00 2001 From: Matt Loring Date: Wed, 13 Jan 2016 22:13:00 -0800 Subject: [PATCH] Handle remaining todos in code --- lib/hooks/index.js | 4 +--- lib/trace-agent.js | 9 ++++++++- lib/trace-writer.js | 15 +++++++++++---- package.json | 1 + 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/hooks/index.js b/lib/hooks/index.js index 415713e80..d81b9d818 100644 --- a/lib/hooks/index.js +++ b/lib/hooks/index.js @@ -70,9 +70,7 @@ function findModulePath(request, parent) { var paths = resolvedModule[1]; for (var i = 0, PL = paths.length; i < PL; i++) { if (mainScriptDir.indexOf(paths[i]) === 0) { - // This is unsafe on windows if request contains a namespace - // TODO: convert path separators in request - return path.join(paths[i], request); + return path.join(paths[i], request.replace('/', path.sep)); } } return null; diff --git a/lib/trace-agent.js b/lib/trace-agent.js index 8f8ca513e..d70c56e61 100644 --- a/lib/trace-agent.js +++ b/lib/trace-agent.js @@ -24,6 +24,7 @@ var TraceWriter = require('./trace-writer.js'); var uuid = require('uuid'); var constants = require('./constants.js'); var tracingPolicy = require('./tracing-policy.js'); +var isEqual = require('lodash.isequal'); /** @type {TraceAgent} */ var traceAgent; @@ -51,6 +52,7 @@ function TraceAgent(config, logger) { TraceAgent.prototype.stop = function() { hooks.deactivate(); cls.destroyNamespace(); + this.traceWriter.stop(); this.namespace = null; traceAgent = null; this.logger.info('trace agent deactivated'); @@ -234,7 +236,12 @@ TraceAgent.prototype.addContextToHeaders = function(spanData, headers) { module.exports = { get: function(config, logger) { if (traceAgent) { - // TODO: log error if config object is different from traceAgent.config + if (!isEqual(config, traceAgent.config_)) { + traceAgent.logger.warn('New configuration does not match configuration' + + 'of existing agent. The old configuration will be used.\nNew: ' + + JSON.stringify(config) + '\nExisting: ' + + JSON.stringify(traceAgent.config_)); + } return traceAgent; } traceAgent = new TraceAgent(config, logger); diff --git a/lib/trace-writer.js b/lib/trace-writer.js index 87aa7601a..95bdd0506 100644 --- a/lib/trace-writer.js +++ b/lib/trace-writer.js @@ -48,10 +48,11 @@ function TraceWriter(logger, config) { /** @private {Object} default labels to be attached to written spans */ this.defaultLabels_ = {}; + /** @private {Boolean} whether the trace writer is active */ + this.isActive = true; + // Schedule periodic flushing of the buffer, but only if we are able to get // the project number (potentially from the network.) - // TODO(ofrobots): if the agent gets stopped, we need to stop flush interval - // too. var that = this; that.getProjectNumber(function(err, project) { if (err) { return; } // ignore as index.js takes care of this. @@ -88,6 +89,10 @@ function TraceWriter(logger, config) { }); } +TraceWriter.prototype.stop = function() { + this.isActive = false; +}; + TraceWriter.prototype.getHostname = function(cb) { var that = this; utils.getHostname(headers, function(err, hostname) { @@ -169,8 +174,10 @@ TraceWriter.prototype.scheduleFlush_ = function(project) { this.flushBuffer_(project); // Do it again after delay - setTimeout(this.scheduleFlush_.bind(this, project), - this.config_.flushDelaySeconds * 1000).unref(); + if (this.isActive) { + setTimeout(this.scheduleFlush_.bind(this, project), + this.config_.flushDelaySeconds * 1000).unref(); + } }; /** diff --git a/package.json b/package.json index 58e7e63bc..821e5701d 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "dependencies": { "@google/cloud-diagnostics-common": "^0.2.1", "continuation-local-storage": "^3.1.4", + "lodash.isequal": "^4.0.0", "methods": "^1.1.1", "semver": "^5.0.1", "shimmer": "^1.0.0",