Skip to content

Commit 00b2662

Browse files
authored
feat: add memory monitoring for prometheus (#479)
1 parent 8046783 commit 00b2662

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

.pre-commit-config.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exclude: |
1717
(LICENSE.*|COPYING.*)
1818
default_language_version:
1919
python: python3
20-
node: "14.18.0"
20+
node: "16.17.0"
2121
repos:
2222
- repo: local
2323
hooks:
@@ -34,14 +34,14 @@ repos:
3434
language: fail
3535
files: '[a-zA-Z0-9_]*/i18n/en\.po$'
3636
- repo: https://github.com/oca/maintainer-tools
37-
rev: dfba427ba03900b69e0a7f2c65890dc48921d36a
37+
rev: 4cd2b852214dead80822e93e6749b16f2785b2fe
3838
hooks:
3939
# update the NOT INSTALLABLE ADDONS section above
4040
- id: oca-update-pre-commit-excluded-addons
4141
- id: oca-fix-manifest-website
4242
args: ["https://github.com/camptocamp/odoo-cloud-platform"]
4343
- repo: https://github.com/myint/autoflake
44-
rev: v1.4
44+
rev: v1.6.1
4545
hooks:
4646
- id: autoflake
4747
args:
@@ -52,30 +52,30 @@ repos:
5252
- --remove-duplicate-keys
5353
- --remove-unused-variables
5454
- repo: https://github.com/psf/black
55-
rev: 22.3.0
55+
rev: 22.8.0
5656
hooks:
5757
- id: black
5858
- repo: https://github.com/pre-commit/mirrors-prettier
59-
rev: v2.4.1
59+
rev: v2.7.1
6060
hooks:
6161
- id: prettier
6262
name: prettier (with plugin-xml)
6363
additional_dependencies:
64-
- "prettier@2.4.1"
65-
- "@prettier/plugin-xml@1.1.0"
64+
- "prettier@2.7.1"
65+
- "@prettier/plugin-xml@2.2.0"
6666
args:
6767
- --plugin=@prettier/plugin-xml
6868
files: \.(css|htm|html|js|json|jsx|less|md|scss|toml|ts|xml|yaml|yml)$
6969
- repo: https://github.com/pre-commit/mirrors-eslint
70-
rev: v7.32.0
70+
rev: v8.24.0
7171
hooks:
7272
- id: eslint
7373
verbose: true
7474
args:
7575
- --color
7676
- --fix
7777
- repo: https://github.com/pre-commit/pre-commit-hooks
78-
rev: v4.0.1
78+
rev: v4.3.0
7979
hooks:
8080
- id: trailing-whitespace
8181
# exclude autogenerated files
@@ -97,7 +97,7 @@ repos:
9797
- id: mixed-line-ending
9898
args: ["--fix=lf"]
9999
- repo: https://github.com/asottile/pyupgrade
100-
rev: v2.29.0
100+
rev: v2.38.2
101101
hooks:
102102
- id: pyupgrade
103103
args: ["--keep-percent-format"]
@@ -114,13 +114,13 @@ repos:
114114
hooks:
115115
- id: setuptools-odoo-make-default
116116
- repo: https://github.com/PyCQA/flake8
117-
rev: 3.9.2
117+
rev: 5.0.4
118118
hooks:
119119
- id: flake8
120120
name: flake8
121121
additional_dependencies: ["flake8-bugbear==21.9.2"]
122122
- repo: https://github.com/OCA/pylint-odoo
123-
rev: 7.0.2
123+
rev: v8.0.19
124124
hooks:
125125
- id: pylint_odoo
126126
name: pylint with optional checks

monitoring_prometheus/controllers/prometheus_metrics.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
from odoo.http import Controller, route
77

8+
from ..models.psutils_helpers import get_process_info
9+
810

911
class PrometheusController(Controller):
1012
@route("/metrics", auth="public")
1113
def metrics(self):
14+
get_process_info()
1215
return generate_latest()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import ir_http
2+
from . import psutils_helpers
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import psutil
2+
from prometheus_client import Gauge
3+
4+
MEMORY_USAGE_VMS = Gauge(
5+
"odoo_worker_memory_user_vms_mb", "Memory usage in MB", ["process", "pid"]
6+
)
7+
8+
MEMORY_USAGE_RSS = Gauge(
9+
"odoo_worker_memory_user_rss_mb", "Memory usage in MB", ["process", "pid"]
10+
)
11+
12+
13+
def get_process_info():
14+
for process in psutil.process_iter(
15+
["pid", "name", "memory_full_info", "cmdline", "nice"]
16+
):
17+
try:
18+
if process.info["memory_full_info"]:
19+
if process.info["nice"] == 10:
20+
ProcessLabel = "workercron"
21+
elif process.info["pid"] == 1:
22+
ProcessLabel = "dispatcher"
23+
elif any("gevent" in x for x in process.info["cmdline"]):
24+
ProcessLabel = "gevent"
25+
elif any("odoo" in x for x in process.info["cmdline"]):
26+
ProcessLabel = "workerhttp"
27+
elif any("shell" in x for x in process.cmdline()):
28+
ProcessLabel = "OdooShell"
29+
else:
30+
ProcessLabel = "other"
31+
MEMORY_USAGE_VMS.labels(ProcessLabel, process.info["pid"]).set(
32+
process.info["memory_full_info"].rss // 1000000
33+
)
34+
MEMORY_USAGE_RSS.labels(ProcessLabel, process.info["pid"]).set(
35+
process.info["memory_full_info"].vms // 1000000
36+
)
37+
38+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
39+
continue

0 commit comments

Comments
 (0)