Skip to content

Commit 0cf9c56

Browse files
authored
feat: Remove http check for extension hello route (#422)
* feat: Remove http check for extension hello route * feat: lint
1 parent a06668d commit 0cf9c56

File tree

2 files changed

+10
-30
lines changed

2 files changed

+10
-30
lines changed

datadog_lambda/extension.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
import logging
22
from os import path
33

4-
try:
5-
# only available in python 3
6-
# not an issue since the extension is not compatible with python 2.x runtime
7-
# https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html
8-
import urllib.request
9-
except ImportError:
10-
# safe since both calls to urllib are protected with try/expect and will return false
11-
urllib = None
12-
134
AGENT_URL = "http://127.0.0.1:8124"
14-
HELLO_PATH = "/lambda/hello"
155
FLUSH_PATH = "/lambda/flush"
166
EXTENSION_PATH = "/opt/extensions/datadog-agent"
177

188
logger = logging.getLogger(__name__)
199

2010

21-
def is_extension_running():
22-
if not path.exists(EXTENSION_PATH):
23-
return False
24-
try:
25-
urllib.request.urlopen(AGENT_URL + HELLO_PATH)
26-
except Exception as e:
27-
logger.debug("Extension is not running, returned with error %s", e)
28-
return False
29-
return True
11+
def is_extension_present():
12+
if path.exists(EXTENSION_PATH):
13+
return True
14+
return False
3015

3116

3217
def flush_extension():
3318
try:
19+
import urllib.request
20+
3421
req = urllib.request.Request(AGENT_URL + FLUSH_PATH, "".encode("ascii"))
3522
urllib.request.urlopen(req)
3623
except Exception as e:
@@ -39,4 +26,4 @@ def flush_extension():
3926
return True
4027

4128

42-
should_use_extension = is_extension_running()
29+
should_use_extension = is_extension_present()

tests/test_extension.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from unittest.mock import patch
77

88
from datadog_lambda.extension import (
9-
is_extension_running,
9+
is_extension_present,
1010
flush_extension,
1111
should_use_extension,
1212
)
@@ -48,19 +48,12 @@ def tearDown(self):
4848

4949
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
5050
def test_is_extension_running_true(self):
51-
assert is_extension_running()
52-
assert self.server.called
51+
assert is_extension_present()
5352

5453
def test_is_extension_running_file_not_found(self):
55-
assert not is_extension_running()
54+
assert not is_extension_present()
5655
assert not self.server.called
5756

58-
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
59-
def test_is_extension_running_http_failure(self):
60-
self.server.raises = True
61-
assert not is_extension_running()
62-
assert self.server.called
63-
6457
@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
6558
def test_flush_ok(self):
6659
assert flush_extension()

0 commit comments

Comments
 (0)