Skip to content

Commit

Permalink
Merge pull request #1737 from DataDog/tristan/gce-hostname-fix
Browse files Browse the repository at this point in the history
[GCE] Update hostname to be unique
  • Loading branch information
tmichelet committed Jul 30, 2015
2 parents 7220d1c + 3111d51 commit bc62a7e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions checks/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@ def _get_hostname_metadata(self):

metadata["hostname"] = get_hostname()

# Add cloud provider aliases
host_aliases = GCE.get_host_aliases(self.agentConfig)
if host_aliases:
metadata['host_aliases'] = host_aliases

return metadata

def _should_send_additional_data(self, data_name):
Expand Down
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ def get_config(parse_args=True, cfg_path=None, options=None):
if config.has_option("Main", "utf8_decoding"):
agentConfig["utf8_decoding"] = _is_affirmative(config.get("Main", "utf8_decoding"))

agentConfig["gce_updated_hostname"] = False
if config.has_option("Main", "gce_updated_hostname"):
agentConfig["gce_updated_hostname"] = _is_affirmative(config.get("Main", "gce_updated_hostname"))

except ConfigParser.NoSectionError, e:
sys.stderr.write('Config file not found or incorrectly formatted.\n')
sys.exit(2)
Expand Down
3 changes: 3 additions & 0 deletions datadog.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ api_key:
# for more information
# collect_instance_metadata: yes

# use unique hostname for GCE hosts, see http://dtdg.co/1eAynZk
gce_updated_hostname: yes

# Set the threshold for accepting points to allow anything
# with recent_point_threshold seconds
# Defaults to 30 seconds if no value is provided
Expand Down
18 changes: 16 additions & 2 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class GCE(object):
TIMEOUT = 0.1 # second
SOURCE_TYPE_NAME = 'google cloud platform'
metadata = None
EXCLUDED_ATTRIBUTES = ["sshKeys"]
EXCLUDED_ATTRIBUTES = ["sshKeys", "user-data", "cli-cert", "ipsec-cert", "ssl-cert"]


@staticmethod
Expand Down Expand Up @@ -303,7 +303,21 @@ def get_tags(agentConfig):
def get_hostname(agentConfig):
try:
host_metadata = GCE._get_metadata(agentConfig)
return host_metadata['instance']['hostname'].split('.')[0]
hostname = host_metadata['instance']['hostname']
if agentConfig.get('gce_updated_hostname'):
return hostname
else:
return hostname.split('.')[0]
except Exception:
return None

@staticmethod
def get_host_aliases(agentConfig):
try:
host_metadata = GCE._get_metadata(agentConfig)
project_id = host_metadata['project']['projectId']
instance_name = host_metadata['instance']['hostname'].split('.')[0]
return ['%s.%s' % (instance_name, project_id)]
except Exception:
return None

Expand Down

0 comments on commit bc62a7e

Please sign in to comment.