-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Fix #3051: optional query log rotation via GRAPHIFY_QUERY_LOG_MAX_RECORDS #3244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akshitj11
wants to merge
3
commits into
Graphify-Labs:v8
Choose a base branch
from
akshitj11:fix/3051-query-log-rotation
base: v8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+224
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| """Query logging for graphify — append-only JSONL, fail-silent.""" | ||
| from __future__ import annotations | ||
|
|
||
| import contextlib | ||
| import json | ||
| import os | ||
| import re | ||
| import time | ||
| from datetime import datetime, timezone | ||
| from pathlib import Path | ||
| from typing import Any | ||
| from typing import Any, Iterator | ||
|
|
||
| from graphify.paths import _atomic_replace | ||
|
|
||
| _NODES_RE = re.compile(r"(\d+)\s+nodes?\s+found") | ||
|
|
||
|
|
@@ -40,6 +42,74 @@ def nodes_from_result(result: str) -> int | None: | |
| return int(m.group(1)) if m else None | ||
|
|
||
|
|
||
| def _max_records() -> int | None: | ||
| raw = os.environ.get("GRAPHIFY_QUERY_LOG_MAX_RECORDS", "").strip() | ||
| if not raw: | ||
| return None | ||
| try: | ||
| n = int(raw) | ||
| except ValueError: | ||
| return None | ||
| return n if n > 0 else None | ||
|
|
||
|
|
||
| def _archive_path(path: Path) -> Path: | ||
| return path.with_name(f"{path.stem}.archive{path.suffix}") | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _query_log_lock(path: Path) -> Iterator[None]: | ||
| """Serialize append+rotate on POSIX (watch.py uses the same flock pattern). | ||
|
|
||
| Multi-process rotation on Windows is best-effort when fcntl is unavailable. | ||
| If the lock file cannot be opened, degrades to unlocked append rather than | ||
| dropping the log line. | ||
| """ | ||
| try: | ||
| import fcntl | ||
| except ImportError: | ||
| yield | ||
| return | ||
| lock_path = path.with_name(path.name + ".lock") | ||
| lock_path.parent.mkdir(parents=True, exist_ok=True) | ||
| try: | ||
| fh = open(lock_path, "a+", encoding="utf-8") | ||
| except OSError: | ||
| yield | ||
| return | ||
| try: | ||
| fcntl.flock(fh.fileno(), fcntl.LOCK_EX) | ||
| yield | ||
| finally: | ||
| try: | ||
| fcntl.flock(fh.fileno(), fcntl.LOCK_UN) | ||
| except OSError: | ||
| pass | ||
| fh.close() | ||
|
|
||
|
|
||
| def _rotate_if_needed(path: Path, max_records: int) -> None: | ||
| if not path.is_file(): | ||
| return | ||
| lines = path.read_text(encoding="utf-8").splitlines(keepends=True) | ||
| if not lines or len(lines) <= max_records: | ||
| return | ||
| overflow, keep = lines[:-max_records], lines[-max_records:] | ||
|
|
||
| def _write_keep(fh) -> None: | ||
| fh.write("".join(keep)) | ||
|
|
||
| _atomic_replace(path, _write_keep) | ||
| archive = _archive_path(path) | ||
| archive.parent.mkdir(parents=True, exist_ok=True) | ||
| try: | ||
| with archive.open("a", encoding="utf-8") as fh: | ||
| fh.writelines(overflow) | ||
| except OSError: | ||
| with path.open("a", encoding="utf-8") as fh: | ||
| fh.writelines(overflow) | ||
|
|
||
|
|
||
| def log_query( | ||
|
akshitj11 marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
fans out to 6 callees (efferent coupling); 25 callers depend on it (afferent coupling). Grounded coupling-delta finding (deterministic), not an LLM guess. |
||
| *, | ||
| kind: str, | ||
|
|
@@ -74,7 +144,11 @@ def log_query( | |
| if result is not None and _log_responses(): | ||
| rec["response"] = result | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| with path.open("a", encoding="utf-8") as fh: | ||
| fh.write(json.dumps(rec, ensure_ascii=False) + "\n") | ||
| with _query_log_lock(path): | ||
| with path.open("a", encoding="utf-8") as fh: | ||
| fh.write(json.dumps(rec, ensure_ascii=False) + "\n") | ||
| max_records = _max_records() | ||
| if max_records is not None: | ||
| _rotate_if_needed(path, max_records) | ||
| except Exception: | ||
| pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.