Skip to content

Commit 0633ba3

Browse files
authored
Merge pull request #8 from intergral/tp_api
feat(api): add api function to register tracepoint directly
2 parents 1224caf + 5730170 commit 0633ba3

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/deep/api/deep.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
# but WITHOUT ANY WARRANTY; without even the implied warranty of
1010
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1111
# GNU Affero General Public License for more details.
12+
from typing import Dict, List
1213

1314
from deep.api.plugin import load_plugins
1415
from deep.api.resource import Resource
16+
from deep.api.tracepoint import TracePointConfig
17+
from deep.config import ConfigService
18+
from deep.config.tracepoint_config import TracepointConfigService
1519
from deep.grpc import GRPCService
1620
from deep.poll import LongPoll
1721
from deep.processor import TriggerHandler
@@ -25,7 +29,7 @@ class Deep:
2529
DEEP is so small there is no need for service injection work.
2630
"""
2731

28-
def __init__(self, config):
32+
def __init__(self, config: 'ConfigService'):
2933
self.started = False
3034
self.config = config
3135
self.grpc = GRPCService(self.config)
@@ -51,3 +55,26 @@ def shutdown(self):
5155
return
5256
self.task_handler.flush()
5357
self.started = False
58+
def register_tracepoint(self, path: str, line: int, args: Dict[str, str] = None,
59+
watches: List[str] = None) -> 'TracepointRegistration':
60+
if watches is None:
61+
watches = []
62+
if args is None:
63+
args = {}
64+
tp_config = self.config.tracepoints.add_custom(path, line, args, watches)
65+
return TracepointRegistration(tp_config, self.config.tracepoints)
66+
67+
68+
class TracepointRegistration:
69+
_cfg: TracePointConfig
70+
_tpServ: TracepointConfigService
71+
72+
def __init__(self, cfg: TracePointConfig, tracepoints: TracepointConfigService):
73+
self._cfg = cfg
74+
self._tpServ = tracepoints
75+
76+
def get(self) -> TracePointConfig:
77+
return self._cfg
78+
79+
def unregister(self):
80+
self._tpServ.remove_custom(self._cfg)

src/deep/config/tracepoint_config.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212

1313
import abc
1414
import logging
15+
import uuid
16+
from typing import Dict, List
17+
18+
from deep.api.tracepoint import TracePointConfig
1519

1620

1721
class TracepointConfigService:
1822
"""This service deals with new responses from the LongPoll"""
1923

2024
def __init__(self) -> None:
25+
self._custom = []
2126
self._tracepoint_config = []
2227
self._current_hash = None
2328
self._last_update = 0
@@ -43,6 +48,10 @@ def update_new_config(self, ts, new_hash, new_config):
4348
self._last_update = ts
4449
self._current_hash = new_hash
4550
self._tracepoint_config = new_config
51+
self.trigger_update(old_hash, old_config)
52+
53+
def trigger_update(self, old_hash, old_config):
54+
ts = self._last_update
4655
if self._task_handler is not None:
4756
future = self._task_handler.submit_task(self.update_listeners, self._last_update, old_hash,
4857
self._current_hash, old_config, self._tracepoint_config)
@@ -57,7 +66,7 @@ def update_listeners(self, ts, old_hash, current_hash, old_config, new_config):
5766
listeners_copy = self._listeners.copy()
5867
for listeners in listeners_copy:
5968
try:
60-
listeners.config_change(ts, old_hash, current_hash, old_config, new_config)
69+
listeners.config_change(ts, old_hash, current_hash, old_config, new_config + self._custom)
6170
except Exception:
6271
logging.exception("Error updating listener %s", listeners)
6372

@@ -73,6 +82,18 @@ def current_config(self):
7382
def current_hash(self):
7483
return self._current_hash
7584

85+
def add_custom(self, path: str, line: int, args: Dict[str, str], watches: List[str]) -> TracePointConfig:
86+
config = TracePointConfig(str(uuid.uuid4()), path, line, args, watches)
87+
self._custom.append(config)
88+
self.trigger_update(None, None)
89+
return config
90+
91+
def remove_custom(self, config: TracePointConfig):
92+
for idx, cfg in enumerate(self._custom):
93+
if cfg.id == config.id:
94+
del self._custom[idx]
95+
return
96+
7697

7798
class ConfigUpdateListener(abc.ABC):
7899
"""

0 commit comments

Comments
 (0)