Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: log EC2 instance ID when using AWS for hostname
Browse files Browse the repository at this point in the history
Closes #931
  • Loading branch information
bbangert committed Jun 23, 2017
1 parent 465551d commit d8efd5e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions autopush/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
EndpointHTTPFactory,
MemUsageHTTPFactory
)
import autopush.utils as utils
import autopush.logging as logging
from autopush.exceptions import InvalidSettings
from autopush.db import DatabaseManager
from autopush.haproxy import HAProxyServerEndpoint
Expand Down Expand Up @@ -118,6 +120,8 @@ def main(cls, args=None, use_files=True):
"""
ns = cls.parse_args(cls.config_files if use_files else [], args)
if not ns.no_aws:
logging.HOSTNAME = utils.get_ec2_instance_id()
PushLogger.setup_logging(
cls.logger_name,
log_level=ns.log_level or ("debug" if ns.debug else "info"),
Expand Down
5 changes: 4 additions & 1 deletion autopush/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import datadog
from datadog import ThreadStats

from autopush.utils import get_ec2_instance_id

if TYPE_CHECKING: # pragma: nocover
from autopush.settings import AutopushSettings # noqa

Expand Down Expand Up @@ -109,7 +111,8 @@ def from_settings(settings):
"""Create an IMetrics from the given settings"""
if settings.datadog_api_key:
return DatadogMetrics(
hostname=settings.hostname,
hostname=get_ec2_instance_id() if settings.ami_id else
settings.hostname,
api_key=settings.datadog_api_key,
app_key=settings.datadog_app_key,
flush_interval=settings.datadog_flush_interval,
Expand Down
20 changes: 20 additions & 0 deletions autopush/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

from mock import patch, Mock
from nose.tools import eq_


Expand Down Expand Up @@ -29,3 +30,22 @@ def test_other_os_and_browser(self):
eq_(raw["ua_os_family"], "BlackBerry OS")
eq_(dd["ua_browser_family"], "Other")
eq_(raw["ua_browser_family"], "BlackBerry")

@patch("requests.get")
def test_get_ec2_instance_id_unknown(self, request_mock):
import requests
from autopush.utils import get_ec2_instance_id

request_mock.side_effect = requests.HTTPError
result = get_ec2_instance_id()
eq_(result, "Unknown")

@patch("requests.get")
def test_get_ec2_instance_id(self, request_mock):
from autopush.utils import get_ec2_instance_id
mock_reply = Mock()
mock_reply.content = "i-123242"

request_mock.return_value = mock_reply
result = get_ec2_instance_id()
eq_(result, "i-123242")
14 changes: 14 additions & 0 deletions autopush/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ def get_amid():
return "Unknown"


def get_ec2_instance_id():
# type: () -> str
"""Fetch the EC2 instance-id
"""
try:
resp = requests.get(
"http://169.254.169.254/latest/meta-data/instance-id",
timeout=1)
return resp.content
except (requests.HTTPError, requests.ConnectionError):
return "Unknown"


def decipher_public_key(key_data):
# type: (str) -> str
"""A public key may come in several flavors. Attempt to extract the
Expand Down

0 comments on commit d8efd5e

Please sign in to comment.