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

Commit

Permalink
Add config and more descriptive names on GCE/GKE
Browse files Browse the repository at this point in the history
Allow for custom config files specified by the GCLOUD_DIAGNOSTICS_CONFIG
environment variable.

Also attach provided description to the debuggee so it is visible in the
UI.
  • Loading branch information
Matt Loring committed May 12, 2016
1 parent 3fcf3e8 commit c7e1572
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 58 deletions.
84 changes: 44 additions & 40 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,60 @@

// Default configuration
module.exports = {
enabled: true,
workingDirectory: process.cwd(),
debug: {
enabled: true,
workingDirectory: process.cwd(),

// The path within your repository to the directory containing the
// package.json for your deployed application. This should be provided
// if your deployed application appears as a subdirectory of your repository.
appPathRelativeToRepository: undefined,
// An identifier for the current code deployment.
description: undefined,

// Log levels: 0-disabled,1-error,2-warn,3-info,4-debug.
logLevel: 1,
// The path within your repository to the directory containing the
// package.json for your deployed application. This should be provided
// if your deployed application appears as a subdirectory of your repository.
appPathRelativeToRepository: undefined,

// How frequently should the list of breakpoints be refreshed from the
// cloud debug server.
breakpointUpdateIntervalSec: 10,
// Log levels: 0-disabled,1-error,2-warn,3-info,4-debug.
logLevel: 1,

// We expire stale breakpoints on the server.
breakpointExpirationSec: 60 * 60 * 24, // 24 hours
// How frequently should the list of breakpoints be refreshed from the
// cloud debug server.
breakpointUpdateIntervalSec: 10,

capture: {
// Whether to include details about stack frames belonging to node-core.
includeNodeModules: false,
// We expire stale breakpoints on the server.
breakpointExpirationSec: 60 * 60 * 24, // 24 hours

// Maximum number of stack frames to capture data for. The limit is aimed
// to reduce overall capture time
maxFrames: 20,
capture: {
// Whether to include details about stack frames belonging to node-core.
includeNodeModules: false,

// Only collect locals and arguments on a few top frames. For the rest
// only collect the source location
maxExpandFrames: 5,
// Maximum number of stack frames to capture data for. The limit is aimed
// to reduce overall capture time
maxFrames: 20,

// To reduce the overall capture time, limit the number of properties
// gathered on large object. A value of 0 disables the limit.
maxProperties: 0,
// Only collect locals and arguments on a few top frames. For the rest
// only collect the source location
maxExpandFrames: 5,

// Total 'size' of data to gather. This is NOT the number of bytes of data
// that are sent over the wire, but instead a very very coarse approximation
// based on the length of names and values of the properties. This should
// be somewhat proportional to the amount of processing needed to capture
// the data and subsequently the network traffic. A value of 0 disables the
// limit.
maxDataSize: 20000,
// To reduce the overall capture time, limit the number of properties
// gathered on large object. A value of 0 disables the limit.
maxProperties: 0,

// To limit the size of the buffer, we truncate long strings.
// A value of 0 disables truncation.
maxStringLength: 0
},
// Total 'size' of data to gather. This is NOT the number of bytes of data
// that are sent over the wire, but instead a very very coarse approximation
// based on the length of names and values of the properties. This should
// be somewhat proportional to the amount of processing needed to capture
// the data and subsequently the network traffic. A value of 0 disables the
// limit.
maxDataSize: 20000,

// These configuration options are for internal experimentation only.
internal: {
registerDelayOnFetcherErrorSec: 300 // 5 minutes.
}
// To limit the size of the buffer, we truncate long strings.
// A value of 0 disables truncation.
maxStringLength: 0
},

// These configuration options are for internal experimentation only.
internal: {
registerDelayOnFetcherErrorSec: 300 // 5 minutes.
}
}
};
37 changes: 26 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,37 @@
// NOTE: this file is on the critical path for the startup of the user's
// application. The path-length here needs to be minimal.

var config = require('./config.js');
var logger = require('@google/cloud-diagnostics-common').logger;
var Debuglet = require('./lib/debuglet.js');
var path = require('path');
var _ = require('lodash');

var initConfig = function() {
var config = {};
if (process.env.hasOwnProperty('GCLOUD_DIAGNOSTICS_CONFIG')) {
var c = require(path.resolve(process.env.GCLOUD_DIAGNOSTICS_CONFIG));
if (c && c.debug) {
_.defaultsDeep(config, c.debug);
}
}
var defaults = require('./config.js').debug;
_.defaultsDeep(config, defaults);
if (process.env.hasOwnProperty('GCLOUD_DEBUG_LOGLEVEL')) {
config.logLevel = process.env.GCLOUD_DEBUG_LOGLEVEL;
}
if (process.env.hasOwnProperty('GCLOUD_DEBUG_DISABLE')) {
config.enabled = false;
}
if (process.env.hasOwnProperty('GCLOUD_DEBUG_REPO_APP_PATH')) {
config.appPathRelativeToRepository =
process.env.GCLOUD_DEBUG_REPO_APP_PATH;
}
return config;
};

// exports is populated by the agent
module.exports = {};
if (process.env.hasOwnProperty('GCLOUD_DEBUG_LOGLEVEL')) {
config.logLevel = process.env.GCLOUD_DEBUG_LOGLEVEL;
}
if (process.env.hasOwnProperty('GCLOUD_DEBUG_DISABLE')) {
config.enabled = false;
}
if (process.env.hasOwnProperty('GCLOUD_DEBUG_REPO_APP_PATH')) {
config.appPathRelativeToRepository =
process.env.GCLOUD_DEBUG_REPO_APP_PATH;
}
var config = initConfig();

