diff --git a/datadog_lambda/extension.py b/datadog_lambda/extension.py index c2087a5f..d66848ff 100644 --- a/datadog_lambda/extension.py +++ b/datadog_lambda/extension.py @@ -1,7 +1,15 @@ import logging -import requests from os import path +try: + # only available in python 3 + # not an issue since the extension is not compatible with python 2.x runtime + # https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html + import urllib.request +except ImportError: + # safe since both calls to urllib are protected with try/expect and will return false + urllib = None + AGENT_URL = "http://127.0.0.1:8124" HELLO_PATH = "/lambda/hello" FLUSH_PATH = "/lambda/flush" @@ -14,7 +22,7 @@ def is_extension_running(): if not path.exists(EXTENSION_PATH): return False try: - requests.get(AGENT_URL + HELLO_PATH) + urllib.request.urlopen(AGENT_URL + HELLO_PATH) except Exception as e: logger.debug("Extension is not running, returned with error %s", e) return False @@ -23,7 +31,8 @@ def is_extension_running(): def flush_extension(): try: - requests.post(AGENT_URL + FLUSH_PATH, data={}) + req = urllib.request.Request(AGENT_URL + FLUSH_PATH, "".encode("ascii")) + urllib.request.urlopen(req) except Exception as e: logger.debug("Failed to flush extension, returned with error %s", e) return False diff --git a/tests/test_extension.py b/tests/test_extension.py index e2128cce..2286e402 100644 --- a/tests/test_extension.py +++ b/tests/test_extension.py @@ -1,4 +1,5 @@ import os +import sys import unittest import httpretty @@ -19,6 +20,9 @@ def exceptionCallback(request, uri, headers): class TestLambdaExtension(unittest.TestCase): + # do not execute tests for Python v2.x + __test__ = sys.version_info >= (3, 0) + @patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__)) def test_is_extension_running_true(self): httpretty.enable()