Skip to content

History & Trends

Persistent storage and historical trend analysis (v0.5.0+)

SQLite Persistence

All analysis results are automatically persisted to a local SQLite database via better-sqlite3. The database is lazily initialized on first use and uses WAL mode for optimal read performance.

Database location: Controlled by SQLITE_PATH (default: ./storage/kafka-analyzer.db)

If the storage directory is not writable or better-sqlite3 fails to load, the system degrades gracefully — analysis continues without persistence.

Stored Data

TableDescription
analysis_recordsEvery analysis run with summary statistics
anomaly_detailsPer-anomaly details linked to analysis records
lag_snapshotsEvery get_lag result for trend analysis
baselinesAuto (7-day rolling avg) and manual baselines

Event-Driven Architecture

Persistence and baseline updates are decoupled from the analysis pipeline via an internal EventEmitter:

analyze_log completes → emit('analysis:completed')
    ├─→ persistence listener → write to SQLite → emit('analysis:persisted')
    └─→ baseline listener → update auto baselines (7-day avg)

get_lag completes → emit('lag:completed')
    └─→ persistence listener → write to lag_snapshots

Listener errors are isolated — a failing listener logs the error but never interrupts the main analysis flow.

Anomaly Multiplier Detection

When analysis detects anomalies, the system automatically compares current values against stored baselines. If a value is ≥ 2× the baseline, the anomaly receives multiplier metadata:

⚠️ lag_spike [P0] consumer
   3 partitions with lag > 10000 detected
   🔺 比基线多 3.5 倍(基线: 2857, 当前: 10000, 来源: auto-7d)

This happens synchronously before results are returned — no extra tool call needed.

MCP Tools

query_history

Query past analysis records with optional filters.

Parameters:

ParameterTypeDescription
fromstringStart time ISO 8601 (default: 7 days ago)
tostringEnd time ISO 8601 (default: now)
sourcestringFilter by source: paste / file / exporter / loki
clusterstringFilter by cluster name
limitnumberMax results (default: 50, max: 200)

compare_trend

Compare a metric across two time ranges.

Parameters:

ParameterTypeRequiredDescription
metricstringYeserror_rate / lag / anomaly_count / event_count
current_rangestringYes1h / 6h / 1d / 7d
compare_rangestringNoprevious / last_week / last_month (default: previous)
clusterstringNoCluster name

Output example:

json
{
  "metric": "lag",
  "current": { "range": "2026-07-04 ~ 2026-07-05", "avg": 8500, "max": 15000 },
  "compare": { "range": "2026-07-03 ~ 2026-07-04", "avg": 3200, "max": 8000 },
  "change": { "avgMultiplier": 2.66, "maxMultiplier": 1.88, "trend": "up" },
  "summary": "平均Lag比对比时段2.66倍,峰值1.88倍,趋势上升"
}

set_baseline

Manually set a metric baseline value, overriding any auto baseline.

Parameters:

ParameterTypeRequiredDescription
metric_keystringYese.g. lag:cluster:group:topic
valuenumberYesBaseline value
metric_typestringYeslag / error_rate / anomaly_count

list_baselines

List all baselines, optionally filtered by metric type.

Parameters:

ParameterTypeRequiredDescription
metric_typestringNolag / error_rate / anomaly_count

cleanup_storage

Clean up expired persisted data.

Parameters:

ParameterTypeRequiredDescription
retention_daysnumberNoRetention in days (default: 30)
dry_runbooleanNoPreview only, no deletion (default: false)

Baselines

Baselines define the "normal" value for a metric, used by multiplier detection.

  • Auto baselines: Computed from 7-day rolling averages of historical data. Updated automatically after each analysis is persisted.
  • Manual baselines: Set via set_baseline tool. Override auto baselines for the same metric key.

When no baseline exists for a metric, multiplier detection is silently skipped.

Data Retention

By default, data older than 30 days is automatically cleaned up. This is configurable:

ConfigEnv VarDefaultDescription
storageRetentionDaysSTORAGE_RETENTION_DAYS30Days to retain
storageAutoCleanupSTORAGE_AUTO_CLEANUPtrueEnable daily auto-cleanup

Manual baselines are never auto-deleted. Auto baselines not updated within the retention window are removed.

Released under the MIT License.