Skip to content

Architecture Overview

Kafka Log Analyzer technical architecture design

Overall Architecture

┌─────────────────────────────────────────────────────────────────┐
│                      Claude Code Runtime                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌───────────────┐  ┌───────────────┐  ┌───────────────┐      │
│  │   Commands    │  │     Hooks     │  │   Prompts     │      │
│  │               │  │               │  │               │      │
│  │ /kafka-analyze│  │ on_alert      │  │ kafka_diag    │      │
│  │ /kafka-lag    │  │ scheduled     │  │               │      │
│  └───────┬───────┘  └───────┬───────┘  └───────────────┘      │
│          │                  │                                   │
│          └──────────────────┼───────────────────────────────────┤
│                             ▼                                   │
│  ┌─────────────────────────────────────────────────────────┐  │
│  │                    MCP Server                            │  │
│  │                                                         │  │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐    │  │
│  │  │   Tools     │  │  Resources  │  │  Prompts    │    │  │
│  │  │             │  │             │  │             │    │  │
│  │  │ analyze_log │  │ kafka://    │  │             │    │  │
│  │  │ get_lag     │  │ metrics     │  │             │    │  │
│  │  │ timeline    │  │ history     │  │             │    │  │
│  │  └─────────────┘  └─────────────┘  └─────────────┘    │  │
│  │         │                                                    │
│  │         ▼                                                    │
│  │  ┌─────────────────────────────────────────────────────┐  │  │
│  │  │              Analysis Engine                        │  │  │
│  │  │                                                     │  │  │
│  │  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐│  │  │
│  │  │  │ Python      │  │  Anomaly    │  │  Report     ││  │  │
│  │  │  │ Scripts     │  │  Detector   │  │  Generator  ││  │  │
│  │  │  │ (Reuse)     │  │  (7 types)  │  │             ││  │  │
│  │  │  └─────────────┘  └─────────────┘  └─────────────┘│  │  │
│  │  └─────────────────────────────────────────────────────┘  │  │
│  └─────────────────────────────────────────────────────────┘  │
│                             │                                   │
│                             ▼                                   │
│  ┌─────────────────────────────────────────────────────────┐  │
│  │                    Data Layer                            │  │
│  │                                                         │  │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐    │  │
│  │  │ Prometheus  │  │    Loki     │  │   SQLite    │    │  │
│  │  │ Exporter    │  │   (Logs)    │  │  (History)  │    │  │
│  │  └─────────────┘  └─────────────┘  └─────────────┘    │  │
│  └─────────────────────────────────────────────────────────┘  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Core Components

1. MCP Server

The MCP Server is the core component, responsible for:

  • Tools: Providing analyze_log, get_lag, timeline, etc.
  • Resources: Providing real-time metrics and historical data subscription
  • Prompts: Providing diagnostic templates

Technology choices:

  • TypeScript + @modelcontextprotocol/sdk
  • Node.js >= 18.0

2. Analysis Engine

The analysis engine reuses existing Python scripts:

ComponentFunctionImplementation
Log ParserParse Kafka logsscripts/parse_kafka_log.py
Anomaly DetectorDetect anomaly patternsscripts/detect_anomalies.py
Report GeneratorGenerate diagnostic reportsscripts/generate_report.py

Integration method:

typescript
// src/analysis/parser.ts
import { spawn } from 'child_process';

async function parseLog(content: string) {
  const result = await spawn('python3', [
    'scripts/parse_kafka_log.py',
    '--input', '-'
  ]);
  return JSON.parse(result.stdout);
}

3. Data Layer

The data layer contains three data sources:

Prometheus / Kafka Exporter

  • Real-time metrics query
  • Consumer Lag monitoring
  • Broker performance data

Loki

  • Log aggregation query
  • LogQL query syntax
  • Time range filtering

SQLite

  • Historical analysis records
  • Trend comparison data
  • Baseline storage

Data Flow

Real-time Analysis Flow

