Skip to content

feat: Remove http check for extension hello route #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions datadog_lambda/extension.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import logging
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"
EXTENSION_PATH = "/opt/extensions/datadog-agent"

logger = logging.getLogger(__name__)


def is_extension_running():
if not path.exists(EXTENSION_PATH):
return False
try:
urllib.request.urlopen(AGENT_URL + HELLO_PATH)
except Exception as e:
logger.debug("Extension is not running, returned with error %s", e)
return False
return True
def is_extension_present():
if path.exists(EXTENSION_PATH):
return True
return False


def flush_extension():
try:
import urllib.request

req = urllib.request.Request(AGENT_URL + FLUSH_PATH, "".encode("ascii"))
urllib.request.urlopen(req)
except Exception as e:
Expand All @@ -39,4 +26,4 @@ def flush_extension():
return True


should_use_extension = is_extension_running()
should_use_extension = is_extension_present()
13 changes: 3 additions & 10 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from unittest.mock import patch

from datadog_lambda.extension import (
is_extension_running,
is_extension_present,
flush_extension,
should_use_extension,
)
Expand Down Expand Up @@ -48,19 +48,12 @@ def tearDown(self):

@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
def test_is_extension_running_true(self):
assert is_extension_running()
assert self.server.called
assert is_extension_present()

def test_is_extension_running_file_not_found(self):
assert not is_extension_running()
assert not is_extension_present()
assert not self.server.called

@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
def test_is_extension_running_http_failure(self):
self.server.raises = True
assert not is_extension_running()
assert self.server.called

@patch("datadog_lambda.extension.EXTENSION_PATH", os.path.abspath(__file__))
def test_flush_ok(self):
assert flush_extension()
Expand Down