Skip to content

Commit 25721d1

Browse files
author
Slavek Kabrda
authored
Provide better message on import errors to ease debugging (#65)
1 parent 9fcae36 commit 25721d1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

datadog_callback.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,35 @@
55
import os
66
import time
77

8+
IMPORT_ERROR = None
89
try:
910
import datadog
1011
import yaml
1112
from packaging import version
12-
HAS_MODULES = True
13-
except ImportError:
14-
HAS_MODULES = False
13+
except ImportError as e:
14+
IMPORT_ERROR = str(e)
1515

1616

1717
import ansible
1818
from ansible.plugins.callback import CallbackBase
1919
from __main__ import cli
2020

2121
ANSIBLE_ABOVE_28 = False
22-
if HAS_MODULES and version.parse(ansible.__version__) >= version.parse('2.8.0'):
22+
if IMPORT_ERROR is None and version.parse(ansible.__version__) >= version.parse('2.8.0'):
2323
ANSIBLE_ABOVE_28 = True
2424
from ansible.context import CLIARGS
2525

2626
DEFAULT_DD_URL = "https://api.datadoghq.com"
2727

2828
class CallbackModule(CallbackBase):
2929
def __init__(self):
30-
if not HAS_MODULES:
30+
if IMPORT_ERROR is not None:
3131
self.disabled = True
32-
print('Datadog callback disabled: missing "datadog", "yaml", and/or "packaging" python package.')
32+
print(
33+
'Datadog callback disabled because of a dependency problem: {}. '
34+
'Please install requirements with "pip install -r requirements.txt"'
35+
.format(IMPORT_ERROR)
36+
)
3337
else:
3438
self.disabled = False
3539
# Set logger level - datadog api and urllib3
@@ -39,7 +43,7 @@ def __init__(self):
3943
self._playbook_name = None
4044
self._start_time = time.time()
4145
self._options = None
42-
if HAS_MODULES and cli:
46+
if IMPORT_ERROR is None and cli:
4347
if ANSIBLE_ABOVE_28:
4448
self._options = CLIARGS
4549
else:

0 commit comments

Comments
 (0)