Skip to content

Commit

Permalink
Let the possibility to disable instance metadata collection
Browse files Browse the repository at this point in the history
Fix #975
  • Loading branch information
remh committed Jun 19, 2014
1 parent 344d97d commit 81540f7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def _set_agent_config_hostname(self, agentConfig):
# in the config file.
# DEPRECATED
if agentConfig.get('hostname') is None and agentConfig.get('use_ec2_instance_id'):
instanceId = EC2.get_instance_id()
instanceId = EC2.get_instance_id(agentConfig)
if instanceId is not None:
log.info("Running on EC2, instanceId: %s" % instanceId)
agentConfig['hostname'] = instanceId
Expand Down
6 changes: 3 additions & 3 deletions checks/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,12 @@ def _build_payload(self, start_event=True):
host_tags.extend([unicode(tag.strip()) for tag in self.agentConfig['tags'].split(",")])

if self.agentConfig['collect_ec2_tags']:
host_tags.extend(EC2.get_tags())
host_tags.extend(EC2.get_tags(self.agentConfig))

if host_tags:
payload['host-tags']['system'] = host_tags

GCE_tags = GCE.get_tags()
GCE_tags = GCE.get_tags(self.agentConfig)
if GCE_tags is not None:
payload['host-tags'][GCE.SOURCE_TYPE_NAME] = GCE_tags

Expand All @@ -438,7 +438,7 @@ def _build_payload(self, start_event=True):
return payload

def _get_metadata(self):
metadata = EC2.get_metadata()
metadata = EC2.get_metadata(self.agentConfig)
if metadata.get('hostname'):
metadata['ec2-hostname'] = metadata.get('hostname')
del metadata['hostname']
Expand Down
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ def get_config(parse_args=True, cfg_path=None, options=None):
if config.has_option("Main", "skip_ssl_validation"):
agentConfig["skip_ssl_validation"] = _is_affirmative(config.get("Main", "skip_ssl_validation"))

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

agentConfig["collect_ec2_tags"] = False
if config.has_option("Main", "collect_ec2_tags"):
agentConfig["collect_ec2_tags"] = _is_affirmative(config.get("Main", "collect_ec2_tags"))
Expand Down
8 changes: 8 additions & 0 deletions datadog.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ api_key:
# Collect AWS EC2 custom tags as agent tags
# collect_ec2_tags: no

# Collect instance metadata
# The Agent will try to collect instance metadata for EC2 and GCE instances by
# trying to connect to the local endpoint: http://169.254.169.254
# See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
# and https://developers.google.com/compute/docs/metadata
# for more information
# collect_instance_metadata: 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ api_key: APIKEYHERE
# Certificate file.
# ca_certs = datadog-cert.pem

# Collect instance metadata
# The Agent will try to collect instance metadata for EC2 and GCE instances by
# trying to connect to the local endpoint: http://169.254.169.254
# See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
# and https://developers.google.com/compute/docs/metadata
# for more information
# collect_instance_metadata: yes

# ========================================================================== #
# Information page configuration
# ========================================================================== #
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_metadata(self):
if os.environ.get('TRAVIS', False): return
# Test gathering metadata from ec2
start = time.time()
d = EC2.get_metadata()
d = EC2.get_metadata({'collect_instance_metadata': True})
end = time.time()
assert type(d) == types.DictType
# Either we're on ec2 or we're not (at least 7 attributes expected)
Expand Down
35 changes: 24 additions & 11 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def get_hostname(config=None):

#Try to get GCE instance name
if hostname is None:
gce_hostname = GCE.get_hostname()
gce_hostname = GCE.get_hostname(config)
if gce_hostname is not None:
if is_valid_hostname(gce_hostname):
return gce_hostname
Expand All @@ -222,7 +222,7 @@ def _get_hostname_unix():

# if we have an ec2 default hostname, see if there's an instance-id available
if hostname is not None and True in [hostname.lower().startswith(p) for p in [u'ip-', u'domu']]:
instanceid = EC2.get_instance_id()
instanceid = EC2.get_instance_id(config)
if instanceid:
hostname = instanceid

Expand All @@ -249,10 +249,14 @@ class GCE(object):


@staticmethod
def _get_metadata():
def _get_metadata(agentConfig):
if GCE.metadata is not None:
return GCE.metadata

if not agentConfig['collect_instance_metadata']:
GCE.metadata = {}
return GCE.metadata

socket_to = None
try:
socket_to = socket.getdefaulttimeout()
Expand All @@ -279,10 +283,12 @@ def _get_metadata():


@staticmethod
def get_tags():
def get_tags(agentConfig):
if not agentConfig['collect_instance_metadata']:
return []

try:
host_metadata = GCE._get_metadata()
host_metadata = GCE._get_metadata(agentConfig)
tags = []

for key, value in host_metadata['instance'].get('attributes', {}).iteritems():
Expand All @@ -303,9 +309,9 @@ def get_tags():
return None

@staticmethod
def get_hostname():
def get_hostname(agentConfig):
try:
host_metadata = GCE._get_metadata()
host_metadata = GCE._get_metadata(agentConfig)
return host_metadata['instance']['hostname'].split('.')[0]
except Exception:
return None
Expand All @@ -320,7 +326,10 @@ class EC2(object):
metadata = {}

@staticmethod
def get_tags():
def get_tags(agentConfig):
if not agentConfig['collect_instance_metadata']:
return []

socket_to = None
try:
socket_to = socket.getdefaulttimeout()
Expand Down Expand Up @@ -352,7 +361,7 @@ def get_tags():


@staticmethod
def get_metadata():
def get_metadata(agentConfig):
"""Use the ec2 http service to introspect the instance. This adds latency if not running on EC2
"""
# >>> import urllib2
Expand All @@ -366,6 +375,10 @@ def get_metadata():
# Every call may add TIMEOUT seconds in latency so don't abuse this call
# python 2.4 does not support an explicit timeout argument so force it here
# Rather than monkey-patching urllib2, just lower the timeout globally for these calls

if not agentConfig['collect_instance_metadata']:
return {}

socket_to = None
try:
socket_to = socket.getdefaulttimeout()
Expand All @@ -391,9 +404,9 @@ def get_metadata():
return EC2.metadata

@staticmethod
def get_instance_id():
def get_instance_id(agentConfig):
try:
return EC2.get_metadata().get("instance-id", None)
return EC2.get_metadata(agentConfig).get("instance-id", None)
except Exception:
return None

Expand Down

0 comments on commit 81540f7

Please sign in to comment.