From f3c3827a86b02ed8d68a334dd196b4b140b2ce48 Mon Sep 17 00:00:00 2001 From: Ben Donnelly Date: Wed, 1 Nov 2023 17:42:45 +0000 Subject: [PATCH 1/2] feat(api): add api function to register tracepoint directly --- src/deep/api/deep.py | 30 +++++++++++++++++++++++++++- src/deep/config/tracepoint_config.py | 24 +++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/deep/api/deep.py b/src/deep/api/deep.py index 30750f2..4dfd853 100644 --- a/src/deep/api/deep.py +++ b/src/deep/api/deep.py @@ -9,9 +9,13 @@ # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. +from typing import Dict, List from deep.api.plugin import load_plugins from deep.api.resource import Resource +from deep.api.tracepoint import TracePointConfig +from deep.config import ConfigService +from deep.config.tracepoint_config import TracepointConfigService from deep.grpc import GRPCService from deep.poll import LongPoll from deep.processor import TriggerHandler @@ -25,7 +29,7 @@ class Deep: DEEP is so small there is no need for service injection work. """ - def __init__(self, config): + def __init__(self, config: 'ConfigService'): self.started = False self.config = config self.grpc = GRPCService(self.config) @@ -51,3 +55,27 @@ def shutdown(self): return self.task_handler.flush() self.started = False + def register_tracepoint(self, path: str, line: int, args: Dict[str, str] = None, + watches: List[str] = None) -> 'TracepointRegistration': + if watches is None: + watches = [] + if args is None: + args = {} + tp_config = self.config.tracepoints.add_custom(path, line, args, watches) + return TracepointRegistration(tp_config, self.config.tracepoints) + + +class TracepointRegistration: + _cfg: TracePointConfig + _tpServ: TracepointConfigService + + def __init__(self, cfg: TracePointConfig, tracepoints: TracepointConfigService): + self._cfg = cfg + self._tpServ = tracepoints + + def get(self) -> TracePointConfig: + return self._cfg + + def unregister(self): + self._tpServ.remove_custom(self._cfg) + diff --git a/src/deep/config/tracepoint_config.py b/src/deep/config/tracepoint_config.py index 65b5173..6c66393 100644 --- a/src/deep/config/tracepoint_config.py +++ b/src/deep/config/tracepoint_config.py @@ -12,12 +12,17 @@ import abc import logging +import uuid +from typing import Dict, List + +from deep.api.tracepoint import TracePointConfig class TracepointConfigService: """This service deals with new responses from the LongPoll""" def __init__(self) -> None: + self._custom = [] self._tracepoint_config = [] self._current_hash = None self._last_update = 0 @@ -43,6 +48,11 @@ def update_new_config(self, ts, new_hash, new_config): self._last_update = ts self._current_hash = new_hash self._tracepoint_config = new_config + self.trigger_update(old_hash, old_config) + + + def trigger_update(self, old_hash, old_config): + ts = self._last_update if self._task_handler is not None: future = self._task_handler.submit_task(self.update_listeners, self._last_update, old_hash, self._current_hash, old_config, self._tracepoint_config) @@ -57,7 +67,7 @@ def update_listeners(self, ts, old_hash, current_hash, old_config, new_config): listeners_copy = self._listeners.copy() for listeners in listeners_copy: try: - listeners.config_change(ts, old_hash, current_hash, old_config, new_config) + listeners.config_change(ts, old_hash, current_hash, old_config, new_config + self._custom) except Exception: logging.exception("Error updating listener %s", listeners) @@ -73,6 +83,18 @@ def current_config(self): def current_hash(self): return self._current_hash + def add_custom(self, path: str, line: int, args: Dict[str, str], watches: List[str]) -> TracePointConfig: + config = TracePointConfig(str(uuid.uuid4()), path, line, args, watches) + self._custom.append(config) + self.trigger_update(None, None) + return config + + def remove_custom(self, config: TracePointConfig): + for idx, cfg in enumerate(self._custom): + if cfg.id == config.id: + del self._custom[idx] + return + class ConfigUpdateListener(abc.ABC): """ From 573017061ad146e5ee1e72f67146988f80b8646b Mon Sep 17 00:00:00 2001 From: Ben Donnelly Date: Wed, 1 Nov 2023 18:15:40 +0000 Subject: [PATCH 2/2] feat(api): add api function to register tracepoint directly --- src/deep/api/deep.py | 1 - src/deep/config/tracepoint_config.py | 1 - 2 files changed, 2 deletions(-) diff --git a/src/deep/api/deep.py b/src/deep/api/deep.py index 4dfd853..77ee659 100644 --- a/src/deep/api/deep.py +++ b/src/deep/api/deep.py @@ -78,4 +78,3 @@ def get(self) -> TracePointConfig: def unregister(self): self._tpServ.remove_custom(self._cfg) - diff --git a/src/deep/config/tracepoint_config.py b/src/deep/config/tracepoint_config.py index 6c66393..a4df6a1 100644 --- a/src/deep/config/tracepoint_config.py +++ b/src/deep/config/tracepoint_config.py @@ -50,7 +50,6 @@ def update_new_config(self, ts, new_hash, new_config): self._tracepoint_config = new_config self.trigger_update(old_hash, old_config) - def trigger_update(self, old_hash, old_config): ts = self._last_update if self._task_handler is not None: