Skip to content

Commit f5446eb

Browse files
committed
style: enforce google docstrings and remove inline comments
1 parent eae5a28 commit f5446eb

9 files changed

Lines changed: 27 additions & 20 deletions

File tree

packages/core/src/nl2sql/common/event_logger.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def log_event(
5252
tenant_id: Tenant/Customer ID.
5353
"""
5454

55-
# Basic PII/Secret Redaction (Keys to scrub)
5655
sensitive_keys = {"api_key", "password", "secret", "authorization"}
5756
cleaned_payload = self._redact(payload, sensitive_keys)
5857

@@ -67,7 +66,15 @@ def log_event(
6766
self.logger.info(json.dumps(event))
6867

6968
def _redact(self, data: Any, keys_to_redact: set) -> Any:
70-
"""Recursively redact sensitive keys from dictionary."""
69+
"""Recursively redact sensitive keys from dictionary.
70+
71+
Args:
72+
data: Input data (dict, list, or primitive).
73+
keys_to_redact: Set of lowercase keys to match and redact.
74+
75+
Returns:
76+
The sanitized data structure with sensitive values replaced by '***REDACTED***'.
77+
"""
7178
if isinstance(data, dict):
7279
return {
7380
k: ("***REDACTED***" if k.lower() in keys_to_redact else self._redact(v, keys_to_redact))

packages/core/src/nl2sql/common/logger.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ def configure_logging(level: str = "INFO", json_format: bool = False):
9494
if json_format:
9595
handler.setFormatter(JsonFormatter())
9696
else:
97-
# Include trace_id in standard format if present
98-
# This is a bit tricky with dynamic formatting, usually easier to check record in formatter
99-
# For simplicity, we stick to standard format but maybe prepend trace_id if possible?
100-
# We'll stick to a standard format for text logs for now, trace_id mainly for JSON/Production
97+
# Standard text format
10198
formatter = logging.Formatter(
10299
"%(asctime)s - [%(trace_id)s] - %(name)s - %(levelname)s - %(message)s"
103100
)

packages/core/src/nl2sql/common/metrics.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader
66
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
77

8-
# Legacy in-memory logs (kept for CLI compatibility)
8+
# Legacy lists for CLI compatibility
99
TOKEN_LOG: List[Dict[str, Any]] = []
1010
LATENCY_LOG: List[Dict[str, Any]] = []
1111

12-
# OpenTelemetry Instruments
1312
_meter = metrics.get_meter("nl2sql.core")
1413
node_duration_histogram = _meter.create_histogram(
1514
name="nl2sql.node.duration",

packages/core/src/nl2sql/common/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ def configure_env(self, env: str) -> None:
108108

109109
settings = Settings()
110110

111-
# Auto-configure logging based on settings
112-
# We want JSON logs in production (OTLP) to allow parsing by aggregators
111+
# Configure logging during import
113112
from nl2sql.common.logger import configure_logging
114113
configure_logging(
115114
level="INFO",

packages/core/src/nl2sql/pipeline/graph.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
def traced_node(node: Callable):
2525
"""Wraps a node to inject trace_id and tenant_id from state into the logging context."""
2626
def wrapper(state: Union[Dict, Any]):
27-
# Extract trace_id and tenant_id from state
2827
tid = None
2928
tenant_id = None
3029

3130
if isinstance(state, dict):
3231
tid = state.get("trace_id")
33-
# user_context might be dict or object depending on serialization state in LangGraph
3432
uc = state.get("user_context")
3533
if isinstance(uc, dict):
3634
tenant_id = uc.get("tenant_id")

packages/core/src/nl2sql/pipeline/nodes/decomposer/node.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ def __call__(self, state: GraphState) -> Dict[str, Any]:
135135
if state.semantic_analysis:
136136
analysis = state.semantic_analysis
137137
if analysis.keywords or analysis.synonyms:
138-
# Construct a search blob: Canonical + Keywords + Synonyms
139138
expanded_query = f"{analysis.canonical_query} {' '.join(analysis.keywords)} {' '.join(analysis.synonyms)}"
140139
print(f"Expanded Query: {expanded_query}")
141140

packages/core/src/nl2sql/services/callbacks/monitor.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
from langchain_core.outputs import LLMResult
77

88
class PipelineMonitorCallback(BaseCallbackHandler):
9+
"""Callback handler for monitoring pipeline execution and auditing events.
10+
11+
Integrates with OpenTelemetry for metrics and EventLogger for audit trails.
12+
"""
13+
914
def __init__(self, presenter: ConsolePresenter):
1015
from nl2sql.common.settings import settings
1116
from nl2sql.common.metrics import configure_metrics
1217

13-
# One-time setup of metrics based on settings
1418
configure_metrics(
1519
exporter_type=settings.observability_exporter,
1620
otlp_endpoint=settings.otlp_endpoint
@@ -20,29 +24,31 @@ def __init__(self, presenter: ConsolePresenter):
2024
self.tokens = TokenHandler(self.node_handler.node_metrics)
2125

2226
def on_chain_start(self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any) -> Any:
27+
"""Called when a chain starts."""
2328
node_name = kwargs.get("metadata", {}).get("langgraph_node")
2429
run_id = str(kwargs.get("run_id"))
2530
parent_run_id = kwargs.get("parent_run_id")
2631
parent_run_id = str(parent_run_id) if parent_run_id else None
2732
self.node_handler.on_chain_start(run_id, parent_run_id, node_name, inputs)
2833

2934
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> Any:
35+
"""Called when a chain ends."""
3036
run_id = str(kwargs.get("run_id"))
3137
self.node_handler.on_chain_end(run_id)
3238

3339
def on_chain_error(self, error: BaseException, **kwargs: Any) -> Any:
40+
"""Called when a chain errors."""
3441
run_id = str(kwargs.get("run_id"))
3542
self.node_handler.on_chain_error(run_id, error)
3643

3744
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> Any:
45+
"""Called when an LLM ends."""
3846
from nl2sql.common.event_logger import event_logger
3947
from nl2sql.common.logger import _trace_id_ctx, _tenant_id_ctx
4048

4149
tags = kwargs.get("tags", [])
4250
agent_name = next((t for t in tags if not t.startswith("seq:") and not t.startswith("langsmith:")), "unknown")
4351

44-
# Capture audit event
45-
# Assuming single generation per call for simplicity in audit log
4652
text_output = ""
4753
model_name = "unknown"
4854
token_usage = {}
@@ -58,11 +64,10 @@ def on_llm_end(self, response: LLMResult, **kwargs: Any) -> Any:
5864
audit_payload = {
5965
"agent": agent_name,
6066
"model": model_name,
61-
"response_snippet": text_output[:1000], # Trucate for sanity, full content maybe too big?
67+
"response_snippet": text_output[:1000],
6268
"token_usage": token_usage
6369
}
6470

65-
# Pull context directly from vars since we are in the same thread execution context
6671
event_logger.log_event(
6772
event_type="llm_interaction",
6873
payload=audit_payload,

packages/core/src/nl2sql/services/callbacks/node_handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010

1111
class NodeHandler:
12+
"""Handles node execution lifecycle events and metrics recording."""
13+
1214
def __init__(self, presenter: ConsolePresenter):
1315
self.presenter = presenter
1416

@@ -116,7 +118,6 @@ def on_chain_end(self, run_id: str):
116118
}
117119
)
118120

119-
# OTeL Instrumentation
120121
node_duration_histogram.record(
121122
end - start,
122123
attributes={

packages/core/src/nl2sql/services/callbacks/token_handler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77

88
class TokenHandler:
9+
"""Handles token usage tracking and metrics."""
10+
911
def __init__(self, node_metrics: dict[str, NodeMetrics]):
1012
self.node_metrics = node_metrics
1113

1214
def on_llm_end(self, response: LLMResult, agent_name: str = "unknown", model_name: str = "unknown"):
15+
"""Records token usage from LLM response."""
1316
usage = None
1417
if response and response.llm_output:
1518
usage = response.llm_output.get("token_usage") or response.llm_output.get("usage")
@@ -36,7 +39,6 @@ def on_llm_end(self, response: LLMResult, agent_name: str = "unknown", model_nam
3639
}
3740
)
3841

39-
# OTeL Instrumentation
4042
token_usage_counter.add(
4143
t,
4244
attributes={

0 commit comments

Comments
 (0)