|
| 1 | +# AI JVM Analyzer |
| 2 | + |
| 3 | +AI-powered JVM performance analyzer using Amazon Bedrock. Receives webhook alerts from monitoring systems (Grafana, CloudWatch), collects thread dumps and profiling data, and generates actionable performance analysis reports. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +``` |
| 8 | +┌─────────────────────────────────────────────────────────────┐ |
| 9 | +│ Monitoring System (Grafana/CloudWatch/Prometheus) │ |
| 10 | +│ - Detects high CPU, memory, or thread count alerts │ |
| 11 | +└─────────────────────────────────────────────────────────────┘ |
| 12 | + │ |
| 13 | + ▼ POST /webhook |
| 14 | +┌─────────────────────────────────────────────────────────────┐ |
| 15 | +│ WebhookController │ |
| 16 | +│ - Receives alert payloads with pod name and IP │ |
| 17 | +│ - Validates alerts, filters invalid entries │ |
| 18 | +└─────────────────────────────────────────────────────────────┘ |
| 19 | + │ |
| 20 | + ▼ |
| 21 | +┌─────────────────────────────────────────────────────────────┐ |
| 22 | +│ AnalyzerService │ |
| 23 | +│ - Parallel processing with Virtual Threads │ |
| 24 | +│ - Fetches thread dump from pod's /actuator/threaddump │ |
| 25 | +│ - Retrieves profiling data (flamegraph) from S3 │ |
| 26 | +└─────────────────────────────────────────────────────────────┘ |
| 27 | + │ |
| 28 | + ┌───────────────┴───────────────┐ |
| 29 | + ▼ ▼ |
| 30 | +┌─────────────────────────┐ ┌─────────────────────────┐ |
| 31 | +│ AiService │ │ S3Repository │ |
| 32 | +│ - Spring AI + Bedrock │ │ - Fetch profiling data │ |
| 33 | +│ - Claude Sonnet 4 │ │ - Store analysis │ |
| 34 | +│ - Structured prompts │ │ - Thread dumps │ |
| 35 | +└─────────────────────────┘ └─────────────────────────┘ |
| 36 | +``` |
| 37 | + |
| 38 | +## Project Structure |
| 39 | + |
| 40 | +``` |
| 41 | +src/main/java/com/example/ai/jvmanalyzer/ |
| 42 | +├── Application.java # Spring Boot entry point, beans config |
| 43 | +├── WebhookController.java # REST endpoint for monitoring webhooks |
| 44 | +├── AnalyzerService.java # Orchestrates analysis workflow |
| 45 | +├── AiService.java # Bedrock integration via Spring AI |
| 46 | +└── S3Repository.java # S3 storage for profiling data and results |
| 47 | +``` |
| 48 | + |
| 49 | +## How It Works |
| 50 | + |
| 51 | +1. Monitoring system detects performance issue (high CPU, thread count, etc.) |
| 52 | +2. Alert webhook sent to `/webhook` with pod name and IP address |
| 53 | +3. Analyzer fetches thread dump from the pod's actuator endpoint |
| 54 | +4. Retrieves latest flamegraph/profiling data from S3 |
| 55 | +5. Sends both to Claude Sonnet 4 for analysis |
| 56 | +6. Stores thread dump, profiling data, and AI analysis report in S3 |
| 57 | + |
| 58 | +## Webhook Payload Format |
| 59 | + |
| 60 | +```json |
| 61 | +{ |
| 62 | + "alerts": [ |
| 63 | + { |
| 64 | + "labels": { |
| 65 | + "pod": "unicorn-store-spring-abc123", |
| 66 | + "instance": "10.0.1.50:8080" |
| 67 | + } |
| 68 | + } |
| 69 | + ] |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Analysis Report Contents |
| 74 | + |
| 75 | +The AI generates a structured report including: |
| 76 | +- Health status (Healthy/Degraded/Critical) |
| 77 | +- Thread analysis with state distribution |
| 78 | +- Top 3 critical issues with root cause and fix |
| 79 | +- Performance hotspots from flamegraph |
| 80 | +- Immediate and short-term recommendations |
| 81 | + |
| 82 | +## Dependencies |
| 83 | + |
| 84 | +| Dependency | Version | Purpose | |
| 85 | +|------------|---------|---------| |
| 86 | +| Spring Boot | 4.0.1 | Application framework | |
| 87 | +| Spring AI | 1.1.1 | Bedrock integration | |
| 88 | +| AWS SDK | 2.40.15 | S3 client | |
| 89 | +| Testcontainers | 2.0.3 | Integration testing | |
| 90 | +| jqwik | 1.9.3 | Property-based testing | |
| 91 | + |
| 92 | +## Configuration |
| 93 | + |
| 94 | +| Property | Default | Description | |
| 95 | +|----------|---------|-------------| |
| 96 | +| `analyzer.thread-dump.url-template` | `http://{podIp}:8080/actuator/threaddump` | Thread dump endpoint | |
| 97 | +| `analyzer.s3.bucket` | `ai-jvm-analyzer-bucket` | S3 bucket for storage | |
| 98 | +| `analyzer.s3.prefix.analysis` | `analysis/` | Prefix for analysis results | |
| 99 | +| `analyzer.s3.prefix.profiling` | `profiling/` | Prefix for profiling data | |
| 100 | +| `spring.ai.bedrock.converse.chat.options.model` | `anthropic.claude-sonnet-4-20250514-v1:0` | Bedrock model | |
| 101 | + |
| 102 | +## Environment Variables |
| 103 | + |
| 104 | +| Variable | Required | Description | |
| 105 | +|----------|----------|-------------| |
| 106 | +| `AWS_REGION` | Yes | AWS region for Bedrock and S3 | |
| 107 | +| `AWS_S3_BUCKET` | Yes | S3 bucket name | |
| 108 | + |
| 109 | +## Building |
| 110 | + |
| 111 | +```bash |
| 112 | +mvn package # Standard JAR |
| 113 | +mvn package -Pnative # Native image (GraalVM 25) |
| 114 | +mvn jib:dockerBuild # Container with Jib |
| 115 | +``` |
| 116 | + |
| 117 | +## API Endpoints |
| 118 | + |
| 119 | +| Method | Endpoint | Description | |
| 120 | +|--------|----------|-------------| |
| 121 | +| POST | `/webhook` | Receive monitoring alerts | |
| 122 | +| GET | `/actuator/health` | Health check | |
| 123 | +| GET | `/actuator/prometheus` | Metrics | |
| 124 | + |
| 125 | +## S3 Storage Layout |
| 126 | + |
| 127 | +``` |
| 128 | +s3://ai-jvm-analyzer-bucket/ |
| 129 | +├── profiling/ |
| 130 | +│ └── {pod-name}/ |
| 131 | +│ └── profile-{yyyyMMdd}-{timestamp}.html # Flamegraph data |
| 132 | +└── analysis/ |
| 133 | + ├── {timestamp}_threaddump_{pod-name}.json # Raw thread dump |
| 134 | + ├── {timestamp}_profiling_{pod-name}.html # Profiling snapshot |
| 135 | + └── {timestamp}_analysis_{pod-name}.md # AI analysis report |
| 136 | +``` |
| 137 | + |
| 138 | +## IAM Permissions Required |
| 139 | + |
| 140 | +```json |
| 141 | +{ |
| 142 | + "Version": "2012-10-17", |
| 143 | + "Statement": [ |
| 144 | + { |
| 145 | + "Effect": "Allow", |
| 146 | + "Action": [ |
| 147 | + "s3:GetObject", |
| 148 | + "s3:PutObject", |
| 149 | + "s3:ListBucket" |
| 150 | + ], |
| 151 | + "Resource": [ |
| 152 | + "arn:aws:s3:::ai-jvm-analyzer-bucket", |
| 153 | + "arn:aws:s3:::ai-jvm-analyzer-bucket/*" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "Effect": "Allow", |
| 158 | + "Action": "bedrock:InvokeModel", |
| 159 | + "Resource": "arn:aws:bedrock:*::foundation-model/anthropic.claude-sonnet-4-20250514-v1:0" |
| 160 | + } |
| 161 | + ] |
| 162 | +} |
| 163 | +``` |
0 commit comments