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

Commit dbff4dc

Browse files
authored
Move debuggee agent logic to agent/ (#203)
1 parent 416031f commit dbff4dc

File tree

6 files changed

+163
-120
lines changed

6 files changed

+163
-120
lines changed

src/agent/debuglet.js

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
'use strict';
1818

19+
var crypto = require('crypto');
1920
var fs = require('fs');
2021
var path = require('path');
2122
var EventEmitter = require('events').EventEmitter;
2223
var extend = require('extend');
2324
var util = require('util');
2425
var semver = require('semver');
26+
var _ = require('lodash');
2527

2628
var v8debugapi = require('./v8debugapi.js');
2729
var Debuggee = require('../debuggee.js');
@@ -32,6 +34,7 @@ var common = require('@google/cloud-diagnostics-common');
3234
var Logger = common.logger;
3335
var StatusMessage = require('../status-message.js');
3436
var SourceMapper = require('./sourcemapper.js');
37+
var pjson = require('../../package.json');
3538

3639
var assert = require('assert');
3740

@@ -182,7 +185,7 @@ Debuglet.prototype.start = function() {
182185
that.logger_.debug('Starting debuggee, project', project);
183186
that.running_ = true;
184187
that.project_ = project;
185-
that.debuggee_ = new Debuggee(
188+
that.debuggee_ = Debuglet.createDebuggee(
186189
project, id, that.config_.serviceContext, sourceContext,
187190
that.config_.description, null, onGCP);
188191
that.scheduleRegistration_(0 /* immediately */);
@@ -194,11 +197,73 @@ Debuglet.prototype.start = function() {
194197
});
195198
};
196199

197-
198200
/**
199201
* @private
200202
*/
201-
Debuglet.prototype.getProjectId_ = function(callback) {
203+
Debuglet.createDebuggee =
204+
function(projectId, uid, serviceContext, sourceContext, description,
205+
errorMessage, onGCP) {
206+
var cwd = process.cwd();
207+
var mainScript = path.relative(cwd, process.argv[1]);
208+
209+
var version = 'google.com/node-' + (onGCP ? 'gcp' : 'standalone') + '/v' +
210+
pjson.version;
211+
var desc = process.title + ' ' + mainScript;
212+
213+
var labels = {
214+
'main script': mainScript,
215+
'process.title': process.title,
216+
'node version': process.versions.node,
217+
'V8 version': process.versions.v8,
218+
'agent.name': pjson.name,
219+
'agent.version': pjson.version,
220+
'projectid': projectId
221+
};
222+
223+
if (serviceContext) {
224+
if (_.isString(serviceContext.service) &&
225+
serviceContext.service !== 'default') {
226+
// As per app-engine-ids, the module label is not reported
227+
// when it happens to be 'default'.
228+
labels.module = serviceContext.service;
229+
desc += ' module:' + serviceContext.service;
230+
}
231+
232+
if (_.isString(serviceContext.version)) {
233+
labels.version = serviceContext.version;
234+
desc += ' version:' + serviceContext.version;
235+
}
236+
}
237+
238+
if (description) {
239+
desc += ' description:' + description;
240+
}
241+
242+
var uniquifier =
243+
desc + version + uid + sourceContext + JSON.stringify(labels);
244+
uniquifier = crypto.createHash('sha1').update(uniquifier).digest('hex');
245+
246+
var statusMessage =
247+
errorMessage ?
248+
new StatusMessage(StatusMessage.UNSPECIFIED, errorMessage, true) :
249+
null;
250+
251+
var properties = {
252+
project: projectId,
253+
uniquifier: uniquifier,
254+
description: desc,
255+
agentVersion: version,
256+
labels: labels,
257+
statusMessage: statusMessage,
258+
sourceContexts: [sourceContext]
259+
};
260+
return new Debuggee(properties);
261+
};
262+
263+
/**
264+
* @private
265+
*/
266+
Debuglet.prototype.getProjectId_ = function(callback) {
202267
var that = this;
203268

204269
// We need to figure out whether we are running on GCP. We can use our ability

src/debuggee.js

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,99 +15,62 @@
1515
*/
1616
'use strict';
1717

18-
var crypto = require('crypto');
19-
var path = require('path');
2018
var pjson = require('../package.json');
21-
var StatusMessage = require('./status-message.js');
2219
var _ = require('lodash');
2320

2421
/**
2522
* Creates a Debuggee service object.
2623
* @ref https://cloud.google.com/debugger/api/reference/rest/v2/Debuggee
2724
*
28-
* @param {string} projectId - Google Cloud Project ID
29-
* @param {string} uid - unique identified for the source code on this instance.
30-
* @param {?object} serviceContext
31-
* @param {string} serviceContext.service - A string identifying the service/
32-
* module that this instance belongs to.
33-
* @param {string} serviceContext.version - A string identifying the version of
34-
* the service.
35-
* @param {?object} sourceContext
36-
* @param {?string} description - A user specified string identifying this
37-
* debuggable instance.
38-
* @param {?string} errorMessage - A error string to register this as a erroring
39-
* debuggable instance. This is useful if we have a problem starting the
40-
* debugger support, and want to report to the API so that the users has a
41-
* way of noticing.
42-
* @param {?boolean} onGCP - set to true when the debuggee is running inside
43-
* Google Cloud Platform.
25+
* @param {object} properties - an object with properties to use for Debuggee
26+
* initialization.
27+
* @param {object} properties.project - Google Cloud Project ID
28+
* @param {string} properties.uniquifier - Debuggee uniquifier within the
29+
* project. Any string that identifies the application within the project
30+
* can be used. Including environment and version or build IDs is
31+
* recommended.
32+
* @param {?string} properties.description - A user specified string identifyin
33+
* this debuggable instance (default: none)
34+
* @param {?string} properties.agentVersion - version ID of the agent. (default:
35+
* the version of this module)
36+
* @param {?object} labels - a set of custom properties about the debuggee that
37+
* are reported to the service.
38+
* @param {?array<object>} properties.sourceContexts
39+
* @param {?StatusMessage} properties.statusMessage - A error string to register
40+
* this as an erroring debuggable instance. This is useful if we have a
41+
* problem starting the debugger support, and want to report to the API so
42+
* that the user has a way of noticing.
43+
* TODO(ofrobots): has this been renamed to `status` in the API?
4444
*/
45-
function Debuggee(projectId, uid, serviceContext, sourceContext,
46-
description, errorMessage, onGCP) {
45+
function Debuggee(properties) {
4746
if (!(this instanceof Debuggee)) {
48-
return new Debuggee(projectId, uid, serviceContext, sourceContext,
49-
description, errorMessage, onGCP);
47+
return new Debuggee(properties);
5048
}
5149

52-
if (!_.isString(projectId)) {
53-
throw new Error('projectId must be a string');
50+
properties = properties || {};
51+
52+
if (!_.isString(properties.project)) {
53+
throw new Error('properties.project must be a string');
5454
}
55-
if (!_.isString(uid)) {
56-
throw new Error('uid must be a string');
55+
if (!_.isString(properties.uniquifier)) {
56+
throw new Error('properties.uniquifier must be a string');
5757
}
5858

59-
var cwd = process.cwd();
60-
var mainScript = path.relative(cwd, process.argv[1]);
61-
62-
var version = 'google.com/node-' + (onGCP ? 'gcp' : 'standalone') + '/v' +
63-
pjson.version;
64-
var desc = process.title + ' ' + mainScript;
65-
66-
var labels = {
67-
'main script': mainScript,
68-
'process.title': process.title,
69-
'node version': process.versions.node,
70-
'V8 version': process.versions.v8,
71-
'agent.name': pjson.name,
72-
'agent.version': pjson.version,
73-
'projectid': projectId
74-
};
75-
76-
if (serviceContext) {
77-
if (_.isString(serviceContext.service) &&
78-
serviceContext.service !== 'default') {
79-
// As per app-engine-ids, the module label is not reported
80-
// when it happens to be 'default'.
81-
labels.module = serviceContext.service;
82-
desc += ' module:' + serviceContext.service;
83-
}
84-
85-
if (_.isString(serviceContext.version)) {
86-
labels.version = serviceContext.version;
87-
desc += ' version:' + serviceContext.version;
88-
}
59+
this.project = properties.project;
60+
this.uniquifier = properties.uniquifier;
61+
this.agentVersion =
62+
properties.agentVersion || (pjson.name + '/default/v' + pjson.version);
63+
if (properties.description) {
64+
this.description = properties.description;
8965
}
90-
91-
if (description) {
92-
desc += ' description:' + description;
66+
if (properties.labels) {
67+
this.labels = properties.labels;
9368
}
94-
95-
var uniquifier =
96-
desc + version + uid + sourceContext + JSON.stringify(labels);
97-
uniquifier = crypto.createHash('sha1').update(uniquifier).digest('hex');
98-
99-
if (errorMessage) {
100-
this.statusMessage =
101-
new StatusMessage(StatusMessage.UNSPECIFIED, errorMessage, true);
69+
if (properties.sourceContexts) {
70+
this.sourceContexts = properties.sourceContexts;
10271
}
103-
104-
this.project = projectId;
105-
this.uniquifier = uniquifier;
106-
this.description = desc;
107-
this.agentVersion = version;
108-
this.labels = labels;
109-
if (sourceContext) {
110-
this.sourceContexts = [sourceContext];
72+
if (properties.statusMessage) {
73+
this.statusMessage = properties.statusMessage;
11174
}
11275
}
11376

system-test/test-controller.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ describe('Controller', function() {
3636
it('should register successfully', function(done) {
3737
var controller = new Controller(debug);
3838
var debuggee =
39-
new Debuggee(process.env.GCLOUD_PROJECT, 'test-uid-' + Date.now());
39+
new Debuggee({
40+
project: process.env.GCLOUD_PROJECT,
41+
uniquifier: 'test-uid-' + Date.now()
42+
});
4043

4144
controller.register(debuggee, function(err, body) {
4245
assert.ifError(err, 'should be able to register successfully');
@@ -50,7 +53,10 @@ describe('Controller', function() {
5053
it('should list breakpoints', function(done) {
5154
var controller = new Controller(debug);
5255
var debuggee =
53-
new Debuggee(process.env.GCLOUD_PROJECT, 'test-uid-' + Date.now());
56+
new Debuggee({
57+
project: process.env.GCLOUD_PROJECT,
58+
uniquifier: 'test-uid-' + Date.now()
59+
});
5460
controller.register(debuggee, function(err, body) {
5561
assert.ifError(err, 'should be able to register successfully');
5662

test/standalone/test-debuglet.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var errorBp = {
4242
location: { path: 'fixtures/foo.js', line: 2 }
4343
};
4444

45-
describe(__filename, function(){
45+
describe(__filename, function() {
4646
var debuglet;
4747

4848
beforeEach(function() {
@@ -486,5 +486,35 @@ describe(__filename, function(){
486486
), 'hi b');
487487
});
488488
});
489-
});
490489

490+
describe('createDebuggee', function() {
491+
it('should have sensible labels', function() {
492+
var debuggee = Debuglet.createDebuggee(
493+
'some project', 'id',
494+
{service: 'some-service', version: 'production'});
495+
assert.ok(debuggee);
496+
assert.ok(debuggee.labels);
497+
assert.strictEqual(debuggee.labels.module, 'some-service');
498+
assert.strictEqual(debuggee.labels.version, 'production');
499+
});
500+
501+
it('should not add a module label when service is default', function() {
502+
var debuggee =
503+
Debuglet.createDebuggee('fancy-project', 'very-unique',
504+
{service: 'default', version: 'yellow.5'});
505+
assert.ok(debuggee);
506+
assert.ok(debuggee.labels);
507+
assert.strictEqual(debuggee.labels.module, undefined);
508+
assert.strictEqual(debuggee.labels.version, 'yellow.5');
509+
});
510+
511+
it('should have an error statusMessage with the appropriate arg',
512+
function() {
513+
var debuggee = Debuglet.createDebuggee(
514+
'a', 'b', undefined, undefined, undefined, 'Some Error Message');
515+
assert.ok(debuggee);
516+
assert.ok(debuggee.statusMessage);
517+
});
518+
});
519+
520+
});

test/test-controller.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ describe('Controller API', function() {
5454
.post(api + '/debuggees/register')
5555
.reply(200,
5656
{debuggee: {id: 'fake-debuggee'}, activePeriodSec: 600});
57-
var debuggee = new Debuggee('fake-project', 'fake-id');
57+
var debuggee =
58+
new Debuggee({project: 'fake-project', uniquifier: 'fake-id'});
5859
var controller = new Controller(fakeDebug);
5960
controller.register(debuggee, function(err, result) {
6061
assert(!err, 'not expecting an error');
@@ -76,7 +77,8 @@ describe('Controller API', function() {
7677
.post(api + '/debuggees/register')
7778
.reply(200,
7879
{debuggee: {id: 'fake-debuggee'}, activePeriodSec: 600});
79-
var debuggee = new Debuggee('fake-project', 'fake-id');
80+
var debuggee =
81+
new Debuggee({project: 'fake-project', uniquifier: 'fake-id'});
8082
controller.register(debuggee, function(err, result) {
8183
assert(!err, 'not expecting an error');
8284
assert.equal(result.debuggee.id, 'fake-debuggee');
@@ -97,7 +99,8 @@ describe('Controller API', function() {
9799
},
98100
activePeriodSec: 600,
99101
});
100-
var debuggee = new Debuggee('fake-project', 'fake-id');
102+
var debuggee =
103+
new Debuggee({project: 'fake-project', uniquifier: 'fake-id'});
101104
var controller = new Controller(fakeDebug);
102105
controller.register(debuggee, function(err/*, result*/) {
103106
assert(err, 'expected an error');
@@ -118,7 +121,8 @@ describe('Controller API', function() {
118121
debuggee: { id: 'fake-debuggee' },
119122
activePeriodSec: 600
120123
});
121-
var debuggee = new Debuggee('fake-project', 'fake-id');
124+
var debuggee =
125+
new Debuggee({project: 'fake-project', uniquifier: 'fake-id'});
122126
var controller = new Controller(fakeDebug);
123127
controller.register(debuggee, function(err/*, result*/) {
124128
assert.ifError(err);

0 commit comments

Comments
 (0)