Skip to content

Commit

Permalink
[Backport 7.57.x] Switch gunicorn to stdlib subprocess (#18387)
Browse files Browse the repository at this point in the history
* Switch gunicorn to stdlib subprocess

* fix for mock to work

* add changelog

* remove extraneous changelog

* remove another extra changelog

* Correct arguments to subprocess.run, docs

(cherry picked from commit c33d1d6)

Co-authored-by: Ilia Kurenkov <[email protected]>
  • Loading branch information
1 parent 133e44d commit 35d3ac9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions gunicorn/changelog.d/18384.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch gunicorn to stdlib subprocess
15 changes: 13 additions & 2 deletions gunicorn/datadog_checks/gunicorn/gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@
http://gunicorn.org/
"""
import re
import subprocess
import time

import psutil

from datadog_checks.base import AgentCheck
from datadog_checks.base.utils.subprocess_output import get_subprocess_output


def get_gunicorn_version(cmd):
"""
Adapter around a subprocess call to gunicorn.
"""
# Splitting cmd by whitespace is "Good Enough"(tm):
# - shex.split is not available on Windows
# - passing shell=True exposes us to shell injection vulnerabilities since we get cmd from user config
res = subprocess.run(cmd.split(), capture_output=True, text=True)
return res.stdout, res.stderr, res.returncode


class GUnicornCheck(AgentCheck):
Expand Down Expand Up @@ -166,7 +177,7 @@ def _get_version(self):
"""Get version from `gunicorn --version`"""
cmd = '{} --version'.format(self.gunicorn_cmd)
try:
pc_out, pc_err, _ = get_subprocess_output(cmd, self.log, False)
pc_out, pc_err, _ = get_gunicorn_version(cmd)
except OSError:
self.log.debug("Error collecting gunicorn version.")
return None
Expand Down
4 changes: 0 additions & 4 deletions gunicorn/hatch.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
[env.collectors.datadog-checks]

[[envs.default.matrix]]
python = ["2.7"]
version = ["19.9"]

[[envs.default.matrix]]
python = ["3.11"]
version = ["19.9", "20.1"]
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_collect_metadata_parsing_matching(aggregator, datadog_agent, stdout, st
check = GUnicornCheck(CHECK_NAME, {}, [INSTANCE])
check.check_id = 'test:123'

with mock.patch('datadog_checks.gunicorn.gunicorn.get_subprocess_output', return_value=(stdout, stderr, 0)):
with mock.patch('datadog_checks.gunicorn.gunicorn.get_gunicorn_version', return_value=(stdout, stderr, 0)):
check.check(INSTANCE)

datadog_agent.assert_metadata_count(expect_metadata_count)

0 comments on commit 35d3ac9

Please sign in to comment.