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

Commit d3994f8

Browse files
Use service name as debuggee id on gke (#275)
PR-URL: #275
1 parent a87007a commit d3994f8

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/agent/debuglet.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Debuglet.normalizeConfig_ = function(config) {
146146
var envConfig = {
147147
logLevel: process.env.GCLOUD_DEBUG_LOGLEVEL,
148148
serviceContext: {
149+
// If running on GKE, we will set service context later once
150+
// the cluster name can be retrieved from the metadata service.
149151
service: process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME,
150152
version: process.env.GAE_VERSION || process.env.GAE_MODULE_VERSION,
151153
// Debug UI expects GAE_MINOR_VERSION to be available for AppEngine, but
@@ -206,14 +208,23 @@ Debuglet.prototype.start = function() {
206208

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

209-
that.getProjectId_(function(err, project, onGCP) {
211+
that.getMetadataValues_(function(err, values) {
210212
if (err) {
211213
that.logger_.error('Unable to discover projectId. Please provide ' +
212214
'the projectId to be able to use the Debuglet',
213215
err);
214216
that.emit('initError', err);
215217
return;
216218
}
219+
var project = values.project;
220+
var clusterName = values.clusterName;
221+
var onGCP = values.onGCP;
222+
223+
if (clusterName) {
224+
// If we succeeded to get a cluster name, we must be running on gke.
225+
that.config_.serviceContext.service = clusterName;
226+
that.config_.serviceContext.version = 'unversioned';
227+
}
217228

218229
that.getSourceContext_(function(err, sourceContext) {
219230
if (err) {
@@ -319,7 +330,7 @@ Debuglet.createDebuggee =
319330
/**
320331
* @private
321332
*/
322-
Debuglet.prototype.getProjectId_ = function(callback) {
333+
Debuglet.prototype.getMetadataValues_ = function(callback) {
323334
var that = this;
324335

325336
// We need to figure out whether we are running on GCP. We can use our ability
@@ -339,7 +350,11 @@ Debuglet.prototype.getProjectId_ = function(callback) {
339350
if (!project) {
340351
return callback(err);
341352
}
342-
return callback(null, project, onGCP);
353+
metadata.instance(
354+
'attributes/cluster-name', function(err, response, metadataCluster) {
355+
return callback(null,
356+
{ project: project, clusterName: metadataCluster, onGCP: onGCP});
357+
});
343358
});
344359
};
345360

test/test-debuglet.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,28 @@ describe('Debuglet', function() {
295295
assert.ok(_.isString(debuglet.config_.serviceContext.minorVersion_));
296296
});
297297

298+
it('should use GKE cluster name if available', function(done) {
299+
var scope = nock('http://metadata.google.internal')
300+
.get('/computeMetadata/v1/instance/attributes/cluster-name')
301+
.once()
302+
.reply(200, 'my-cool-cluster');
303+
var debug = require('../src/debug.js')(
304+
{projectId: 'fake-project', credentials: fakeCredentials});
305+
var debuglet = new Debuglet(debug, defaultConfig);
306+
debuglet.once('started', function(id) {
307+
debuglet.stop();
308+
assert.ok(debuglet.config_);
309+
assert.ok(debuglet.config_.serviceContext);
310+
assert.strictEqual(debuglet.config_.serviceContext.service,
311+
'my-cool-cluster');
312+
assert.strictEqual(debuglet.config_.serviceContext.version,
313+
'unversioned');
314+
scope.done();
315+
done();
316+
});
317+
debuglet.start();
318+
});
319+
298320
it('should not have minorVersion unless enviroment provides it',
299321
function() {
300322
var debug = require('../src/debug.js')();

0 commit comments

Comments
 (0)