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

Commit c5d3d22

Browse files
authored
switch to using @google-cloud/common (#190)
This PR starts using the authorized / retry request flow from the @google-cloud/common library. From an operational perspective This shouldn't change behaviour significantly – although there are small differences in how the new library does retries or how it prioritizes different sources of the projectId & credentials (environment vs. metadata vs. config).
1 parent 75b08e9 commit c5d3d22

File tree

11 files changed

+109
-67
lines changed

11 files changed

+109
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
coverage
33
npm-debug.log
44
.DS_Store
5+
.vscode

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"proxyquire": "^1.4.0"
3333
},
3434
"dependencies": {
35+
"@google-cloud/common": "^0.9.0",
3536
"@google/cloud-diagnostics-common": "0.3.0",
3637
"acorn": "^4.0.3",
3738
"async": "^2.1.2",

src/agent/debuglet.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ module.exports = Debuglet;
4646
* @event 'stopped' if the agent stops due to a fatal error after starting
4747
* @constructor
4848
*/
49-
function Debuglet(config, logger) {
49+
function Debuglet(debug, config, logger) {
50+
/** @private {Debug} */
51+
this.debug_ = debug;
52+
5053
/** @private {object} */
5154
this.config_ = config || {};
5255

@@ -69,7 +72,7 @@ function Debuglet(config, logger) {
6972
this.logger_ = logger;
7073

7174
/** @private {DebugletApi} */
72-
this.debugletApi_ = new DebugletApi(config);
75+
this.debugletApi_ = new DebugletApi(this.config_, this.debug_);
7376

7477
/** @private {Object.<string, Breakpoint>} */
7578
this.activeBreakpointMap_ = {};

src/debugletapi.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,15 @@ var API = 'https://clouddebugger.googleapis.com/v2/controller';
3333
/** @const {string} */ var DEBUGGEE_MAJOR_VERSION_LABEL = 'version';
3434
/** @const {string} */ var DEBUGGEE_MINOR_VERSION_LABEL = 'minorversion';
3535

36-
/** @const {Array<string>} list of scopes needed to operate with the debug API */
37-
var SCOPES = [
38-
'https://www.googleapis.com/auth/cloud-platform',
39-
'https://www.googleapis.com/auth/cloud_debugletcontroller'
40-
];
4136

4237
/**
4338
* @constructor
4439
*/
45-
function DebugletApi(config) {
46-
var config_ = config || {};
40+
function DebugletApi(config, debug) {
41+
config = config || {};
4742

48-
/** @private {Object} request style request object */
49-
this.request_ = utils.authorizedRequestFactory(SCOPES, {
50-
keyFile: config_.keyFilename,
51-
credentials: config_.credentials
52-
});
43+
/** @priavate {Debug} */
44+
this.debug_ = debug;
5345

5446
/** @private {string} numeric project id */
5547
this.project_ = null;
@@ -58,13 +50,13 @@ function DebugletApi(config) {
5850
this.debuggeeId_ = null;
5951

6052
/** @private {string} a descriptor of the current code version */
61-
this.descriptor_ = config_.description;
53+
this.descriptor_ = config.description;
6254

6355
/** @private {string} the service name of the current code */
64-
this.serviceName_ = config_.serviceContext && config_.serviceContext.service;
56+
this.serviceName_ = config.serviceContext && config.serviceContext.service;
6557

6658
/** @private {string} the version of the current code */
67-
this.serviceVersion_ = config_.serviceContext && config_.serviceContext.version;
59+
this.serviceVersion_ = config.serviceContext && config.serviceContext.version;
6860
}
6961

7062
/**
@@ -196,13 +188,12 @@ DebugletApi.prototype.register_ = function(errorMessage, callback) {
196188
}
197189

198190
var options = {
199-
url: API + '/debuggees/register',
191+
uri: API + '/debuggees/register',
200192
method: 'POST',
201193
json: true,
202194
body: { debuggee: debuggee }
203195
};
204-
205-
that.request_(options, function(err, response, body) {
196+
this.debug_.request(options, function(err, body, response) {
206197
if (err) {
207198
callback(err);
208199
} else if (response.statusCode !== 200) {
@@ -231,9 +222,9 @@ DebugletApi.prototype.listBreakpoints = function(callback) {
231222
query.waitToken = that.nextWaitToken;
232223
}
233224

234-
var url = API + '/debuggees/' + encodeURIComponent(that.debuggeeId_) +
225+
var uri = API + '/debuggees/' + encodeURIComponent(that.debuggeeId_) +
235226
'/breakpoints?' + qs.stringify(query);
236-
that.request_({url: url, json: true}, function(err, response, body) {
227+
that.debug_.request({uri: uri, json: true}, function(err, body, response) {
237228
if (!response) {
238229
callback(err || new Error('unknown error - request response missing'));
239230
return;
@@ -266,7 +257,7 @@ DebugletApi.prototype.updateBreakpoint =
266257
breakpoint.action = 'capture';
267258
breakpoint.isFinalState = true;
268259
var options = {
269-
url: API + '/debuggees/' + encodeURIComponent(this.debuggeeId_) +
260+
uri: API + '/debuggees/' + encodeURIComponent(this.debuggeeId_) +
270261
'/breakpoints/' + encodeURIComponent(breakpoint.id),
271262
json: true,
272263
method: 'PUT',
@@ -281,7 +272,7 @@ DebugletApi.prototype.updateBreakpoint =
281272
// stringify them. The try-catch keeps it resilient and avoids crashing the
282273
// user's app.
283274
try {
284-
this.request_(options, function(err, response, body) {
275+
this.debug_.request(options, function(err, body, response) {
285276
callback(err, body);
286277
});
287278
} catch (error) {

src/index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
'use strict';
1818

1919
var logger = require('@google/cloud-diagnostics-common').logger;
20+
var common = require('@google-cloud/common');
2021
var Debuglet = require('./agent/debuglet.js');
22+
var util = require('util');
2123
var _ = require('lodash');
2224

2325
/**
@@ -45,11 +47,24 @@ var _ = require('lodash');
4547
*/
4648
function Debug(options) {
4749
if (!(this instanceof Debug)) {
48-
//TODO(ofrobots)
49-
//options = common.util.normalizeArguments(this, options);
50+
options = common.util.normalizeArguments(this, options);
5051
return new Debug(options);
5152
}
53+
54+
var config = {
55+
baseUrl: 'https://clouddebugger.googleapis.com/v2',
56+
scopes: [
57+
// TODO: do we still need cloud-platform scope?
58+
'https://www.googleapis.com/auth/cloud-platform',
59+
'https://www.googleapis.com/auth/cloud_debugletcontroller'
60+
// TODO: the client library probably wants cloud_debugger scope as well.
61+
],
62+
packageJson: require('../package.json')
63+
};
64+
65+
common.Service.call(this, config, options);
5266
}
67+
util.inherits(Debug, common.Service);
5368

5469
var initConfig = function(config_) {
5570
var config = config_ || {};
@@ -77,21 +92,22 @@ var debuglet;
7792
* with Stackdriver Debug.
7893
*
7994
* @param {object=} config - Debug configuration. TODO(ofrobots): get rid of
80-
* config.js and include jsdoc here
95+
* config.js and include jsdoc here?
8196
* TODO: add an optional callback function.
8297
*
8398
* @resource [Introductory video]{@link https://www.youtube.com/watch?v=tyHcK_kAOpw}
8499
*
85100
* @example
86101
* debug.startAgent();
87102
*/
88-
Debug.prototype.startAgent = function(config_) {
103+
Debug.prototype.startAgent = function(config) {
89104
if (debuglet) {
90105
throw new Error('Debug Agent has already been started');
91106
}
92-
var config = initConfig(config_);
107+
config = initConfig(config);
93108
if (config.enabled) {
94-
debuglet = new Debuglet(config, logger.create(config.logLevel, '@google-cloud/debug'));
109+
debuglet = new Debuglet(
110+
this, config, logger.create(config.logLevel, '@google-cloud/debug'));
95111
debuglet.start();
96112
this.private_ = debuglet;
97113
}

test/auth-request.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
var request = require('request');
19+
module.exports = function(options, callback) {
20+
request(options, function(err, response, body) {
21+
callback(err, body, response);
22+
});
23+
};

test/e2e/test-breakpoints.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ function runTest() {
250250
// and log points.
251251
if (cluster.isMaster) {
252252
cluster.setupMaster({ silent: true });
253-
var handler = function(m) {
253+
var handler = function(a) {
254254
if (!debuggee) {
255255
// Cache the needed info from the first worker.
256-
debuggee = m.private_.debugletApi_.debuggeeId_;
257-
project = m.private_.debugletApi_.project_;
256+
debuggee = a[0];
257+
project = a[1];
258258
} else {
259259
// Make sure all other workers are consistent.
260-
assert.equal(debuggee, m.private_.debugletApi_.debuggeeId_);
261-
assert.equal(project, m.private_.debugletApi_.project_);
260+
assert.equal(debuggee, a[0]);
261+
assert.equal(project, a[1]);
262262
}
263263
};
264264
var stdoutHandler = function(chunk) {
@@ -288,12 +288,8 @@ if (cluster.isMaster) {
288288
assert.ok(api.uid_, 'debuglet provided unique id');
289289
assert.ok(api.debuggeeId_, 'debuglet has registered');
290290
// The parent process needs to know the debuggeeId and project.
291-
process.send(debug);
291+
process.send([api.debuggeeId_, api.project_]);
292292
setInterval(fib.bind(null, 12), 2000);
293293
}, 7000);
294294

295295
}
296-
297-
process.on('exit', function() {
298-
console.log('worker transcript:', transcript);
299-
});

test/e2e/test-log-throttling.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ function runTest() {
181181

182182
if (cluster.isMaster) {
183183
cluster.setupMaster({ silent: true });
184-
var handler = function(m) {
184+
var handler = function(a) {
185185
// Cache the needed info from the first worker.
186186
if (!debuggee) {
187-
debuggee = m.private_.debugletApi_.debuggeeId_;
188-
project = m.private_.debugletApi_.project_;
187+
debuggee = a[0];
188+
project = a[1];
189189
}
190190
};
191191
var stdoutHandler = function(chunk) {
@@ -216,7 +216,7 @@ if (cluster.isMaster) {
216216
var api = debuglet.debugletApi_;
217217
assert.ok(api.uid_, 'debuglet provided unique id');
218218
assert.ok(api.debuggeeId_, 'debuglet has registered');
219-
process.send(debug);
219+
process.send([api.debuggeeId_, api.project_]);
220220
setInterval(fib.bind(null, 12), 500);
221221
}, 7000);
222222
}

test/standalone/test-config-credentials.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('test-config-credentials', function() {
4545
var config = extend({}, defaultConfig, {
4646
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json')
4747
});
48+
var debug = require('../..')(config);
4849
var scope = nock('https://accounts.google.com')
4950
.post('/o/oauth2/token', function(body) {
5051
assert.equal(body.client_id, credentials.client_id);
@@ -63,14 +64,15 @@ describe('test-config-credentials', function() {
6364
setImmediate(done);
6465
return true;
6566
}).reply(200);
66-
debuglet = new Debuglet(config, logger.create(logger.WARN, 'testing'));
67+
debuglet = new Debuglet(debug, config, logger.create(logger.WARN, 'testing'));
6768
debuglet.start();
6869
});
6970

7071
it('should use the credentials field of the config object', function(done) {
7172
var config = extend({}, defaultConfig, {
7273
credentials: require('../fixtures/gcloud-credentials.json')
7374
});
75+
var debug = require('../..')(config);
7476
var scope = nock('https://accounts.google.com')
7577
.post('/o/oauth2/token', function(body) {
7678
assert.equal(body.client_id, config.credentials.client_id);
@@ -89,32 +91,34 @@ describe('test-config-credentials', function() {
8991
setImmediate(done);
9092
return true;
9193
}).reply(200);
92-
debuglet = new Debuglet(config, logger.create(undefined, 'testing'));
94+
debuglet = new Debuglet(debug, config, logger.create(undefined, 'testing'));
9395
debuglet.start();
9496
});
9597

96-
it('should ignore credentials if keyFilename is provided', function(done) {
97-
var correctCredentials = require('../fixtures/gcloud-credentials.json');
98-
var config = extend({}, defaultConfig, {
99-
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
100-
credentials: {
98+
it('should ignore keyFilename if credentials is provided', function(done) {
99+
var fileCredentials = require('../fixtures/gcloud-credentials.json');
100+
var credentials = {
101101
client_id: 'a',
102102
client_secret: 'b',
103103
refresh_token: 'c',
104104
type: 'authorized_user'
105-
}
105+
};
106+
var config = extend({}, defaultConfig, {
107+
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
108+
credentials: credentials
106109
});
110+
var debug = require('../..')(config);
107111
['client_id', 'client_secret', 'refresh_token'].forEach(function (field) {
108-
assert(correctCredentials.hasOwnProperty(field));
112+
assert(fileCredentials.hasOwnProperty(field));
109113
assert(config.credentials.hasOwnProperty(field));
110114
assert.notEqual(config.credentials[field],
111-
correctCredentials[field]);
115+
fileCredentials[field]);
112116
});
113117
var scope = nock('https://accounts.google.com')
114118
.post('/o/oauth2/token', function(body) {
115-
assert.equal(body.client_id, correctCredentials.client_id);
116-
assert.equal(body.client_secret, correctCredentials.client_secret);
117-
assert.equal(body.refresh_token, correctCredentials.refresh_token);
119+
assert.equal(body.client_id, credentials.client_id);
120+
assert.equal(body.client_secret, credentials.client_secret);
121+
assert.equal(body.refresh_token, credentials.refresh_token);
118122
return true;
119123
}).reply(200, {
120124
refresh_token: 'hello',
@@ -128,7 +132,7 @@ describe('test-config-credentials', function() {
128132
setImmediate(done);
129133
return true;
130134
}).reply(200);
131-
debuglet = new Debuglet(config, logger.create(undefined, 'testing'));
135+
debuglet = new Debuglet(debug, config, logger.create(undefined, 'testing'));
132136
debuglet.start();
133137
});
134138
});

0 commit comments

Comments
 (0)