feat: add skip_reason to Plugin; improve PluginLoader init messaging
This commit is contained in:
+10
-4
@@ -39,13 +39,14 @@ class Plugin(ABC):
|
|||||||
|
|
||||||
def __init__(self, config: Optional[Dict[str, Any]] = None):
|
def __init__(self, config: Optional[Dict[str, Any]] = None):
|
||||||
"""Initialize plugin with optional configuration.
|
"""Initialize plugin with optional configuration.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
config: Plugin-specific configuration from YAML (e.g., thresholds, paths)
|
config: Plugin-specific configuration from YAML (e.g., thresholds, paths)
|
||||||
"""
|
"""
|
||||||
self.config = config or {}
|
self.config = config or {}
|
||||||
self.logger = logging.getLogger(f"plugin.{self.name}")
|
self.logger = logging.getLogger(f"plugin.{self.name}")
|
||||||
self._initialized = False
|
self._initialized = False
|
||||||
|
self.skip_reason: Optional[str] = None
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def initialize(self) -> bool:
|
async def initialize(self) -> bool:
|
||||||
@@ -369,9 +370,14 @@ class PluginLoader:
|
|||||||
try:
|
try:
|
||||||
initialized = await plugin.initialize()
|
initialized = await plugin.initialize()
|
||||||
if not initialized:
|
if not initialized:
|
||||||
self.logger.warning(
|
if plugin.skip_reason:
|
||||||
f"Plugin {plugin.name} failed initialization, skipping"
|
self.logger.info(
|
||||||
)
|
f"Plugin {plugin.name} skipped: {plugin.skip_reason}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.logger.warning(
|
||||||
|
f"Plugin {plugin.name} failed initialization, skipping"
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(
|
self.logger.error(
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import textwrap
|
||||||
|
|
||||||
|
from hbd.client.plugin import Plugin, PluginLoader, PluginRegistry
|
||||||
|
|
||||||
|
|
||||||
|
def test_plugin_skip_reason_defaults_none(tmp_path):
|
||||||
|
plugin_code = textwrap.dedent("""
|
||||||
|
from hbd.client.plugin import MonitorPlugin
|
||||||
|
|
||||||
|
class MinimalPlugin(MonitorPlugin):
|
||||||
|
name = "minimal"
|
||||||
|
version = "1.0.0"
|
||||||
|
interval = 60
|
||||||
|
|
||||||
|
async def initialize(self):
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def _collect_metrics(self):
|
||||||
|
return {}
|
||||||
|
""")
|
||||||
|
(tmp_path / "minimal.py").write_text(plugin_code)
|
||||||
|
registry = PluginRegistry()
|
||||||
|
loader = PluginLoader(registry)
|
||||||
|
asyncio.run(loader.load_from_directory(tmp_path))
|
||||||
|
plugin = registry.get("minimal")
|
||||||
|
assert plugin is not None
|
||||||
|
assert plugin.skip_reason is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_loader_logs_info_when_skip_reason_set(tmp_path, caplog):
|
||||||
|
plugin_code = textwrap.dedent("""
|
||||||
|
from hbd.client.plugin import MonitorPlugin
|
||||||
|
|
||||||
|
class SkippablePlugin(MonitorPlugin):
|
||||||
|
name = "skippable"
|
||||||
|
version = "1.0.0"
|
||||||
|
interval = 60
|
||||||
|
|
||||||
|
async def initialize(self):
|
||||||
|
self.skip_reason = "not configured in yaml"
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def _collect_metrics(self):
|
||||||
|
return {}
|
||||||
|
""")
|
||||||
|
(tmp_path / "skippable.py").write_text(plugin_code)
|
||||||
|
registry = PluginRegistry()
|
||||||
|
loader = PluginLoader(registry)
|
||||||
|
|
||||||
|
with caplog.at_level(logging.INFO, logger="plugin.loader"):
|
||||||
|
count = asyncio.run(loader.load_from_directory(tmp_path))
|
||||||
|
|
||||||
|
assert count == 0
|
||||||
|
assert any("skipped: not configured in yaml" in r.message for r in caplog.records)
|
||||||
|
assert not any("failed initialization" in r.message for r in caplog.records)
|
||||||
|
|
||||||
|
|
||||||
|
def test_loader_logs_warning_when_no_skip_reason(tmp_path, caplog):
|
||||||
|
plugin_code = textwrap.dedent("""
|
||||||
|
from hbd.client.plugin import MonitorPlugin
|
||||||
|
|
||||||
|
class FailPlugin(MonitorPlugin):
|
||||||
|
name = "fail"
|
||||||
|
version = "1.0.0"
|
||||||
|
interval = 60
|
||||||
|
|
||||||
|
async def initialize(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def _collect_metrics(self):
|
||||||
|
return {}
|
||||||
|
""")
|
||||||
|
(tmp_path / "fail_plugin.py").write_text(plugin_code)
|
||||||
|
registry = PluginRegistry()
|
||||||
|
loader = PluginLoader(registry)
|
||||||
|
|
||||||
|
with caplog.at_level(logging.WARNING, logger="plugin.loader"):
|
||||||
|
count = asyncio.run(loader.load_from_directory(tmp_path))
|
||||||
|
|
||||||
|
assert count == 0
|
||||||
|
assert any("failed initialization" in r.message for r in caplog.records)
|
||||||
Reference in New Issue
Block a user