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
- AGENT_URL = "http://127.0.0.1:8124"
14
4
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
11
+ try :
12
+ import http .client
13
+
14
+ conn = http .client .HTTPConnection ("127.0.0.1" , 8124 )
15
+ except Exception as e :
16
+ logger .debug ("unable to create http connection to extension: " , e )
17
+ conn = None
18
+
19
+
21
20
def is_extension_running ():
22
21
if not path .exists (EXTENSION_PATH ):
23
22
return False
24
23
try :
25
- urllib .request .urlopen (AGENT_URL + HELLO_PATH )
24
+ conn .request ("GET" , HELLO_PATH )
25
+ resp = conn .getresponse ()
26
+ return resp .status == 200
26
27
except Exception as e :
27
28
logger .debug ("Extension is not running, returned with error %s" , e )
28
29
return False
@@ -31,8 +32,9 @@ def is_extension_running():
31
32
32
33
def flush_extension ():
33
34
try :
34
- req = urllib .request .Request (AGENT_URL + FLUSH_PATH , "" .encode ("ascii" ))
35
- urllib .request .urlopen (req )
35
+ conn .request ("POST" , FLUSH_PATH , b"" )
36
+ resp = conn .getresponse ()
37
+ return resp .status == 200
36
38
except Exception as e :
37
39
logger .debug ("Failed to flush extension, returned with error %s" , e )
38
40
return False
0 commit comments