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

Commit

Permalink
refactorings
Browse files Browse the repository at this point in the history
* rename debugletapi to controller
* rename apiclasses.js to status-message.js
* add a debuggee class
* add a basic debuggee object
  • Loading branch information
ofrobots committed Dec 15, 2016
1 parent c5d3d22 commit 638f902
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 115 deletions.
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"indent": 2,
"latedef": "nofunc",
"maxlen": 100,
"newcap": true,
"node": true,
"noarg": true,
"quotmark": "single",
Expand Down
4 changes: 2 additions & 2 deletions src/agent/debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var util = require('util');
var semver = require('semver');

var v8debugapi = require('./v8debugapi.js');
var DebugletApi = require('../debugletapi.js');
var DebugletApi = require('../controller.js');
var scanner = require('./scanner.js');
var Logger = require('@google/cloud-diagnostics-common').logger;
var StatusMessage = require('../apiclasses.js').StatusMessage;
var StatusMessage = require('../status-message.js');
var SourceMapper = require('./sourcemapper.js');

var assert = require('assert');
Expand Down
2 changes: 1 addition & 1 deletion src/agent/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var remove = lodash.remove;
var flatten = lodash.flatten;
var isEmpty = lodash.isEmpty;

var StatusMessage = require('../apiclasses.js').StatusMessage;
var StatusMessage = require('../status-message.js');

// Error message indices into the resolved variable table.
var BUFFER_FULL_MESSAGE_INDEX = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/agent/v8debugapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

/** @const */ var state = require('./state.js');
/** @const */ var logModule = require('@google/cloud-diagnostics-common').logger;
/** @const */ var apiclasses = require('../apiclasses.js');
/** @const */ var StatusMessage = apiclasses.StatusMessage;
/** @const */ var StatusMessage = require('../status-message.js');

