Skip to content

Commit 582e759

Browse files
committed
[IMP] rest_log: improve profiling and add tests
1 parent 002ecd2 commit 582e759

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

rest_log/components/service.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ def dispatch(self, method_name, *args, params=None):
4444
return super().dispatch(method_name, *args, params=params)
4545
if self._start_profiling(method_name):
4646
call_name = f"{self._collection}.{self._usage}.{method_name}"
47-
with Profiler(description=f"REST LOG {call_name}"):
47+
with Profiler(
48+
description=f"REST LOG {call_name} "
49+
f"by {self.env.user.name} (uid={self.env.uid})"
50+
):
4851
return self._dispatch_with_db_logging(method_name, *args, params=params)
4952
return self._dispatch_with_db_logging(method_name, *args, params=params)
5053

@@ -233,30 +236,35 @@ def _get_matching_active_conf(self, method_name):
233236
def _start_profiling(self, method_name):
234237
if request.session.profile_session and request.db:
235238
return None
236-
profiling_uids = 0
239+
profiling_uids = self._profiling_get_uids()
240+
profiling_conf_match = self._profiling_get_matching_conf(method_name)
241+
res = profiling_conf_match and self.env.uid in profiling_uids
242+
if res:
243+
_logger.info(
244+
"Profiling enabled for uids=%s %s",
245+
str(profiling_uids),
246+
f"{self._collection}.{self._usage}.{method_name}",
247+
)
248+
return res
249+
250+
def _profiling_get_uids(self):
237251
try:
238-
profiling_uids = [
239-
int(x)
240-
for x in self.env["ir.config_parameter"]
252+
param = (
253+
self.env["ir.config_parameter"]
241254
.sudo()
242255
.get_param("rest.log.profiling.uid", "")
243-
.split(",")
244-
]
256+
)
257+
if not param.strip():
258+
return []
259+
return [int(x.strip()) for x in param.strip().split(",") if x.strip()]
245260
except ValueError as err:
246261
_logger.warning(
247262
"Cannot get uid from system parameter rest.log.profiling.uid: %s",
248263
str(err),
249264
)
250-
res = (
251-
self.env["rest.log"]._get_matching_conf_from_param(
252-
"rest.log.profiling.conf", self._collection, self._usage, method_name
253-
)
254-
and self.env.uid in profiling_uids
265+
return []
266+
267+
def _profiling_get_matching_conf(self, method_name):
268+
return self.env["rest.log"]._get_matching_conf_from_param(
269+
"rest.log.profiling.conf", self._collection, self._usage, method_name
255270
)
256-
if res:
257-
_logger.info(
258-
"Profiling enabled for uids=%s %s",
259-
str(profiling_uids),
260-
f"{self._collection}.{self._usage}.{method_name}",
261-
)
262-
return res

rest_log/tests/test_db_logging.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,33 @@ def test_log_entry_values_failure_with_response(self):
274274
"status": 418,
275275
},
276276
)
277+
278+
def test_start_profiling(self):
279+
with self._get_mocked_request() as mocked_request:
280+
config_param = self.env["ir.config_parameter"].sudo()
281+
config_param.set_param(
282+
"rest.log.profiling.conf",
283+
f"{self.service._collection}.{self.service._usage}.avg_endpoint",
284+
)
285+
config_param.set_param("rest.log.profiling.uid", str(self.env.uid))
286+
with self.assertLogs("odoo.addons.rest_log.components.service") as logs:
287+
self.assertTrue(self.service._start_profiling("avg_endpoint"))
288+
self.assertEqual(
289+
logs.output[0],
290+
f"INFO:odoo.addons.rest_log.components.service:"
291+
f"Profiling enabled for uids=[{self.env.uid}] "
292+
"base.rest.test.logmycalls.avg_endpoint",
293+
)
294+
295+
config_param.set_param("rest.log.profiling.uid", "99999")
296+
self.assertFalse(self.service._start_profiling("avg_endpoint"))
297+
298+
mocked_request.session.profile_session = "rest-log-test"
299+
self.assertIsNone(self.service._start_profiling("avg_endpoint"))
300+
301+
def test_get_profiling_uids(self):
302+
config_param = self.env["ir.config_parameter"].sudo()
303+
with self._get_mocked_request():
304+
self.assertEqual(self.service._profiling_get_uids(), [])
305+
config_param.set_param("rest.log.profiling.uid", f"{self.env.uid}, 99999")
306+
self.assertEqual(self.service._profiling_get_uids(), [self.env.uid, 99999])

0 commit comments

Comments
 (0)