Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Get rid of module returning a constructor (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Feb 2, 2017
1 parent fdbbea5 commit 7cbee73
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 124 deletions.
18 changes: 17 additions & 1 deletion src/agent/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,21 @@ module.exports = {
internal: {
registerDelayOnFetcherErrorSec: 300, // 5 minutes.
maxRegistrationRetryDelay: 40
}
},

/**
* @property {boolean} Used by tests to force loading of a new agent if one
* exists already
* @memberof DebugAgentConfig
* @private
*/
forceNewAgent_: false,

/**
* @property {boolean} Uses by tests to cause the start() function to return
* the debuglet.
* @memberof DebugAgentConfig
* @private
*/
testMode_: false
};
67 changes: 67 additions & 0 deletions src/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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 common = require('@google-cloud/common');
var util = require('util');

/**
* <p class="notice">
* **This is an experimental release of Stackdriver Debug.** This API is not
* covered by any SLA of deprecation policy and may be subject to backwards
* incompatible changes.
* </p>
*
* This module provides Stackdriver Debugger support for Node.js applications.
* [Stackdriver Debugger](https://cloud.google.com/debug/) is a feature of
* [Google Cloud Platform](https://cloud.google.com/) that lets you debug your
* applications in production without stopping or pausing your application.
*
* This module provides an agent that lets you automatically enable debugging
* without changes to your application.
*
* @constructor
* @alias module:debug
*
* @resource [What is Stackdriver Debug]{@link https://cloud.google.com/debug/}
*
* @param {object} options - [Configuration object](#/docs)
*/
function Debug(options) {
if (!(this instanceof Debug)) {
options = common.util.normalizeArguments(this, options);
return new Debug(options);
}

var config = {
projectIdRequired: false,
baseUrl: 'https://clouddebugger.googleapis.com/v2',
scopes: ['https://www.googleapis.com/auth/cloud_debugger'],
packageJson: require('../package.json')
};

common.Service.call(this, config, options);

// FIXME(ofrobots): We need our own copy of options because Service may
// default to '{{projectId}}' when options doesn't contain the `projectId`.
// property. This breaks the SSOT principle. Remove this when
// https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1891
// is resolved.
this.options = options;
}
util.inherits(Debug, common.Service);

module.exports = Debug;
78 changes: 20 additions & 58 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,18 @@

'use strict';

var common = require('@google-cloud/common');
var Debug = require('./debug.js');
var Debuglet = require('./agent/debuglet.js');
var util = require('util');

/**
* <p class="notice">
* **This is an experimental release of Stackdriver Debug.** This API is not
* covered by any SLA of deprecation policy and may be subject to backwards
* incompatible changes.
* </p>
*
* This module provides Stackdriver Debugger support for Node.js applications.
* [Stackdriver Debugger](https://cloud.google.com/debug/) is a feature of
* [Google Cloud Platform](https://cloud.google.com/) that lets you debug your
* applications in production without stopping or pausing your application.
*
* This module provides an agent that lets you automatically enable debugging
* without changes to your application.
*
* @constructor
* @alias module:debug
*
* @resource [What is Stackdriver Debug]{@link https://cloud.google.com/debug/}
*
* @param {object} options - [Configuration object](#/docs)
*/
function Debug(options) {
if (!(this instanceof Debug)) {
options = common.util.normalizeArguments(this, options);
return new Debug(options);
}

var config = {
projectIdRequired: false,
baseUrl: 'https://clouddebugger.googleapis.com/v2',
scopes: ['https://www.googleapis.com/auth/cloud_debugger'],
packageJson: require('../package.json')
};

common.Service.call(this, config, options);

// FIXME(ofrobots): We need our own copy of options because Service may
// default to '{{projectId}}' when options doesn't contain the `projectId`.
// property. This breaks the SSOT principle. Remove this when
// https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1891
// is resolved.
this.options = options;
}
util.inherits(Debug, common.Service);

// Singleton.
var debuglet;

/**
* Start the Debug agent that will make your application available for debugging
* with Stackdriver Debug.
*
* @param {object=} config - Debug configuration. TODO(ofrobots): get rid of
* config.js and include jsdoc here?
* @param {object=} options - Options
* @param {object=} options.debugAgent - Debug agent configuration
* TODO: add an optional callback function.
*
* @resource [Introductory video]{@link
Expand All @@ -82,15 +36,23 @@ var debuglet;
* @example
* debug.startAgent();
*/
Debug.prototype.startAgent = function(config) {
// config.forceNewAgent_ is for testing purposes only.
if (debuglet && !config.forceNewAgent_) {
throw new Error('Debug Agent has already been started');
function start(options) {
options = options || {};
var agentConfig = options.debug || {};

// forceNewAgent_ is for testing purposes only.
if (debuglet && !agentConfig.forceNewAgent_) {
throw new Error('Debug Agent has already been starterd');
}

debuglet = new Debuglet(this, config);
var debug = new Debug(options);
debuglet = new Debuglet(debug, agentConfig);
debuglet.start();
this.private_ = debuglet;
};

module.exports = Debug;
// We return the debuglet to facilitate testing.
return agentConfig.testMode_ ? debuglet : undefined;
}

module.exports = {
start: start
};
2 changes: 1 addition & 1 deletion system-test/test-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ assert.ok(

var Controller = require('../src/controller.js');
var Debuggee = require('../src/debuggee.js');
var debug = require('../')();
var debug = require('../src/debug.js')();


describe('Controller', function() {
Expand Down
2 changes: 1 addition & 1 deletion system-test/test-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = require('lodash'); // for _.find. Can't use ES6 yet.
var cp = require('child_process');
var semver = require('semver');
var promisifyAll = require('@google-cloud/common').util.promisifyAll;
var Debug = require('..');
var Debug = require('../src/debug.js');
var Debugger = require('../test/debugger.js');

var CLUSTER_WORKERS = 3;
Expand Down
18 changes: 10 additions & 8 deletions test/fixtures/fib.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ function fib(n) {
* limitations under the License.
*/

var debug = require('../..')();
debug.startAgent({
logLevel: 2,
maxLogsPerSecond: 2,
logDelaySeconds: 5,
breakpointUpdateIntervalSec: 1
var debuglet = require('../..').start({
debug: {
logLevel: 2,
maxLogsPerSecond: 2,
logDelaySeconds: 5,
breakpointUpdateIntervalSec: 1,
testMode_: true
}
});

// Make troubleshooting easier if run by itself
Expand All @@ -43,7 +45,7 @@ var registrationTimeout = setTimeout(function() {
}, 2000);
}, 5000);

debug.private_.once('registered', function() {
debuglet.once('registered', function() {
if (timedOut) {
return;
}
Expand All @@ -56,7 +58,7 @@ debug.private_.once('registered', function() {
}
};

var debuggee = debug.private_.debuggee_;
var debuggee = debuglet.debuggee_;
setErrorIfNotOk(debuggee, 'should create debuggee');
setErrorIfNotOk(debuggee.project, 'debuggee should have a project');
setErrorIfNotOk(debuggee.id, 'debuggee should have registered');
Expand Down
Loading

0 comments on commit 7cbee73

Please sign in to comment.