/** @const */ var messages = {
INVALID_BREAKPOINT: 'invalid snapshot - id or location missing',
Expand Down
93 changes: 17 additions & 76 deletions src/debugletapi.js → src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,23 @@

'use strict';

/*!
* @module debug/controller
*/

var fs = require('fs');
var path = require('path');
var assert = require('assert');
var crypto = require('crypto');
var pjson = require('../package.json');
var qs = require('querystring');
var utils = require('@google/cloud-diagnostics-common').utils;
var StatusMessage = require('./apiclasses.js').StatusMessage;
var Debuggee = require('./debuggee.js');

/** @const {string} Cloud Debug API endpoint */
var API = 'https://clouddebugger.googleapis.com/v2/controller';

/* c.f. the Java Debugger agent */
/** @const {string} */ var DEBUGGEE_MODULE_LABEL = 'module';
/** @const {string} */ var DEBUGGEE_MAJOR_VERSION_LABEL = 'version';
/** @const {string} */ var DEBUGGEE_MINOR_VERSION_LABEL = 'minorversion';


/**
* @constructor
*/
function DebugletApi(config, debug) {
function Controller(config, debug) {
config = config || {};

/** @priavate {Debug} */
Expand Down Expand Up @@ -68,7 +63,7 @@ function DebugletApi(config, debug) {
* @param {Logger} logger a logger
* @param {!function(?Error)} callback
*/
DebugletApi.prototype.init = function(uid, logger, callback) {
Controller.prototype.init = function(uid, logger, callback) {
var that = this;
that.uid_ = uid;
that.nextWaitToken_ = null;
Expand Down Expand Up @@ -105,7 +100,7 @@ DebugletApi.prototype.init = function(uid, logger, callback) {
* Register to the API
* @param {!function(?Error,Object=)} callback
*/
DebugletApi.prototype.register = function(callback) {
Controller.prototype.register = function(callback) {
this.register_(null, callback);
};

Expand All @@ -114,7 +109,7 @@ DebugletApi.prototype.register = function(callback) {
* Register an error to the API
* @param {!string} errorMessage to be reported to the Debug API
*/
DebugletApi.prototype.registerError = function(message) {
Controller.prototype.registerError = function(message) {
this.register_(message, function() {});
};

Expand All @@ -126,66 +121,12 @@ DebugletApi.prototype.registerError = function(message) {
* @param {!function(?Error,Object=)} callback
* @private
*/
DebugletApi.prototype.register_ = function(errorMessage, callback) {
Controller.prototype.register_ = function(errorMessage, callback) {
var that = this;

var cwd = process.cwd();
var mainScript = path.relative(cwd, process.argv[1]);

var version = 'google.com/node-' +
(that.onGCP ? 'gcp' : 'standalone') +
'/v' + pjson.version;
var desc = process.title + ' ' + mainScript;
var labels = {
'main script': mainScript,
'process.title': process.title,
'node version': process.versions.node,
'V8 version': process.versions.v8,
'agent.name': pjson.name,
'agent.version': pjson.version,
'projectid': that.project_
};

var serviceName = that.serviceName_;
if (serviceName) {
desc += ' module:' + serviceName;
labels[DEBUGGEE_MODULE_LABEL] = serviceName;
}

var serviceVersion = that.serviceVersion_;
if (serviceVersion) {
desc += ' version:' + serviceVersion;
if (serviceVersion !== 'default') {
labels[DEBUGGEE_MAJOR_VERSION_LABEL] = serviceVersion;
}
}

var descriptor = that.descriptor_;
if (descriptor) {
desc += ' description:' + descriptor;
}

if (process.env.GAE_MINOR_VERSION) {
labels[DEBUGGEE_MINOR_VERSION_LABEL] = process.env.GAE_MINOR_VERSION;
}

var uniquifier = desc + version + that.uid_ + that.sourceContext_ +
JSON.stringify(labels);
uniquifier = crypto.createHash('sha1').update(uniquifier).digest('hex');

var debuggee = {
project: that.project_,
uniquifier: uniquifier,
description: desc,
agentVersion: version,
labels: labels,
sourceContexts: [that.sourceContext_]
};

if (errorMessage) {
debuggee.status = new StatusMessage(StatusMessage.UNSPECIFIED, errorMessage,
true);
}
var debuggee = new Debuggee(
that.project_, that.uid_,
{service: that.serviceName_, version: that.serviceVersion_},
that.sourceContext_, that.descriptor_, errorMessage, that.onGCP);

var options = {
uri: API + '/debuggees/register',
Expand Down Expand Up @@ -214,7 +155,7 @@ DebugletApi.prototype.register_ = function(errorMessage, callback) {
* Fetch the list of breakpoints from the server. Assumes we have registered.
* @param {!function(?Error,Object=,Object=)} callback accepting (err, response, body)
*/
DebugletApi.prototype.listBreakpoints = function(callback) {
Controller.prototype.listBreakpoints = function(callback) {
var that = this;
assert(that.debuggeeId_, 'should register first');
var query = { success_on_timeout: true };
Expand Down Expand Up @@ -250,7 +191,7 @@ DebugletApi.prototype.listBreakpoints = function(callback) {
* @param {!Breakpoint} breakpoint
* @param {!Function} callback accepting (err, body)
*/
DebugletApi.prototype.updateBreakpoint =
Controller.prototype.updateBreakpoint =
function(breakpoint, callback) {
assert(this.debuggeeId_, 'should register first');

Expand Down Expand Up @@ -280,4 +221,4 @@ DebugletApi.prototype.updateBreakpoint =
}
};

module.exports = DebugletApi;
module.exports = Controller;
114 changes: 114 additions & 0 deletions src/debuggee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright 2016 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 crypto = require('crypto');
var path = require('path');
var pjson = require('../package.json');
var StatusMessage = require('./status-message.js');
var _ = require('lodash');

/**
* Creates a Debuggee service object.
* @ref https://cloud.google.com/debugger/api/reference/rest/v2/Debuggee
*
* @param {string} projectId - Google Cloud Project ID
* @param {string} uid - unique identified for the source code on this instance.
* @param {?object} serviceContext
* @param {string} serviceContext.service - A string identifying the service/
* module that this instance belongs to.
* @param {string} serviceContext.version - A string identifying the version of
* the service.
* @param {?object} sourceContext
* @param {?string} description - A user specified string identifying this
* debuggable instance.
* @param {?string} errorMessage - A error string to register this as a erroring
* debuggable instance. This is useful if we have a problem starting the
* debugger support, and want to report to the API so that the users has a
* way of noticing.
* @param {?boolean} onGCP - set to true when the debuggee is running inside
* Google Cloud Platform.
*/
function Debuggee(projectId, uid, serviceContext, sourceContext, description,
errorMessage, onGCP) {
if (!(this instanceof Debuggee)) {
return new Debuggee(projectId, uid, serviceContext, sourceContext,
description, errorMessage, onGCP);
}

if (!_.isString(projectId)) {
throw new Error('projectId must be a string');
}
if (!_.isString(uid)) {
throw new Error('uid must be a string');
}

var cwd = process.cwd();
var mainScript = path.relative(cwd, process.argv[1]);

var version = 'google.com/node-' + (onGCP ? 'gcp' : 'standalone') + '/v' +
pjson.version;
var desc = process.title + ' ' + mainScript;

var labels = {
'main script': mainScript,
'process.title': process.title,
'node version': process.versions.node,
'V8 version': process.versions.v8,
'agent.name': pjson.name,
'agent.version': pjson.version,
'projectid': projectId
};

if (serviceContext) {
if (_.isString(serviceContext.service) &&
serviceContext.service !== 'default') {
// As per app-engine-ids, the module label is not reported
// when it happens to be 'default'.
labels.module = serviceContext.service;
desc += ' module:' + serviceContext.service;
}

if (_.isString(serviceContext.version)) {
labels.version = serviceContext.version;
desc += ' version:' + serviceContext.version;
}
}

if (description) {
desc += ' description:' + description;
}

var uniquifier =
desc + version + uid + sourceContext + JSON.stringify(labels);
uniquifier = crypto.createHash('sha1').update(uniquifier).digest('hex');

if (errorMessage) {
this.statusMessage =
new StatusMessage(StatusMessage.UNSPECIFIED, errorMessage, true);
}

this.project = projectId;
this.uniquifier = uniquifier;
this.description = desc;
this.agentVersion = version;
this.labels = labels;
if (sourceContext) {
this.sourceContexts = [sourceContext];
}
}

module.exports = Debuggee;
34 changes: 17 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,25 @@ var _ = require('lodash');

/**
* <p class="notice">
* **This is an experimental release of Stackdriver Debug.** This API is not
* **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
*
* 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.
*
* 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). NOTE: at the moment
* this parameter is ignored.
*
* @param {object} options - [Configuration object](#/docs)
*/
function Debug(options) {
if (!(this instanceof Debug)) {
Expand All @@ -52,9 +51,10 @@ function Debug(options) {
}

var config = {
projectIdRequired: false,
baseUrl: 'https://clouddebugger.googleapis.com/v2',
scopes: [
// TODO: do we still need cloud-platform scope?
// TODO: do we still need cloud-platform scope?
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/cloud_debugletcontroller'
// TODO: the client library probably wants cloud_debugger scope as well.
Expand Down Expand Up @@ -90,13 +90,14 @@ 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?
* TODO: add an optional callback function.
*
* @resource [Introductory video]{@link https://www.youtube.com/watch?v=tyHcK_kAOpw}
*
*
* @resource [Introductory video]{@link
* https://www.youtube.com/watch?v=tyHcK_kAOpw}
*
* @example
* debug.startAgent();
*/
Expand All @@ -114,4 +115,3 @@ Debug.prototype.startAgent = function(config) {
};

module.exports = Debug;

Loading

0 comments on commit 638f902

Please sign in to comment.