track_cold_starts
Monitor and analyze cold start patterns for AWS Lambda functions to identify performance bottlenecks and optimize execution efficiency.
Instructions
Track and analyze cold start patterns for Lambda functions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| functionName | Yes | Name of the Lambda function | |
| timeRange | No | Time range for cold start analysis (default: 24h) |
Implementation Reference
- src/cold-start-tracker.js:14-49 (handler)Core handler implementing the track_cold_starts tool logic: parses time range, queries CloudWatch Logs for INIT_START events, analyzes cold start patterns, calculates statistics, and generates recommendations.async trackColdStarts(functionName, timeRange) { const timeRangeMs = this.parseTimeRange(timeRange); const endTime = new Date(); const startTime = new Date(endTime.getTime() - timeRangeMs); const logGroupName = `/aws/lambda/${functionName}`; try { // Get cold start events const coldStartEvents = await this.getColdStartEvents(logGroupName, startTime, endTime); // Get all invocation events for comparison const allInvocations = await this.getAllInvocations(logGroupName, startTime, endTime); // Analyze patterns const analysis = this.analyzeColdStartPatterns(coldStartEvents, allInvocations, timeRange); return { total: coldStartEvents.length, rate: this.calculateColdStartRate(coldStartEvents.length, allInvocations.length), avgDuration: analysis.avgDuration, maxDuration: analysis.maxDuration, minDuration: analysis.minDuration, peakHours: analysis.peakHours, frequency: analysis.frequency, triggers: analysis.triggers, recommendations: this.generateRecommendations(analysis), timeline: this.createTimeline(coldStartEvents, timeRange), patterns: analysis.patterns, statistics: analysis.statistics }; } catch (error) { console.error('Error tracking cold starts:', error); return this.getEmptyResult(); } }
- index.js:286-315 (handler)MCP server wrapper handler for track_cold_starts: extracts arguments, delegates to ColdStartTracker, formats and returns the response content.async trackColdStarts(args) { const { functionName, timeRange = '24h' } = args; const coldStartData = await this.coldStartTracker.trackColdStarts( functionName, timeRange ); return { content: [ { type: 'text', text: `# Cold Start Analysis: ${functionName}\n\n` + `## Cold Start Statistics\n` + `- **Total Cold Starts**: ${coldStartData.total}\n` + `- **Cold Start Rate**: ${coldStartData.rate}%\n` + `- **Average Cold Start Duration**: ${coldStartData.avgDuration}ms\n` + `- **Longest Cold Start**: ${coldStartData.maxDuration}ms\n\n` + `## Cold Start Patterns\n` + `- **Peak Hours**: ${coldStartData.peakHours.join(', ')}\n` + `- **Frequency**: ${coldStartData.frequency}\n` + `- **Triggers**: ${coldStartData.triggers.join(', ')}\n\n` + `## Optimization Opportunities\n` + `${coldStartData.recommendations.map(rec => `- ${rec}`).join('\n')}\n\n` + `## Timeline\n` + `${this.formatColdStartTimeline(coldStartData.timeline)}` } ] }; }
- index.js:67-81 (schema)Input schema defining parameters for track_cold_starts tool: functionName (required) and timeRange.inputSchema: { type: 'object', properties: { functionName: { type: 'string', description: 'Name of the Lambda function' }, timeRange: { type: 'string', enum: ['1h', '6h', '24h', '7d'], description: 'Time range for cold start analysis (default: 24h)' } }, required: ['functionName'] }
- index.js:64-82 (registration)Registration of track_cold_starts tool in the MCP server's tool list, including name, description, and input schema.{ name: 'track_cold_starts', description: 'Track and analyze cold start patterns for Lambda functions', inputSchema: { type: 'object', properties: { functionName: { type: 'string', description: 'Name of the Lambda function' }, timeRange: { type: 'string', enum: ['1h', '6h', '24h', '7d'], description: 'Time range for cold start analysis (default: 24h)' } }, required: ['functionName'] } },
- src/cold-start-tracker.js:51-85 (helper)Helper method to fetch cold start events from CloudWatch Logs using filter for 'INIT_START' and paginate with nextToken.async getColdStartEvents(logGroupName, startTime, endTime) { const coldStartEvents = []; let nextToken = null; do { try { const command = new FilterLogEventsCommand({ logGroupName, startTime: startTime.getTime(), endTime: endTime.getTime(), filterPattern: '"INIT_START"', nextToken }); const response = await this.logsClient.send(command); if (response.events) { // Process each cold start event for (const event of response.events) { const coldStartData = await this.extractColdStartData(event, logGroupName); if (coldStartData) { coldStartEvents.push(coldStartData); } } } nextToken = response.nextToken; } catch (error) { console.error('Error fetching cold start events:', error); break; } } while (nextToken); return coldStartEvents; }