Skip to content

Commit dc4ca45

Browse files
committed
feat: add memory monitoring for prometheus
1 parent 8046783 commit dc4ca45

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

.pre-commit-config.yaml

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
exclude: |
22
(?x)
33
# NOT INSTALLABLE ADDONS
4+
^attachment_s3/|
5+
^attachment_swift/|
6+
^base_fileurl_field/|
7+
^cloud_platform_exoscale/|
8+
^cloud_platform_ovh/|
9+
^monitoring_log_requests/|
10+
^monitoring_statsd/|
411
^test_base_fileurl_field/|
512
# END NOT INSTALLABLE ADDONS
613
# Files and folders generated by bots, to avoid loops
@@ -17,7 +24,7 @@ exclude: |
1724
(LICENSE.*|COPYING.*)
1825
default_language_version:
1926
python: python3
20-
node: "14.18.0"
27+
node: "16.17.0"
2128
repos:
2229
- repo: local
2330
hooks:
@@ -34,14 +41,14 @@ repos:
3441
language: fail
3542
files: '[a-zA-Z0-9_]*/i18n/en\.po$'
3643
- repo: https://github.com/oca/maintainer-tools
37-
rev: dfba427ba03900b69e0a7f2c65890dc48921d36a
44+
rev: 4cd2b852214dead80822e93e6749b16f2785b2fe
3845
hooks:
3946
# update the NOT INSTALLABLE ADDONS section above
4047
- id: oca-update-pre-commit-excluded-addons
4148
- id: oca-fix-manifest-website
4249
args: ["https://github.com/camptocamp/odoo-cloud-platform"]
4350
- repo: https://github.com/myint/autoflake
44-
rev: v1.4
51+
rev: v1.6.1
4552
hooks:
4653
- id: autoflake
4754
args:
@@ -52,30 +59,30 @@ repos:
5259
- --remove-duplicate-keys
5360
- --remove-unused-variables
5461
- repo: https://github.com/psf/black
55-
rev: 22.3.0
62+
rev: 22.8.0
5663
hooks:
5764
- id: black
5865
- repo: https://github.com/pre-commit/mirrors-prettier
59-
rev: v2.4.1
66+
rev: v2.7.1
6067
hooks:
6168
- id: prettier
6269
name: prettier (with plugin-xml)
6370
additional_dependencies:
64-
- "prettier@2.4.1"
65-
- "@prettier/plugin-xml@1.1.0"
71+
- "prettier@2.7.1"
72+
- "@prettier/plugin-xml@2.2.0"
6673
args:
6774
- --plugin=@prettier/plugin-xml
6875
files: \.(css|htm|html|js|json|jsx|less|md|scss|toml|ts|xml|yaml|yml)$
6976
- repo: https://github.com/pre-commit/mirrors-eslint
70-
rev: v7.32.0
77+
rev: v8.24.0
7178
hooks:
7279
- id: eslint
7380
verbose: true
7481
args:
7582
- --color
7683
- --fix
7784
- repo: https://github.com/pre-commit/pre-commit-hooks
78-
rev: v4.0.1
85+
rev: v4.3.0
7986
hooks:
8087
- id: trailing-whitespace
8188
# exclude autogenerated files
@@ -97,7 +104,7 @@ repos:
97104
- id: mixed-line-ending
98105
args: ["--fix=lf"]
99106
- repo: https://github.com/asottile/pyupgrade
100-
rev: v2.29.0
107+
rev: v2.38.2
101108
hooks:
102109
- id: pyupgrade
103110
args: ["--keep-percent-format"]
@@ -114,13 +121,13 @@ repos:
114121
hooks:
115122
- id: setuptools-odoo-make-default
116123
- repo: https://github.com/PyCQA/flake8
117-
rev: 3.9.2
124+
rev: 5.0.4
118125
hooks:
119126
- id: flake8
120127
name: flake8
121128
additional_dependencies: ["flake8-bugbear==21.9.2"]
122129
- repo: https://github.com/OCA/pylint-odoo
123-
rev: 7.0.2
130+
rev: v8.0.19
124131
hooks:
125132
- id: pylint_odoo
126133
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)