diff --git a/lib/stackdriver.js b/lib/stackdriver.js index cb74f28..b1eb766 100644 --- a/lib/stackdriver.js +++ b/lib/stackdriver.js @@ -81,6 +81,21 @@ function StackdriverBackend(startupTime, config, emitter) { if (this.sourceFromPrefix) { util.log('Source will be inferred from each metric prefix, separated by "' + this.sourcePrefixSeparator + '", all points sent to Stackdriver for that metric will be associated with that instance'); } else if (this.source) { + var sys = require('sys'); + var exec = require('child_process').exec; + + if (this.source == 'detect-aws') { + // Amazon EC2 instance - connect to local metadata service to grab instance ID + var child = exec('wget -q -O - http://169.254.169.254/latest/meta-data/instance-id', function(error, stdout, stdin) { + this.source = stdout; + }); + } else if (this.source == 'detect-gce') { + // Google Compute Engine instance - connect to local metadata service to grab instance ID + var child = exec('wget -q -O - http://metadata/computeMetadata/v1/instance/id --header "X-Google-Metadata-Request: true"', function(error, stdout, stdin) { + this.source = stdout; + }); + } + util.log('Source param set to ' + this.source + ', all points sent to Stackdriver will be associated with that instance'); }