File tree Expand file tree Collapse file tree 2 files changed +10
-30
lines changed Expand file tree Collapse file tree 2 files changed +10
-30
lines changed Original file line number Diff line number Diff line change 1
1
import logging
2
2
from os import path
3
3
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
-
13
4
AGENT_URL = "http://127.0.0.1:8124"
14
- HELLO_PATH = "/lambda/hello"
15
5
FLUSH_PATH = "/lambda/flush"
16
6
EXTENSION_PATH = "/opt/extensions/datadog-agent"
17
7
18
8
logger = logging .getLogger (__name__ )
19
9
20
10
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
30
15
31
16
32
17
def flush_extension ():
33
18
try :
19
+ import urllib .request
20
+
34
21
req = urllib .request .Request (AGENT_URL + FLUSH_PATH , "" .encode ("ascii" ))
35
22
urllib .request .urlopen (req )
36
23
except Exception as e :
@@ -39,4 +26,4 @@ def flush_extension():
39
26
return True
40
27
41
28
42
- should_use_extension = is_extension_running ()
29
+ should_use_extension = is_extension_present ()
Original file line number Diff line number Diff line change 6
6
from unittest .mock import patch
7
7
8
8
from datadog_lambda .extension import (
9
- is_extension_running ,
9
+ is_extension_present ,
10
10
flush_extension ,
11
11
should_use_extension ,
12
12
)
@@ -48,19 +48,12 @@ def tearDown(self):
48
48
49
49
@patch ("datadog_lambda.extension.EXTENSION_PATH" , os .path .abspath (__file__ ))
50
50
def test_is_extension_running_true (self ):
51
- assert is_extension_running ()
52
- assert self .server .called
51
+ assert is_extension_present ()
53
52
54
53
def test_is_extension_running_file_not_found (self ):
55
- assert not is_extension_running ()
54
+ assert not is_extension_present ()
56
55
assert not self .server .called
57
56
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
-
64
57
@patch ("datadog_lambda.extension.EXTENSION_PATH" , os .path .abspath (__file__ ))
65
58
def test_flush_ok (self ):
66
59
assert flush_extension ()
You can’t perform that action at this time.
0 commit comments