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

Commit

Permalink
Add debuggee name to the re-register log message (#154)
Browse files Browse the repository at this point in the history
* Add debuggee name to the re-register log message

If an error occurs when re-registering a debuggee, the name of
the debuggee is reported in the error message to aid in debugging
why the re-registration failed.

fixes #128

* Update debugletapi test code to use new init() api

The debugletapi#init() method was updated to accept a callback
of the form 'callback(err, project)' instead of 'callback(err)'.
The test code for the class was updated so that callbacks
now include the 'project' parameter.

* Add test to validate setting debugletapi.project_

When the debugletapi.init() method calls its callback, the
project ID reported in the callback should match the project ID
stored in the debugletapi object.

* Add more robust checking of project names

The testing of the debugletapi#init() method was updated so that
the project name given through the callback was the project name
expected.

* Cleaned up the test for the init() method

Now the test for the debugletapi#init() method doesn't stub
out the utils.getProjectNumber method because it is already
being stubbed out earlier in the code.

* Fix a bug involving the GCLOUD_PROJECT env var

The tests in teh test-debugletapi.js file assumed the
GCLOUD_PROJECT environment variable was not set, and would
fail if this assumption wasn't valid.  Now the GCLOUD_PROJECT
environment variable is unset when running this file.

* Now the project is passed to all init cb calls

PR-URL: #154
  • Loading branch information
DominicKramer committed Oct 6, 2016
1 parent 1b22490 commit 9f0e2fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
9 changes: 7 additions & 2 deletions lib/debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ function Debuglet(config, logger) {
/** @private {boolean} */
this.running_ = false;

/** @private {string} */
this.project_ = null;

/** @private {boolean} */
this.fetcherActive_ = false;

Expand Down Expand Up @@ -108,7 +111,7 @@ Debuglet.prototype.start = function() {

that.logger_.info('Unique ID for this Application: ' + id);

that.debugletApi_.init(id, that.logger_, function(err) {
that.debugletApi_.init(id, that.logger_, function(err, project) {
if (err) {
that.logger_.error('Unable to initialize the debuglet api' +
' -- disabling debuglet', err);
Expand All @@ -126,6 +129,7 @@ Debuglet.prototype.start = function() {

// We can register as a debuggee now.
that.running_ = true;
that.project_ = project;
that.scheduleRegistration_(0 /* immediately */);
that.emit('started');
});
Expand All @@ -141,7 +145,8 @@ Debuglet.prototype.scheduleRegistration_ = function(seconds) {
var that = this;

function onError(err) {
that.logger_.error('Failed to re-register debuggee: ' + err);
that.logger_.error('Failed to re-register debuggee ' +
that.project_ + ': ' + err);
that.scheduleRegistration_(Math.min((seconds + 1) * 2,
that.config_.internal.maxRegistrationRetryDelay));
}
Expand Down
4 changes: 2 additions & 2 deletions lib/debugletapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DebugletApi.prototype.init = function(uid, logger, callback) {

function complete(err, project) {
if (err) {
callback(err);
callback(err, project);
return;
}
that.project_ = project;
Expand All @@ -83,7 +83,7 @@ DebugletApi.prototype.init = function(uid, logger, callback) {
logger.warn('Malformed source-context.json file.');
// But we keep on going.
}
callback(null);
callback(null, project);
});
}

Expand Down
16 changes: 12 additions & 4 deletions test/test-debugletapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var request = require('request');
var proxyquire = require('proxyquire');
var agentVersion = require('../package.json').version;

// the tests in this file rely on the GCLOUD_PROJECT environment variable
// not being set
delete process.env.GCLOUD_PROJECT;

// require DebugletAPI while stubbing auth to bypass authentication
//
var utils = {
Expand Down Expand Up @@ -53,8 +57,12 @@ describe('Debuglet API', function() {
});

it('should acquire the project number during init', function(done) {
debugletapi.init('uid123', { warn: function() {} }, function(err) {
debugletapi.init('uid123', { warn: function() {} }, function(err, project) {
assert(!err);
// make sure init() invokes the callback with the correct project name
assert.equal(project, 'project123');
// make sure the debugletapi is properly storing the project name
assert.equal(debugletapi.project_, project);
done();
});
});
Expand Down Expand Up @@ -84,9 +92,9 @@ describe('Debuglet API', function() {
utils.getProjectNumber = function(callback) {
callback(new Error(), null);
};
process.GCLOUD_PROJECT = 'project123';
process.env.GCLOUD_PROJECT = 'project123';
var debugletapi = new DebugletApi();
debugletapi.init('uid1234', { warn: function() {} }, function(err) {
debugletapi.init('uid1234', { warn: function() {} }, function(err, project) {
var scope = nock(url)
.post(api + '/debuggees/register', function (body) {
return body.debuggee.agentVersion ===
Expand All @@ -101,7 +109,7 @@ describe('Debuglet API', function() {
assert.equal(result.debuggee.id, 'fake-debuggee');
assert.equal(debugletapi.debuggeeId_, 'fake-debuggee');
scope.done();
delete process.GCLOUD_PROJECT;
delete process.env.GCLOUD_PROJECT;
utils.getProjectNumber = oldProjNum;
done();
});
Expand Down

0 comments on commit 9f0e2fd

Please sign in to comment.