Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/agent/debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Debuglet.normalizeConfig_ = function(config) {
var envConfig = {
logLevel: process.env.GCLOUD_DEBUG_LOGLEVEL,
serviceContext: {
// If running on GKE, we will set service context later once
// the cluster name can be retrieved from the metadata service.
service: process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME,
version: process.env.GAE_VERSION || process.env.GAE_MODULE_VERSION,
// Debug UI expects GAE_MINOR_VERSION to be available for AppEngine, but
Expand Down Expand Up @@ -206,14 +208,23 @@ Debuglet.prototype.start = function() {

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

that.getProjectId_(function(err, project, onGCP) {
that.getMetadataValues_(function(err, values) {
if (err) {
that.logger_.error('Unable to discover projectId. Please provide ' +
'the projectId to be able to use the Debuglet',
err);
that.emit('initError', err);
return;
}
var project = values.project;
var clusterName = values.clusterName;
var onGCP = values.onGCP;

if (clusterName) {
// If we succeeded to get a cluster name, we must be running on gke.
that.config_.serviceContext.service = clusterName;
that.config_.serviceContext.version = 'unversioned';
}

that.getSourceContext_(function(err, sourceContext) {
if (err) {
Expand Down Expand Up @@ -319,7 +330,7 @@ Debuglet.createDebuggee =
/**
* @private
*/
Debuglet.prototype.getProjectId_ = function(callback) {
Debuglet.prototype.getMetadataValues_ = function(callback) {
var that = this;

// We need to figure out whether we are running on GCP. We can use our ability
Expand All @@ -339,7 +350,11 @@ Debuglet.prototype.getProjectId_ = function(callback) {
if (!project) {
return callback(err);
}
return callback(null, project, onGCP);
metadata.instance(
'attributes/cluster-name', function(err, response, metadataCluster) {
return callback(null,
{ project: project, clusterName: metadataCluster, onGCP: onGCP});
});
});
};

Expand Down
22 changes: 22 additions & 0 deletions test/test-debuglet.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,28 @@ describe('Debuglet', function() {
assert.ok(_.isString(debuglet.config_.serviceContext.minorVersion_));
});

it('should use GKE cluster name if available', function(done) {
var scope = nock('http://metadata.google.internal')
.get('/computeMetadata/v1/instance/attributes/cluster-name')
.once()
.reply(200, 'my-cool-cluster');
var debug = require('../src/debug.js')(
{projectId: 'fake-project', credentials: fakeCredentials});
var debuglet = new Debuglet(debug, defaultConfig);
debuglet.once('started', function(id) {
debuglet.stop();
assert.ok(debuglet.config_);
assert.ok(debuglet.config_.serviceContext);
assert.strictEqual(debuglet.config_.serviceContext.service,
'my-cool-cluster');
assert.strictEqual(debuglet.config_.serviceContext.version,
'unversioned');
scope.done();
done();
});
debuglet.start();
});

it('should not have minorVersion unless enviroment provides it',
function() {
var debug = require('../src/debug.js')();
Expand Down