Skip to content

Commit

Permalink
Add DD_ prefix from Datadog APM with instructions (DataDog#373)
Browse files Browse the repository at this point in the history
* Add DD_ prefix from APM with instructions

Adds the DD_ prefix we have from APM, and adds explaination how to use environment variables in datadogpy

* Fix spaces in whitespace for flake8

* remove whitespace

* remove comma
  • Loading branch information
burningion authored and David Bouchare committed Oct 25, 2019
1 parent 798d2df commit 66a6e09
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ stats.increment('home.page.hits')

```

Environment Variables
---------------------

As an alternate method to using the `initialize` function with the `options` parameters, set the environment variables `DATADOG_API_KEY` and `DATADOG_APP_KEY` within the context of your application.

If `DATADOG_API_KEY` or `DATADOG_APP_KEY` are not set, the library will attempt to fall back to Datadog's APM environmnent variable prefixes: `DD_API_KEY` and `DD_APP_KEY`.

```python
from datadog import initialize, api

# Assuming you've set `DD_API_KEY` and `DD_APP_KEY` in your env,
# initialize() will pick it up automatically
initialize()

title = "Something big happened!"
text = 'And let me tell you all about it here!'
tags = ['version:1', 'application:web']

api.Event.create(title=title, text=text, tags=tags)
```

Thread Safety
-------------
`DogStatsD` and `ThreadStats` are thread-safe.
Expand Down
8 changes: 6 additions & 2 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
:type mute: boolean
"""
# API configuration
api._api_key = api_key or api._api_key or os.environ.get('DATADOG_API_KEY')
api._application_key = app_key or api._application_key or os.environ.get('DATADOG_APP_KEY')
api._api_key = api_key or api._api_key or os.environ.get('DATADOG_API_KEY', os.environ.get('DD_API_KEY'))
api._application_key = (
app_key or
api._application_key or
os.environ.get('DATADOG_APP_KEY', os.environ.get('DD_APP_KEY'))
)
api._host_name = host_name or api._host_name or get_hostname()
api._api_host = api_host or api._api_host or os.environ.get('DATADOG_HOST', 'https://api.datadoghq.com')

Expand Down
12 changes: 6 additions & 6 deletions datadog/dogshell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def main():
parser.add_argument('--config', help="location of your dogrc file (default ~/.dogrc)",
default=os.path.expanduser('~/.dogrc'))
parser.add_argument('--api-key', help="your API key, from "
"https://app.datadoghq.com/account/settings#api "
"You can also set the environment variable DATADOG_API_KEY",
dest='api_key', default=os.environ.get('DATADOG_API_KEY'))
"https://app.datadoghq.com/account/settings#api. "
"You can also set the environment variables DATADOG_API_KEY or DD_API_KEY",
dest='api_key', default=os.environ.get('DATADOG_API_KEY', os.environ.get('DD_API_KEY')))
parser.add_argument('--application-key', help="your Application key, from "
"https://app.datadoghq.com/account/settings#api "
"You can also set the environment variable DATADOG_APP_KEY",
dest='app_key', default=os.environ.get('DATADOG_APP_KEY'))
"https://app.datadoghq.com/account/settings#api. "
"You can also set the environment variables DATADOG_APP_KEY or DD_APP_KEY",
dest='app_key', default=os.environ.get('DATADOG_APP_KEY', os.environ.get('DD_APP_KEY')))
parser.add_argument('--pretty', help="pretty-print output (suitable for human consumption, "
"less useful for scripting)", dest='format',
action='store_const', const='pretty')
Expand Down
2 changes: 1 addition & 1 deletion datadog/threadstats/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _enter(cls):
with cls._counter_lock:
if not cls._was_initialized:
cls._was_initialized = True
api._api_key = os.environ.get('DATADOG_API_KEY')
api._api_key = os.environ.get('DATADOG_API_KEY', os.environ.get('DD_API_KEY'))
api._api_host = os.environ.get('DATADOG_HOST', 'https://api.datadoghq.com')

# Async initialization of the TLS connection with our endpoints
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ def test_env_var_values(self):
del os.environ["DATADOG_APP_KEY"]
del os.environ["DATADOG_HOST"]

os.environ["DD_API_KEY"] = "API_KEY_ENV_DD"
os.environ["DD_APP_KEY"] = "APP_KEY_ENV_DD"
api._api_key = None
api._application_key = None

initialize()

self.assertEqual(api._api_key, "API_KEY_ENV_DD")
self.assertEqual(api._application_key, "APP_KEY_ENV_DD")

def test_function_param_value(self):
initialize(api_key="API_KEY", app_key="APP_KEY", api_host="HOST", host_name="HOSTNAME")

Expand Down

0 comments on commit 66a6e09

Please sign in to comment.