Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions nodescraper/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,15 @@
from typing import Any, Optional, Union, cast

from pydantic import BaseModel, Field, field_serializer, field_validator
from pydantic_core import to_jsonable_python

from nodescraper.constants import DEFAULT_EVENT_REPORTER
from nodescraper.enums import EventPriority


def _data_to_json_safe(obj: Any) -> Any:
"""Recursively convert event data to JSON-serializable form (e.g. exceptions -> str)."""
if isinstance(obj, BaseException):
return str(obj)
if isinstance(obj, dict):
return {k: _data_to_json_safe(v) for k, v in obj.items()}
if isinstance(obj, (list, tuple)):
return [_data_to_json_safe(v) for v in obj]
if isinstance(obj, (str, int, float, bool, type(None))):
return obj
if isinstance(obj, (Enum, datetime.datetime, uuid.UUID)):
return str(obj)
return str(obj)
"""Convert data to JSON-serializable form using Pydantic's fast Rust core."""
return to_jsonable_python(obj, fallback=lambda x: str(x))


LOG_LEVEL_MAP = {
Expand Down Expand Up @@ -178,7 +169,9 @@ def validate_data(cls, data: dict) -> dict:
Returns:
dict: data output
"""
if len(str(data).encode("utf-8")) >= (1024 * 100):
# Use the same serialization that will happen during model_dump()
json_safe_data = _data_to_json_safe(data)
if len(str(json_safe_data).encode("utf-8")) >= (1024 * 100):
raise ValueError("Data must be below 100KB in size")
return data

Expand Down
6 changes: 5 additions & 1 deletion nodescraper/plugins/inband/memory/memory_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ def collect_data(self, args=None) -> tuple[TaskResult, Optional[MemoryDataModel]
self._log_event(
category=EventCategory.OS,
description="Free and total memory read",
data=mem_data.model_dump(),
data={
"mem_total": mem_total,
"mem_available": mem_available,
"mem_free": mem_free,
},
priority=EventPriority.INFO,
)
if mem_available:
Expand Down
7 changes: 7 additions & 0 deletions nodescraper/plugins/inband/memory/memorydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ class MemoryDataModel(DataModel):
mem_available: Optional[str] = None
lsmem_data: Optional[LsmemData] = None
numa_topology: Optional[NumaTopology] = None

def __str__(self) -> str:
return (
f"MemoryDataModel(mem_free={self.mem_free}, mem_total={self.mem_total}, "
f"mem_available={self.mem_available}, lsmem_data={self.lsmem_data}, "
f"numa_topology={self.numa_topology})"
)
1 change: 0 additions & 1 deletion nodescraper/plugins/inband/nic/nic_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ def analyze_data(
description=f"Broadcom device {device_num}: getqos does not match expected QoS: {'; '.join(mismatches)}",
data={
"device_num": device_num,
"qos": qos.model_dump(),
"mismatches": mismatches,
},
priority=EventPriority.WARNING,
Expand Down
1 change: 0 additions & 1 deletion nodescraper/plugins/inband/process/process_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def collect_data(
self._log_event(
category="PROCESS_READ",
description="Process data collected",
data=process_data.model_dump(),
priority=EventPriority.INFO,
)
self.result.message = "Process data collected"
Expand Down
1 change: 0 additions & 1 deletion nodescraper/plugins/inband/storage/storage_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def collect_data(
self._log_event(
category="STORAGE_READ",
description="Available storage read",
data=storage_model.model_dump(),
priority=EventPriority.INFO,
)
self.result.message = f"{len(storage_model.storage_data)} storage devices collected"
Expand Down
Loading