Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sniff DATADOG_HOST environment variable to determine which API host to use #30

Merged
merged 1 commit into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* datadog.dogshell: a command-line tool, wrapping datadog.api, to interact with Datadog REST API.
"""
from pkg_resources import get_distribution, DistributionNotFound
import os
import os.path

from datadog import api
Expand All @@ -30,7 +31,7 @@
__version__ = _dist.version


def initialize(api_key=None, app_key=None, host_name=None, api_host="https://app.datadoghq.com",
def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
proxies=None, statsd_host=None, statsd_port=None):
"""
Initialize and configure Datadog.api and Datadog.statsd modules
Expand All @@ -57,7 +58,8 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host="https://app
api._api_key = api_key
api._application_key = app_key
api._host_name = host_name if host_name is not None else get_hostname()
api._api_host = api_host
api._api_host = api_host if api_host is not None else \
os.environ.get('DATADOG_HOST', 'https://app.datadoghq.com')
api._proxies = proxies

# Given statsd_host and statsd_port, overrides statsd instance
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/api/test_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# stdlib
from functools import wraps
import os
import tempfile
import time


# 3p
import mock
from nose.tools import assert_raises, assert_true, assert_false
Expand All @@ -26,6 +28,23 @@
HOST_NAME,
FAKE_PROXY)

def preserve_environ_datadog_host(func):
"""
Decorator to preserve the original environment value of DATADOG_HOST.
"""
@wraps(func)
def wrapper(*args, **kwds):
environ_api_host = os.environ.get('DATADOG_HOST')
try:
return func(*args, **kwds)
finally:
# restore the original environ value
if environ_api_host:
os.environ['DATADOG_HOST'] = environ_api_host
elif os.environ.get('DATADOG_HOST'):
del os.environ['DATADOG_HOST']

return wrapper

class TestInitialization(DatadogAPINoInitialization):

Expand Down Expand Up @@ -90,6 +109,25 @@ def test_request_parameters(self):
assert 'headers' in options
assert options['headers'] == {'Content-Type': 'application/json'}

@preserve_environ_datadog_host
def test_api_host_from_env(self):
os.environ['DATADOG_HOST'] = 'http://localhost'
initialize()
self.assertEquals(api._api_host, 'http://localhost')

@preserve_environ_datadog_host
def test_api_host_default(self):
if os.environ.get('DATADOG_HOST'):
del os.environ['DATADOG_HOST']
initialize()
self.assertEquals(api._api_host, 'https://app.datadoghq.com')

@preserve_environ_datadog_host
def test_api_host_param(self):
if os.environ.get('DATADOG_HOST'):
del os.environ['DATADOG_HOST']
initialize(api_host='http://localhost')
self.assertEquals(api._api_host, 'http://localhost')

class TestResources(DatadogAPIWithInitialization):

Expand Down