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