Skip to content

Commit

Permalink
Consolidate usage of agent.private_() (#406)
Browse files Browse the repository at this point in the history
With this PR, `agent.private_()` is only used directly in `common.js`.  Thus, tests only use functionality provided through `agent.private_()` through functions exposed through `common.js`.

PR-URL: #406
  • Loading branch information
DominicKramer authored Feb 22, 2017
1 parent 60ef26a commit 6f05b15
Show file tree
Hide file tree
Showing 37 changed files with 205 additions and 142 deletions.
68 changes: 56 additions & 12 deletions test/hooks/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@ var SERVER_KEY = fs.readFileSync(path.join(__dirname, 'fixtures', 'key.pem'));
var SERVER_CERT = fs.readFileSync(path.join(__dirname, 'fixtures', 'cert.pem'));

function init(agent) {
agent._shouldTraceArgs = [];
var shouldTrace = agent.shouldTrace;
agent.shouldTrace = function() {
agent._shouldTraceArgs.push([].slice.call(arguments, 0));
var privateAgent = agent.private_();
privateAgent._shouldTraceArgs = [];
var shouldTrace = privateAgent.shouldTrace;
privateAgent.shouldTrace = function() {
privateAgent._shouldTraceArgs.push([].slice.call(arguments, 0));
return shouldTrace.apply(this, arguments);
};
}

function replaceDebugLogger(agent, fn) {
var privateAgent = agent.private_();
var oldDebug = privateAgent.logger.debug;
privateAgent.logger.debug = fn;
return oldDebug;
}

/**
* Cleans the tracer state between test runs.
*/
Expand All @@ -55,8 +63,9 @@ function cleanTraces(agent) {
'Received: ' + arguments.length);
}

agent.traceWriter.buffer_ = [];
agent._shouldTraceArgs = [];
var privateAgent = agent.private_();
privateAgent.traceWriter.buffer_ = [];
privateAgent._shouldTraceArgs = [];
}

function getTraces(agent) {
Expand All @@ -65,7 +74,7 @@ function getTraces(agent) {
'Received: ' + arguments.length);
}

return agent.traceWriter.buffer_.map(JSON.parse);
return agent.private_().traceWriter.buffer_.map(JSON.parse);
}

function getShouldTraceArgs(agent) {
Expand All @@ -74,7 +83,7 @@ function getShouldTraceArgs(agent) {
'Received: ' + arguments.length);
}

return agent._shouldTraceArgs;
return agent.private_()._shouldTraceArgs;
}