┌─────────────┐      ┌─────────────┐      ┌─────────────┐
│ Kafka       │ ───▶ │ Prometheus  │ ───▶ │ Grafana     │
│ Exporter    │      │             │      │ Alert       │
└─────────────┘      └─────────────┘      └─────────────┘

                                                 ▼ Trigger Hook
┌─────────────┐      ┌─────────────┐      ┌─────────────┐
│ Loki / ES   │ ◀─── │ MCP Server  │ ◀─── │ Claude Code │
│ (Logs)      │      │             │      │ Plugin      │
└─────────────┘      └─────────────┘      └─────────────┘
       │                    │                    │
       │                    ▼                    │
       │            ┌─────────────┐             │
       └───────────▶ │ Analysis    │ ◀───────────┘
                    │ Engine      │
                    └─────────────┘


                    ┌─────────────┐
                    │ Report      │ ──▶ Markdown / Slack / Feishu
                    └─────────────┘

Command Invocation Flow

/kafka-analyze --source file --path server.log


┌─────────────┐
│ Command     │ Parse parameters
│ Handler     │
└─────────────┘

       ▼ Call MCP Tool
┌─────────────┐
│ analyze_log │
│ Tool        │
└─────────────┘

       ▼ Execute analysis
┌─────────────┐
│ Python      │ Parse + Detect
│ Scripts     │
└─────────────┘

       ▼ Return results
┌─────────────┐
│ Markdown    │ Format output
│ Formatter   │
└─────────────┘

Layered Architecture

Presentation Layer
├── Commands      (/kafka-analyze, /kafka-lag)
├── Prompts       (Diagnostic templates)
└── Output Formats (Markdown, JSON, Slack)

Application Layer
├── MCP Server    (Tool registration and routing)
├── Hooks         (Auto-trigger logic)
└── Integrations  (Feishu/Slack/JIRA)

Domain Layer
├── Analysis Engine (Log parsing, anomaly detection)
├── Comparator      (Historical comparison)
└── Report Generator (Report generation)

Infrastructure Layer
├── Data Sources    (Prometheus, Loki, SQLite)
├── Python Scripts  (Reuse existing scripts)
└── Utilities       (Logging, configuration, error handling)

Technical Decisions

Why Reuse Python Scripts?

FactorRewrite to TypeScriptReuse PythonDecision
VerificationNeeds re-verificationAlready verified✅ Reuse
Development Time2-3 weeks0 weeks✅ Reuse
Maintenance CostUnified stackTwo sets✅ Acceptable
PerformancePossibly fasterAdequate✅ Reuse

Why Choose SQLite?

FactorPostgreSQLSQLiteDecision
Deployment ComplexityNeeds standalone deploymentZero config✅ SQLite
Data VolumeSupports massiveSuitable for medium✅ SQLite
Query CapabilityMore powerfulAdequate✅ SQLite
PortabilityNeeds migrationSingle file✅ SQLite

Extension Points

1. Add New Data Source

Add new connector in src/mcp-server/connectors/:

typescript
// connectors/new-source.ts
export class NewSourceConnector {
  async query(params: QueryParams): Promise<Result> {
    // Implement query logic
  }
}

2. Add New Detector

Add new detector in scripts/detect_anomalies.py:

python
def detect_new_anomaly(events):
    # Implement detection logic
    return anomalies

3. Add New Output Format

Add new format in src/utils/report-formatter.ts:

typescript
export function formatToNewFormat(result: AnalysisResult): string {
  // Implement formatting logic
}

Performance Considerations

ScenarioTargetImplementation
Log Parsing> 10,000 lines/secPython script optimization
Analysis Delay (1MB)< 5sAsync processing + cache
MCP Tool Response< 500msDirect call + preload
Hook Trigger Delay< 30sDeduplication + merge window

Security Considerations

Security ItemImplementation
Input ValidationTypeScript type checking + schema validation
API AuthenticationEnvironment variables for secrets
Log SanitizationNo sensitive data logging
Permission ControlMCP Server permission configuration

Related Documentation:

Released under the MIT License.