@@ -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
0 commit comments