Skip to content

Commit

Permalink
Warn about modules loaded before trace agent
Browse files Browse the repository at this point in the history
Add additional logging to report any modules loaded before the trace
agent even if they are not modules that we patch directly.

Fixes #187.
  • Loading branch information
Matt Loring committed Jan 11, 2016
1 parent 8c63bae commit 903cec6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

'use strict';

var filesLoadedBeforeTrace = Object.keys(require.cache);

// Load continuation-local-storage first to ensure the core async APIs get
// patched before any user-land modules get loaded.
require('continuation-local-storage');
Expand All @@ -26,6 +28,17 @@ var semver = require('semver');
var constants = require('./lib/constants.js');
var path = require('path');

var modulesLoadedBeforeTrace = [];
var moduleRegex =
new RegExp('.*?node_modules\\' + path.sep + '([^\\' + path.sep + ']*).*');
for (var i = 0; i < filesLoadedBeforeTrace.length; i++) {
var matches = moduleRegex.exec(filesLoadedBeforeTrace[i]);
if (matches && matches.length > 1 &&
modulesLoadedBeforeTrace.indexOf(matches[1]) === -1) {
modulesLoadedBeforeTrace.push(matches[1]);
}
}

/**
* Phantom implementation of the trace agent. This allows API users to decouple
* the enable/disable logic from the calls to the tracing API. The phantom API
Expand Down Expand Up @@ -121,6 +134,12 @@ var publicAgent = {
var headers = {};
headers[constants.TRACE_AGENT_REQUEST_HEADER] = 1;

if (modulesLoadedBeforeTrace.length > 0) {
logger.warn('Tracing might not work as the following modules ' +
' were loaded before the trace agent was initialized: ' +
JSON.stringify(modulesLoadedBeforeTrace));
}

if (typeof config.projectId === 'undefined') {
// Queue the work to acquire the projectNumber (potentially from the
// network.)
Expand Down Expand Up @@ -172,6 +191,6 @@ var publicAgent = {
module.exports = global._google_trace_agent = publicAgent;

// If the module was --require'd from the command line, start the agent.
if (module.parent.id === 'internal/preload') {
if (module.parent && module.parent.id === 'internal/preload') {
module.exports.start();
}
20 changes: 20 additions & 0 deletions test/fixtures/start-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

process.env.GCLOUD_TRACE_LOGLEVEL = 4;
require('glob'); // Load a module before agent
require('../..').start();
29 changes: 29 additions & 0 deletions test/standalone/test-modules-loaded-before-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var assert = require('assert');
var cp = require('child_process');

describe('modules loaded before agent', function() {
it('should log if modules were loaded before agent', function() {
var output =
cp.execSync('node test/fixtures/start-agent.js');
console.log(output.toString());
assert(output.toString().match(/Tracing might not work.*"glob".*/));
});
});

0 comments on commit 903cec6

Please sign in to comment.