var log = logger.create(config.logLevel, '@google/cloud-debug');

Expand Down
2 changes: 1 addition & 1 deletion lib/debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function Debuglet(config, logger) {
this.logger_ = logger;

/** @private {DebugletApi} */
this.debugletApi_ = new DebugletApi();
this.debugletApi_ = new DebugletApi(config.description);

/** @private {Object.<string, Breakpoint>} */
this.activeBreakpointMap_ = {};
Expand Down
9 changes: 8 additions & 1 deletion lib/debugletapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var SCOPES = [
/**
* @constructor
*/
function DebugletApi() {
function DebugletApi(descriptor) {
/** @private {Object} request style request object */
this.request_ = utils.authorizedRequestFactory(SCOPES);

Expand All @@ -50,6 +50,9 @@ function DebugletApi() {

/** @private {string} debuggee id provided by the server once registered */
this.debuggeeId_ = null;

/** @private {string} a descriptor of the current code version */
this.descriptor_ = descriptor;
}

/**
Expand Down Expand Up @@ -151,6 +154,10 @@ DebugletApi.prototype.register_ = function(errorMessage, callback) {
}
}

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

if (process.env.GAE_MINOR_VERSION) {
labels[DEBUGGEE_MINOR_VERSION_LABEL] = process.env.GAE_MINOR_VERSION;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"coffee-script": "^1.9.3",
"findit": "^2.0.0",
"google-auth-library": "^0.9.5",
"lodash": "^4.12.0",
"request": "^2.61.0",
"semver": "^5.1.0",
"source-map": "^0.5.1",
Expand Down
39 changes: 39 additions & 0 deletions test/fixtures/test-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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';

// Default configuration
module.exports = {
debug: {
enabled: true,

// An identifier for the current code deployment.
description: 'test config',

// Log levels: 0-disabled,1-error,2-warn,3-info,4-debug.
logLevel: 4,

capture: {
// Whether to include details about stack frames belonging to node-core.
includeNodeModules: true,
},

// These configuration options are for internal experimentation only.
internal: {
registerDelayOnFetcherErrorSec: 300 // 5 minutes.
}
}
};
2 changes: 1 addition & 1 deletion test/standalone/test-debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var assert = require('assert');
var request = require('request');
var logger = require('@google/cloud-diagnostics-common').logger;
var config = require('../../config.js');
var config = require('../../config.js').debug;
var Debuglet = require('../../lib/debuglet.js');

var DEBUGGEE_ID = 'bar';
Expand Down
2 changes: 1 addition & 1 deletion test/standalone/test-duplicate-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var breakpointInFoo = {
var assert = require('assert');
var v8debugapi = require('../../lib/v8debugapi.js');
var logModule = require('@google/cloud-diagnostics-common').logger;
var config = require('../../config.js');
var config = require('../../config.js').debug;
var scanner = require('../../lib/scanner.js');
var path = require('path');

Expand Down
42 changes: 42 additions & 0 deletions test/standalone/test-env-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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';

process.env.GCLOUD_DIAGNOSTICS_CONFIG = 'test/fixtures/test-config.js';
process.env.GCLOUD_DEBUG_LOGLEVEL = 4;

var assert = require('assert');

describe('should respect environment variables', function() {
it('should respect GCLOUD_DIAGNOSTICS_CONFIG', function() {
var agent = require('../..');
var config = agent.private_.config_;
// Set by env var
assert.equal(config.logLevel, 4);
// Set default + user config
assert.equal(config.internal.registerDelayOnFetcherErrorSec, 300);
// Set by user
assert.equal(config.capture.includeNodeModules, true);
// In sub config but not set by user
assert.equal(config.capture.maxExpandFrames, 5);
// In top level config set by user
assert.equal(config.description, 'test config');
// In top level config not set by user
assert.equal(config.breakpointUpdateIntervalSec, 10);
});

});
2 changes: 1 addition & 1 deletion test/standalone/test-max-data-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var assert = require('assert');
var logModule = require('@google/cloud-diagnostics-common').logger;
var v8debugapi = require('../../lib/v8debugapi.js');
var scanner = require('../../lib/scanner.js');
var config = require('../../config.js');
var config = require('../../config.js').debug;
var api;

var breakpointInFoo = {
Expand Down
6 changes: 5 additions & 1 deletion test/test-debugletapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ nock.disableNetConnect();

describe('Debuglet API', function() {

var debugletapi = new DebugletApi();
var debugletapi = new DebugletApi('Test debuglet');

it('should return an instance when constructed', function() {
assert.ok(debugletapi);
});

it('should have correct descriptor', function() {
assert.equal(debugletapi.descriptor_, 'Test debuglet');
});

it('should acquire the project number during init', function(done) {
debugletapi.init('uid123', { warn: function() {} }, function(err) {
assert(!err);
Expand Down
2 changes: 1 addition & 1 deletion test/test-v8debugapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var MAX_INT = 2147483647; // Max signed int32.
var assert = require('assert');
var v8debugapi = require('../lib/v8debugapi.js');
var logModule = require('@google/cloud-diagnostics-common').logger;
var config = require('../config.js');
var config = require('../config.js').debug;
var StatusMessage = require('../lib/apiclasses.js').StatusMessage;
var scanner = require('../lib/scanner.js');
var path = require('path');
Expand Down

0 comments on commit c7e1572

Please sign in to comment.