Skip to content

Commit

Permalink
Remove the trace constructor and rename startAgent (#367)
Browse files Browse the repository at this point in the history
This PR is a follow-up to [PR 361](#361).

PR-URL: #367
  • Loading branch information
DominicKramer authored Feb 7, 2017
1 parent b0e6c04 commit c999103
Show file tree
Hide file tree
Showing 45 changed files with 95 additions and 119 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This module provides Stackdriver Trace support for Node.js applications. [Stackd

3. Include and start the library *as the very first action in your application*:

var agent = require('@google/cloud-trace')().startAgent();
var agent = require('@google/cloud-trace').start();

If you use `--require` in your start up command, make sure that the trace agent is --required first.

Expand All @@ -47,7 +47,7 @@ If you are running somewhere other than the Google Cloud Platform, see [running

See [the default configuration](config.js) for a list of possible configuration options. These options can be passed to the agent through the object argument to the start command shown above:

require('@google/cloud-trace')().startAgent({samplingRate: 500});
require('@google/cloud-trace').start({samplingRate: 500});

Alternatively, you can provide configuration through a config file. This can be useful if you want to load our module using `--require` on the command line instead of editing your main script. You can start by copying the default config file and modifying it to suit your needs. The `GCLOUD_DIAGNOSTICS_CONFIG` environment variable should point to your configuration file.

Expand Down Expand Up @@ -142,7 +142,7 @@ For any of the web frameworks listed above (`express`, `hapi`, and `restify`), a
The API is exposed by the `agent` returned by a call to `start`:

```javascript
var agent = require('@google/cloud-trace')().startAgent();
var agent = require('@google/cloud-trace').start();
```

For child spans, you can either use the `startSpan` and `endSpan` API, or use the `runInSpan` function that uses a callback-style. For root spans, you must use `runInRootSpan`.
Expand Down
54 changes: 15 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ var publicAgent = {
return agent.addTransactionLabel(key, value);
},

startAgent: function(projectConfig) {
start: function(projectConfig) {
var config = initConfig(projectConfig);

if (this.isActive() && !config.forceNewAgent_) { // already started.
Expand Down Expand Up @@ -231,34 +231,6 @@ var publicAgent = {
private_: function() { return agent; }
};

/**
* <p class="notice">
* *This module is experimental, and should be used by early adopters. This
* module uses APIs that may be undocumented and subject to change without
* notice.*
* </p>
*
* This module provides Stackdriver Trace support for Node.js applications.
* [Stackdriver Trace](https://cloud.google.com/cloud-trace/) is a feature of
* [Google Cloud Platform](https://cloud.google.com/) that collects latency
* data (traces) from your applications and displays it in near real-time in
* the [Google Cloud Console][cloud-console].
*
* @constructor
* @alias module:trace
*
* @resource [What is Stackdriver Trace]{@link
* https://cloud.google.com/cloud-trace/}
*
* @param {object} options - [Configuration object](#/docs)
*/
// TODO: Remove this constructor.
function Trace(options) {
if (!(this instanceof Trace)) {
return new Trace(options);
}
}

/**
* Start the Trace agent that will make your application available for
* tracing with Stackdriver Trace.
Expand All @@ -269,25 +241,29 @@ function Trace(options) {
* https://www.youtube.com/watch?v=NCFDqeo7AeY}
*
* @example
* trace.startAgent();
* trace.start();
*/
Trace.prototype.startAgent = function(config) {
publicAgent.startAgent(config);
function start(config) {
publicAgent.start(config);
return publicAgent;
};
}

Trace.prototype.isActive = function() {
function isActive() {
return publicAgent.isActive();
};
}

Trace.prototype.get = function() {
function get() {
return publicAgent.get();
};
}

global._google_trace_agent = publicAgent;
module.exports = Trace;
module.exports = {
start: start,
isActive: isActive,
get: get
};

// If the module was --require'd from the command line, start the agent.
if (module.parent && module.parent.id === 'internal/preload') {
module.exports().startAgent();
module.exports.start();
}
2 changes: 1 addition & 1 deletion test/fixtures/preloaded-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

var assert = require('assert');
var agent = require('../../')();
var agent = require('../../');

assert(agent.isActive());
agent.get().stop();
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/start-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@

process.env.GCLOUD_TRACE_LOGLEVEL = 4;
require('glob'); // Load a module before agent
require('../..')().startAgent();
require('../..').start();
2 changes: 1 addition & 1 deletion test/hooks/test-hooks-interop-mongo-express.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('mongodb + express', function() {
var oldDebug;
var mongoose;
before(function() {
agent = require('../..')().startAgent().get().private_();
agent = require('../..').start().get().private_();
mongoose = require('./fixtures/mongoose4');
oldDebug = agent.logger.debug;
agent.logger.debug = function(error) {
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('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
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('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
express = require('./fixtures/express4');

// Mute stderr to satiate appveyor
Expand Down
2 changes: 1 addition & 1 deletion test/hooks/test-trace-grpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ 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('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
agent.config_.enhancedDatabaseReporting = true;

common = require('./common.js');
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 @@ -46,7 +46,7 @@ Object.keys(versions).forEach(function(version) {
var agent;
var hapi;
before(function() {
agent = require('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
hapi = require(versions[version]);
});

Expand Down
4 changes: 2 additions & 2 deletions test/hooks/test-trace-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('test-trace-http', function() {
var http;
var server;
before(function() {
agent = require('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
http = require('http');

server = http.Server(function(req, res) {
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('https', function() {
var agent;
var https;
before(function() {
agent = require('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
https = require('https');
});

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 @@ -35,7 +35,7 @@ Object.keys(versions).forEach(function(version) {
var mongodb;
var server;
before(function() {
agent = require('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
mongodb = require(versions[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('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();

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 @@ -31,7 +31,7 @@ describe('test-trace-mysql', function() {
var mysql;
var pool;
before(function() {
agent = require('../..')().startAgent({
agent = require('../..').start({
enhancedDatabaseReporting: true
}).private_();
mysql = require('./fixtures/mysql2');
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 @@ -38,7 +38,7 @@ Object.keys(versions).forEach(function(version) {
var agent;
var redis;
before(function() {
agent = require('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
redis = require(versions[version]);
});

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 @@ -36,7 +36,7 @@ Object.keys(versions).forEach(function(version) {
var agent;
var restify;
before(function() {
agent = require('../..')().startAgent({ samplingRate: 0 }).private_();
agent = require('../..').start({ samplingRate: 0 }).private_();
restify = require(versions[version]);

// Mute stderr to satiate appveyor
Expand Down
2 changes: 1 addition & 1 deletion test/non-interference/express-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ cp.execFileSync('npm', ['install']);
// Reformat tests to use newly installed express
console.log('Reformating tests');
var gcloud_require = 'require(\'' + path.join(__dirname, '..', '..') +
'\')().startAgent({ forceNewAgent_: true });';
'\').start({ forceNewAgent_: true });';
glob(test_glob, function(err, files) {
error = error || err;
for (var i = 0; i < files.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion test/non-interference/http-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var test_glob = semver.satisfies(process.version, '0.12.x') ?
// Run tests
console.log('Running tests');
var gcloud_require = 'require(\'' + path.join(__dirname, '..', '..') +
'\')().startAgent();';
'\').start();';
glob(test_glob, function(err, files) {
var errors = 0;
var testCount;
Expand Down
2 changes: 1 addition & 1 deletion test/non-interference/restify-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cp.execFileSync('npm', ['install']);
// Reformat tests to use newly installed restify
console.log('Reformating tests');
var gcloud_require = 'require(\'' + path.join(__dirname, '..', '..') +
'\')().startAgent({ forceNewAgent_: true });';
'\').start({ forceNewAgent_: true });';
glob(test_glob, function(err, files) {
for (var i = 0; i < files.length; i++) {
cp.execFileSync('sed', ['-i.bak', 's#\'use strict\';#' +
Expand Down
2 changes: 1 addition & 1 deletion test/performance/express/express-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
process.env.GCLOUD_TRACE_EXCLUDE_HTTP = true;
var traceAgent = require('../../..')().startAgent().private_();
var traceAgent = require('../../..').start().private_();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
}
Expand Down
2 changes: 1 addition & 1 deletion test/performance/http/http-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
var traceAgent;
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
traceAgent = require('../../..')().startAgent().private_();
traceAgent = require('../../..').start().private_();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
}
Expand Down
2 changes: 1 addition & 1 deletion test/performance/mongo/mongo-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
var traceAgent;
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
traceAgent = require('../../..')().startAgent().private_();
traceAgent = require('../../..').start().private_();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
}
Expand Down
2 changes: 1 addition & 1 deletion test/performance/restify/restify-performance-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
if (process.argv[2] === '-i') {
process.env.GCLOUD_TRACE_ENABLED = true;
process.env.GCLOUD_TRACE_EXCLUDE_HTTP = true;
var traceAgent = require('../../..')().startAgent().private_();
var traceAgent = require('../../..').start().private_();
// We want to drop all spans and avoid network ops
traceAgent.traceWriter.writeSpan = function() {};
}
Expand Down
Loading

0 comments on commit c999103

Please sign in to comment.