š¤ Amazon CodeGuru
CodeGuru uses machine learning to analyze your code and your running application. It finds performance bottlenecks, identifies expensive code paths, and recommends specific fixes ā automatically.
š Covers: CodeGuru Reviewer Ā· CodeGuru Profiler Ā· Application Profiling Ā· Performance Bottlenecks Ā· RecommendationsWhat is Amazon CodeGuru?
CodeGuru is an ML-powered developer tool with two distinct services:
š CodeGuru Reviewer
Analyzes code BEFORE it runs. Integrates with GitHub, CodeCommit, Bitbucket. Reviews pull requests and comments on code that looks problematic: security vulnerabilities, resource leaks, concurrency issues, AWS best practice violations.
Think of it as an automatic expert code review on every pull request.
š CodeGuru Profiler
Analyzes code WHILE it runs. Instruments your running application (on EC2, Lambda, ECS, or on-premises) and collects CPU and memory usage data. Shows which lines of code are consuming the most resources.
Think of it as a continuous performance X-ray of your live application.
CodeGuru Profiler ā Deep Dive
The Profiler is more relevant for Domain 4 (Troubleshooting & Optimization). Here's how it works:
š§ How Profiler Works ā Under the Hood
Instrumentation
You add the CodeGuru Profiler agent (library) to your application. For Lambda, there's a special layer. The agent runs alongside your code and samples its execution.
Sampling
The agent periodically (every few milliseconds) takes a snapshot of the call stack ā which functions are currently executing. Over time, this reveals which functions are called most frequently and consume the most CPU time.
Aggregation
Samples are aggregated into a "profiling group" and uploaded to the CodeGuru service. This happens asynchronously with very low overhead (<1% CPU).
Analysis and Visualization
CodeGuru visualizes the data as a flame graph (also called a call graph). The wider a bar, the more time is spent in that function. You can see at a glance where your CPU time goes.
Recommendations
CodeGuru provides specific, actionable recommendations. Example: "Function processOrders() on line 47 of OrderService.java is consuming 68% of CPU time. Consider caching the result of the database query on line 52."
š„ Flame Graphs ā Reading Performance Data
š What Profiler Identifies
š° Expensive Code
Lines or functions that consume disproportionate CPU time. Often found to be: inefficient algorithms (O(n²) where O(n) would work), excessive object creation in loops, redundant database calls.
š Redundant Work
Code that repeatedly computes the same result. Example: calling the same database query inside a loop 100 times instead of calling it once and caching the result.
ā±ļø Wait Time
Time spent waiting for I/O (database calls, network calls, disk reads). Helps identify whether your bottleneck is CPU-bound or I/O-bound (different optimization strategies for each).
š Anomalies
Detects when CPU usage suddenly increases over time ā might indicate a memory leak causing garbage collection pressure, or a gradual performance regression.
CodeGuru Reviewer ā Smart Code Reviews
Reviewer integrates with your source code repositories and reviews code on pull requests (PRs). It uses ML trained on thousands of open source repositories and Amazon's own code to spot issues.
Types of Issues Reviewer Finds
| Category | Examples |
|---|---|
| AWS Best Practices | Not closing S3 connections, incorrect SDK usage, missing retry logic |
| Resource Leaks | Unclosed database connections, streams not closed in finally blocks |
| Security | Hardcoded credentials, insecure random number generation, potential injection flaws |
| Concurrency | Race conditions, thread safety issues, deadlock risks |
| Input Validation | Missing null checks, unvalidated user input |
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); (inside a Lambda handler function)Reviewer comment: "This creates a new DynamoDB client on every invocation. Move the client initialization to module level so it's reused across warm invocations. This reduces latency by ~50ms per call."
Why this matters: Creating SDK clients is expensive. In Lambda, module-level variables persist across warm invocations.
CodeGuru Integrations
Source Control
GitHub, GitHub Enterprise, GitLab, Bitbucket, AWS CodeCommit ā Reviewer analyzes PRs automatically
Languages
Java and Python (Profiler). Java and Python (Reviewer). More languages via community extensions.
Compute
Profiler works on EC2, Lambda (via layer), ECS, EKS, Fargate, and even on-premises JVMs.