function getMatchingSpan(agent, predicate) {
Expand Down Expand Up @@ -168,7 +177,7 @@ function runInTransaction(agent, fn) {
}

cls.getNamespace().run(function() {
var spanData = agent.createRootSpanData('outer');
var spanData = agent.private_().createRootSpanData('outer');
fn(function() {
spanData.close();
});
Expand All @@ -185,19 +194,47 @@ function createChildSpan(agent, cb, duration) {
'Received: ' + arguments.length);
}

var span = agent.startSpan('inner');
var privateAgent = agent.private_();
var span = privateAgent.startSpan('inner');
var t = setTimeout(function() {
agent.endSpan(span);
privateAgent.endSpan(span);
if (cb) {
cb();
}
}, duration);
return function() {
agent.endSpan(span);
privateAgent.endSpan(span);
clearTimeout(t);
};
}

function createRootSpanData(agent, name, traceId, parentId, skipFrames,
spanKind) {
return agent.private_().createRootSpanData(name, traceId, parentId,
skipFrames, spanKind);
}

function getTraceWriter(agent) {
return agent.private_().traceWriter;
}

function getConfig(agent) {
return agent.private_().config();
}

function namespaceRun(agent, fn) {
return agent.private_().namespace.run(fn);
}

function stopAgent(agent) {
agent.private_().stop();
}

function clearNamespace(agent) {
cls.destroyNamespace();
agent.private_().namespace = cls.createNamespace();
}

module.exports = {
init: init,
assertSpanDurationCorrect: assertSpanDurationCorrect,
Expand All @@ -210,6 +247,13 @@ module.exports = {
getTraces: getTraces,
runInTransaction: runInTransaction,
getShouldTraceArgs: getShouldTraceArgs,
replaceDebugLogger: replaceDebugLogger,
createRootSpanData: createRootSpanData,
clearNamespace: clearNamespace,
getConfig: getConfig,
getTraceWriter: getTraceWriter,
namespaceRun: namespaceRun,
stopAgent: stopAgent,
serverWait: SERVER_WAIT,
serverRes: SERVER_RES,
serverPort: SERVER_PORT,
Expand Down
12 changes: 6 additions & 6 deletions test/hooks/test-hooks-interop-mongo-express.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ describe('mongodb + express', function() {
var oldDebug;
var mongoose;
before(function() {
agent = require('../..').start().get().private_();
agent = require('../..').start();
mongoose = require('./fixtures/mongoose4');
oldDebug = agent.logger.debug;
agent.logger.debug = function(error) {
assert(error.indexOf('mongo') === -1, error);
};
oldDebug = common.replaceDebugLogger(agent,
function(error) {
assert(error.indexOf('mongo') === -1, error);
});
});

after(function() {
Expand All @@ -70,7 +70,7 @@ describe('mongodb + express', function() {
http.get({port: common.serverPort}, function(res) {
server.close();
common.cleanTraces(agent);
agent.logger.debug = oldDebug;
common.replaceDebugLogger(agent, oldDebug);
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('test-trace-connect', function() {
var agent;
var connect;
before(function() {
agent = require('../..').start({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 });
connect = require('./fixtures/connect3');
// Mute stderr to satiate appveyor
write = process.stderr.write;
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-express.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('test-trace-express', function() {
var agent;
var express;
before(function() {
agent = require('../..').start({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 });
express = require('./fixtures/express4');

// Mute stderr to satiate appveyor
Expand Down
6 changes: 4 additions & 2 deletions test/hooks/test-trace-grpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@ Object.keys(versions).forEach(function(version) {
describe(version, function() {
before(function() {
// It is necessary for the samplingRate to be 0 for the tests to succeed
agent = require('../..').start({ samplingRate: 0 }).private_();
agent.config_.enhancedDatabaseReporting = true;
agent = require('../..').start({
samplingRate: 0,
enhancedDatabaseReporting: true
});

common = require('./common.js');
common.init(agent);
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('hapi', function() {
var agent;

before(function() {
agent = require('../..').start({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 });
});

Object.keys(versions).forEach(function(version) {
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var common = require('./common.js');
var constants = require('../../src/constants.js');
var TraceLabels = require('../../src/trace-labels.js');

var agent = require('../..').start({ samplingRate: 0 }).private_();
var agent = require('../..').start({ samplingRate: 0 });

var assert = require('assert');

Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('mongodb', function() {
samplingRate: 0,
enhancedDatabaseReporting: true,
databaseResultReportingSize: RESULT_SIZE
}).private_();
});
});

Object.keys(versions).forEach(function(version) {
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('test-trace-mongoose', function() {
var mongoose;
var Simple;
before(function() {
agent = require('../..').start({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 });

mongoose = require('./fixtures/mongoose4');
mongoose.Promise = global.Promise;
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('test-trace-mysql', function() {
agent = require('../..').start({
enhancedDatabaseReporting: true,
databaseResultReportingSize: RESULT_SIZE
}).private_();
});
mysql = require('./fixtures/mysql2');
pool = mysql.createPool({
host : 'localhost',
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('redis', function() {
samplingRate: 0,
enhancedDatabaseReporting: true,
databaseResultReportingSize: RESULT_SIZE
}).private_();
});
});

var client;
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('restify', function() {
var agent;

before(function() {
agent = require('../..').start({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 });
});

var server;
Expand Down
5 changes: 3 additions & 2 deletions test/performance/express/express-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
process.env.GCLOUD_TRACE_EXCLUDE_HTTP = true;
var traceAgent = require('../../..').start().private_();
var common = require('../../hooks/common.js');
var agent = require('../../..').start();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
common.getTraceWriter(agent).writeSpan = function() {};
}

var express = require('express');
Expand Down
12 changes: 7 additions & 5 deletions test/performance/http/http-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

'use strict';

var traceAgent;
var common;
var agent;
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
traceAgent = require('../../..').start().private_();
common = require('../../hooks/common.js');
agent = require('../../..').start();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
common.getTraceWriter(agent).writeSpan = function() {};
}

var http = require('http');
Expand All @@ -36,7 +38,7 @@ var smileyServer = http.createServer(function(req, res) {
var runInTransaction = function(fn) {
var cls = require('../../../src/cls.js');
cls.getNamespace().run(function() {
var span = traceAgent.createRootSpanData('outer');
var span = common.createRootSpanData(agent, 'outer');
fn(function() {
span.close();
});
Expand Down Expand Up @@ -65,7 +67,7 @@ var work = function(endTransaction) {
}
};

if (traceAgent) {
if (agent) {
work = runInTransaction.bind(null, work);
}

Expand Down
12 changes: 7 additions & 5 deletions test/performance/mongo/mongo-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
// Run a mongo image binding the mongo port
// ex) docker run -p 27017:27017 -d mongo

var traceAgent;
var common;
var agent;
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
traceAgent = require('../../..').start().private_();
common = require('../../hooks/common.js');
agent = require('../../..').start();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
common.getTraceWriter(agent).writeSpan = function() {};
}

var mongoose = require('mongoose');
Expand All @@ -51,7 +53,7 @@ var sim = new Simple({
var runInTransaction = function(fn) {
var cls = require('../../../src/cls.js');
cls.getNamespace().run(function() {
var span = traceAgent.createRootSpanData('outer');
var span = common.createRootSpanData(agent, 'outer');
fn(function() {
span.close();
});
Expand Down Expand Up @@ -89,7 +91,7 @@ var work = function(endTransaction) {
});
};

if (traceAgent) {
if (agent) {
work = runInTransaction.bind(null, work);
}

Expand Down
5 changes: 3 additions & 2 deletions test/performance/restify/restify-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
process.env.GCLOUD_TRACE_EXCLUDE_HTTP = true;
var traceAgent = require('../../..').start().private_();
var common = require('../../hooks/common.js');
var agent = require('../../..').start();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
common.getTraceWriter(agent).writeSpan = function() {};
}

var restify = require('restify');
Expand Down
Loading

0 comments on commit 6f05b15

Please sign in to comment.