-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhandler.go
More file actions
595 lines (500 loc) · 21.1 KB
/
handler.go
File metadata and controls
595 lines (500 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/google/pprof/profile"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/ZephyrDeng/pprof-analyzer-mcp/analyzer"
)
// AnalyzePprofArgs 定义 analyze_pprof 工具的输入参数
type AnalyzePprofArgs struct {
ProfileURI string `json:"profile_uri" jsonschema:"要分析的 pprof 文件的 URI (支持 'file://', 'http://', 'https://' 协议)"`
ProfileType string `json:"profile_type" jsonschema:"要分析的 pprof profile 的类型 (cpu, heap, goroutine, allocs, mutex, block)"`
TopN float64 `json:"top_n,omitempty" jsonschema:"返回结果的数量上限 (例如 Top 5, Top 10)"`
OutputFormat string `json:"output_format,omitempty" jsonschema:"分析结果的输出格式 (text, markdown, json, flamegraph-json)"`
}
// handleAnalyzePprof 处理分析 pprof 文件的请求。
func handleAnalyzePprof(_ context.Context, _ *mcp.CallToolRequest, args AnalyzePprofArgs) (*mcp.CallToolResult, any, error) {
if args.ProfileURI == "" {
return nil, nil, fmt.Errorf("missing required argument: profile_uri")
}
if args.ProfileType == "" {
return nil, nil, fmt.Errorf("missing required argument: profile_type")
}
// 设置默认值
if args.TopN <= 0 {
args.TopN = 5
}
if args.OutputFormat == "" {
args.OutputFormat = "flamegraph-json"
}
topN := int(args.TopN)
log.Printf("Handling analyze_pprof: URI=%s, Type=%s, TopN=%d, Format=%s", args.ProfileURI, args.ProfileType, topN, args.OutputFormat)
filePath, cleanup, err := getProfileAsFile(args.ProfileURI)
if err != nil {
return nil, nil, fmt.Errorf("failed to get profile file: %w", err)
}
defer cleanup()
file, err := os.Open(filePath)
if err != nil {
log.Printf("Error opening profile file '%s': %v", filePath, err)
return nil, nil, fmt.Errorf("failed to open profile file '%s': %w", filePath, err)
}
defer file.Close()
prof, err := profile.Parse(file)
if err != nil {
log.Printf("Error parsing profile file '%s': %v", filePath, err)
return nil, nil, fmt.Errorf("failed to parse profile file '%s': %w", filePath, err)
}
log.Printf("Successfully parsed profile file from path: %s", filePath)
var analysisResult string
var analysisErr error
switch args.ProfileType {
case "cpu":
analysisResult, analysisErr = analyzer.AnalyzeCPUProfile(prof, topN, args.OutputFormat)
case "heap":
analysisResult, analysisErr = analyzer.AnalyzeHeapProfile(prof, topN, args.OutputFormat)
case "goroutine":
analysisResult, analysisErr = analyzer.AnalyzeGoroutineProfile(prof, topN, args.OutputFormat)
case "allocs":
analysisResult, analysisErr = analyzer.AnalyzeAllocsProfile(prof, topN, args.OutputFormat)
case "mutex":
analysisResult, analysisErr = analyzer.AnalyzeMutexProfile(prof, topN, args.OutputFormat)
case "block":
analysisResult, analysisErr = analyzer.AnalyzeBlockProfile(prof, topN, args.OutputFormat)
default:
analysisErr = fmt.Errorf("unsupported profile type: '%s'", args.ProfileType)
}
if analysisErr != nil {
log.Printf("Analysis error for type '%s': %v", args.ProfileType, analysisErr)
return nil, nil, analysisErr
}
log.Printf("Analysis successful for type '%s'. Result length: %d", args.ProfileType, len(analysisResult))
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: analysisResult,
},
},
}, nil, nil
}
// GenerateFlamegraphArgs 定义 generate_flamegraph 工具的输入参数
type GenerateFlamegraphArgs struct {
ProfileURI string `json:"profile_uri" jsonschema:"要生成火焰图的 pprof 文件的 URI (支持 'file://', 'http://', 'https://' 协议)"`
ProfileType string `json:"profile_type" jsonschema:"要生成火焰图的 pprof profile 的类型 (cpu, heap, allocs, goroutine, mutex, block)"`
OutputSVGPath string `json:"output_svg_path" jsonschema:"生成的 SVG 火焰图文件的保存路径 (必须是绝对路径或相对于工作区的路径)"`
}
// handleGenerateFlamegraph 处理生成火焰图的请求。
func handleGenerateFlamegraph(_ context.Context, _ *mcp.CallToolRequest, args GenerateFlamegraphArgs) (*mcp.CallToolResult, any, error) {
if args.ProfileURI == "" {
return nil, nil, fmt.Errorf("missing required argument: profile_uri")
}
if args.ProfileType == "" {
return nil, nil, fmt.Errorf("missing required argument: profile_type")
}
if args.OutputSVGPath == "" {
return nil, nil, fmt.Errorf("missing required argument: output_svg_path")
}
log.Printf("Handling generate_flamegraph: URI=%s, Type=%s, Output=%s", args.ProfileURI, args.ProfileType, args.OutputSVGPath)
inputFilePath, cleanup, err := getProfileAsFile(args.ProfileURI)
if err != nil {
return nil, nil, fmt.Errorf("failed to get profile file for flamegraph: %w", err)
}
defer cleanup()
if !filepath.IsAbs(args.OutputSVGPath) {
cwd, err := os.Getwd()
if err != nil {
log.Printf("无法获取当前工作目录: %v", err)
} else {
args.OutputSVGPath = filepath.Join(cwd, args.OutputSVGPath)
log.Printf("将相对输出路径转换为绝对路径: %s", args.OutputSVGPath)
}
}
cmdArgs := []string{"tool", "pprof"}
switch args.ProfileType {
case "heap":
cmdArgs = append(cmdArgs, "-inuse_space")
case "allocs":
cmdArgs = append(cmdArgs, "-alloc_space")
case "cpu", "goroutine", "mutex", "block":
// No extra flags needed
default:
return nil, nil, fmt.Errorf("unsupported profile type for flamegraph: '%s'", args.ProfileType)
}
cmdArgs = append(cmdArgs, "-svg", "-output", args.OutputSVGPath, inputFilePath)
log.Printf("Executing command: go %s", strings.Join(cmdArgs, " "))
_, err = exec.LookPath("dot")
if err != nil {
errMsg := "Graphviz (dot 命令) 未找到或不在 PATH 中。生成 SVG 火焰图需要 Graphviz。\n" +
"请先安装 Graphviz。常见安装方式:\n" +
"- macOS (Homebrew): brew install graphviz\n" +
"- Debian/Ubuntu: sudo apt-get update && sudo apt-get install graphviz\n" +
"- CentOS/Fedora: sudo yum install graphviz 或 sudo dnf install graphviz\n" +
"- Windows (Chocolatey): choco install graphviz"
log.Println(errMsg)
return nil, nil, fmt.Errorf(errMsg)
}
log.Println("Graphviz (dot) found.")
cmd := exec.CommandContext(context.Background(), "go", cmdArgs...)
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error executing 'go tool pprof': %v\nOutput:\n%s", err, string(cmdOutput))
return nil, nil, fmt.Errorf("failed to generate flamegraph: %w. Output: %s", err, string(cmdOutput))
}
log.Printf("Successfully generated flamegraph: %s", args.OutputSVGPath)
log.Printf("pprof output:\n%s", string(cmdOutput))
resultText := fmt.Sprintf("火焰图已成功生成并保存到: %s", args.OutputSVGPath)
svgBytes, readErr := os.ReadFile(args.OutputSVGPath)
if readErr != nil {
log.Printf("成功生成 SVG 文件 '%s' 但读取失败: %v", args.OutputSVGPath, readErr)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: resultText,
},
},
}, nil, nil
}
svgContentStr := string(svgBytes)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: resultText,
},
&mcp.TextContent{
Text: svgContentStr,
},
},
}, nil, nil
}
// DetectMemoryLeaksArgs 定义 detect_memory_leaks 工具的输入参数
type DetectMemoryLeaksArgs struct {
OldProfileURI string `json:"old_profile_uri" jsonschema:"较早的 heap profile 的 URI,支持 'file://', 'http://', 'https://' 协议"`
NewProfileURI string `json:"new_profile_uri" jsonschema:"较新的 heap profile 的 URI,支持 'file://', 'http://', 'https://' 协议"`
Threshold float64 `json:"threshold,omitempty" jsonschema:"检测内存泄漏的增长阈值 (0.1 表示 10%)"`
Limit float64 `json:"limit,omitempty" jsonschema:"返回的潜在内存泄漏类型的最大数量"`
}
// handleDetectMemoryLeaks 处理内存泄漏检测的请求。
func handleDetectMemoryLeaks(_ context.Context, _ *mcp.CallToolRequest, args DetectMemoryLeaksArgs) (*mcp.CallToolResult, any, error) {
if args.OldProfileURI == "" {
return nil, nil, fmt.Errorf("missing required argument: old_profile_uri")
}
if args.NewProfileURI == "" {
return nil, nil, fmt.Errorf("missing required argument: new_profile_uri")
}
// 设置默认值
if args.Threshold == 0 {
args.Threshold = 0.1 // Default 10% growth
}
if args.Limit == 0 {
args.Limit = 10.0
}
limit := int(args.Limit)
if limit <= 0 {
limit = 10
}
log.Printf("Handling detect_memory_leaks: OldURI=%s, NewURI=%s, Threshold=%.2f, Limit=%d",
args.OldProfileURI, args.NewProfileURI, args.Threshold, limit)
// Get the old profile file
oldFilePath, oldCleanup, err := getProfileAsFile(args.OldProfileURI)
if err != nil {
return nil, nil, fmt.Errorf("failed to get old profile file: %w", err)
}
defer oldCleanup()
oldFile, err := os.Open(oldFilePath)
if err != nil {
log.Printf("Error opening old profile file '%s': %v", oldFilePath, err)
return nil, nil, fmt.Errorf("failed to open old profile file '%s': %w", oldFilePath, err)
}
defer oldFile.Close()
oldProf, err := profile.Parse(oldFile)
if err != nil {
log.Printf("Error parsing old profile file '%s': %v", oldFilePath, err)
return nil, nil, fmt.Errorf("failed to parse old profile file '%s': %w", oldFilePath, err)
}
log.Printf("Successfully parsed old profile file from path: %s", oldFilePath)
// Get the new profile file
newFilePath, newCleanup, err := getProfileAsFile(args.NewProfileURI)
if err != nil {
return nil, nil, fmt.Errorf("failed to get new profile file: %w", err)
}
defer newCleanup()
newFile, err := os.Open(newFilePath)
if err != nil {
log.Printf("Error opening new profile file '%s': %v", newFilePath, err)
return nil, nil, fmt.Errorf("failed to open new profile file '%s': %w", newFilePath, err)
}
defer newFile.Close()
newProf, err := profile.Parse(newFile)
if err != nil {
log.Printf("Error parsing new profile file '%s': %v", newFilePath, err)
return nil, nil, fmt.Errorf("failed to parse new profile file '%s': %w", newFilePath, err)
}
log.Printf("Successfully parsed new profile file from path: %s", newFilePath)
// Detect memory leaks
result, err := analyzer.DetectPotentialMemoryLeaks(oldProf, newProf, args.Threshold, limit)
if err != nil {
log.Printf("Error detecting memory leaks: %v", err)
return nil, nil, fmt.Errorf("failed to detect memory leaks: %w", err)
}
log.Printf("Memory leak detection completed successfully. Result length: %d", len(result))
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: result,
},
},
}, nil, nil
}
// OpenInteractivePprofArgs 定义 open_interactive_pprof 工具的输入参数
type OpenInteractivePprofArgs struct {
ProfileURI string `json:"profile_uri" jsonschema:"要分析的 pprof 文件的 URI (支持 'file://', 'http://', 'https://' 或本地路径)"`
HTTPAddress string `json:"http_address,omitempty" jsonschema:"指定 pprof Web UI 的监听地址和端口 (例如 ':8081'),如果省略默认为 ':8081'"`
}
// handleOpenInteractivePprof 处理打开交互式 pprof 的请求。
func handleOpenInteractivePprof(_ context.Context, _ *mcp.CallToolRequest, args OpenInteractivePprofArgs) (*mcp.CallToolResult, any, error) {
if args.ProfileURI == "" {
return nil, nil, fmt.Errorf("missing required argument: profile_uri")
}
httpAddress := args.HTTPAddress
if httpAddress == "" {
httpAddress = ":8081" // 默认端口
log.Printf("No http_address provided, using default: %s", httpAddress)
}
log.Printf("Handling open_interactive_pprof: URI=%s, Address=%s", args.ProfileURI, httpAddress)
inputFilePath, cleanup, err := getProfileAsFile(args.ProfileURI)
if err != nil {
return nil, nil, fmt.Errorf("failed to get profile file: %w", err)
}
// 注意:不能在这里 defer cleanup(),因为 pprof 进程需要持续访问文件
cmdArgs := []string{"tool", "pprof"}
cmdArgs = append(cmdArgs, fmt.Sprintf("-http=%s", httpAddress))
cmdArgs = append(cmdArgs, inputFilePath)
log.Printf("Preparing to execute command in background: go %s", strings.Join(cmdArgs, " "))
_, err = exec.LookPath("go")
if err != nil {
log.Println("Error: 'go' command not found in PATH.")
cleanup()
return nil, nil, fmt.Errorf("'go' command not found in PATH, cannot start pprof")
}
cmd := exec.CommandContext(context.Background(), "go", cmdArgs...)
err = cmd.Start()
if err != nil {
log.Printf("Error starting 'go tool pprof' in background: %v", err)
cleanup()
return nil, nil, fmt.Errorf("failed to start 'go tool pprof': %w", err)
}
pid := cmd.Process.Pid
pprofMutex.Lock()
runningPprofs[pid] = cmd.Process
pprofMutex.Unlock()
log.Printf("Successfully started 'go tool pprof' in background with PID: %d", pid)
resultText := fmt.Sprintf("已成功在后台启动 'go tool pprof' (PID: %d) 来分析 '%s'", pid, inputFilePath)
resultText += fmt.Sprintf(",监听地址约为 %s。", httpAddress)
resultText += "\n你可以使用 'disconnect_pprof_session' 工具并提供 PID 来尝试终止此进程。"
resultText += "\n注意:如果是远程 URL,下载的临时 pprof 文件在进程结束前不会被自动删除。"
log.Println(resultText)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: resultText,
},
},
}, nil, nil
}
// DisconnectPprofSessionArgs 定义 disconnect_pprof_session 工具的输入参数
type DisconnectPprofSessionArgs struct {
PID float64 `json:"pid" jsonschema:"要终止的后台 pprof 进程的 PID (由 'open_interactive_pprof' 返回)"`
HTTPAddress string `json:"http_address,omitempty" jsonschema:"指定 pprof Web UI 的监听地址和端口 (例如 ':8081'),如果省略 pprof 会自动选择"`
}
// handleDisconnectPprofSession 处理断开 pprof 会话的请求。
func handleDisconnectPprofSession(_ context.Context, _ *mcp.CallToolRequest, args DisconnectPprofSessionArgs) (*mcp.CallToolResult, any, error) {
if args.PID <= 0 {
return nil, nil, fmt.Errorf("invalid PID: %d", int(args.PID))
}
pid := int(args.PID)
log.Printf("Handling disconnect_pprof_session for PID: %d", pid)
pprofMutex.Lock()
process, exists := runningPprofs[pid]
if !exists {
pprofMutex.Unlock()
log.Printf("PID %d not found in running pprof sessions.", pid)
return nil, nil, fmt.Errorf("未找到 PID 为 %d 的正在运行的 pprof 会话", pid)
}
delete(runningPprofs, pid)
pprofMutex.Unlock()
log.Printf("Attempting to terminate process with PID: %d", pid)
err := process.Signal(os.Interrupt)
if err != nil {
log.Printf("Failed to send Interrupt signal to PID %d: %v. Trying Kill signal.", pid, err)
err = process.Signal(os.Kill)
if err != nil {
log.Printf("Failed to send Kill signal to PID %d: %v", pid, err)
return nil, nil, fmt.Errorf("尝试终止 PID %d 失败:%w", pid, err)
}
}
_, err = process.Wait()
if err != nil && !strings.Contains(err.Error(), "wait: no child processes") && !strings.Contains(err.Error(), "signal:") {
log.Printf("Warning: Error waiting for process PID %d after signaling: %v", pid, err)
}
resultText := fmt.Sprintf("已成功向 PID %d 发送终止信号。", pid)
log.Println(resultText)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: resultText,
},
},
}, nil, nil
}
// CompareProfilesArgs 定义 compare_profiles 工具的输入参数
type CompareProfilesArgs struct {
BaselineProfileURI string `json:"baseline_profile_uri" jsonschema:"基线 profile 的 URI (旧版本),支持 'file://', 'http://', 'https://' 协议"`
TargetProfileURI string `json:"target_profile_uri" jsonschema:"目标 profile 的 URI (新版本),支持 'file://', 'http://', 'https://' 协议"`
ProfileType string `json:"profile_type" jsonschema:"要比较的 pprof profile 的类型 (cpu, heap, allocs, mutex, block)"`
TopN float64 `json:"top_n,omitempty" jsonschema:"返回结果的数量上限"`
OutputFormat string `json:"output_format,omitempty" jsonschema:"输出格式 (text, markdown, json)"`
}
// handleCompareProfiles 处理 profile 比较的请求。
func handleCompareProfiles(_ context.Context, _ *mcp.CallToolRequest, args CompareProfilesArgs) (*mcp.CallToolResult, any, error) {
if args.BaselineProfileURI == "" {
return nil, nil, fmt.Errorf("missing required argument: baseline_profile_uri")
}
if args.TargetProfileURI == "" {
return nil, nil, fmt.Errorf("missing required argument: target_profile_uri")
}
if args.ProfileType == "" {
return nil, nil, fmt.Errorf("missing required argument: profile_type")
}
// 设置默认值
if args.TopN <= 0 {
args.TopN = 10
}
if args.OutputFormat == "" {
args.OutputFormat = "markdown"
}
topN := int(args.TopN)
log.Printf("Handling compare_profiles: Baseline=%s, Target=%s, Type=%s, TopN=%d, Format=%s",
args.BaselineProfileURI, args.TargetProfileURI, args.ProfileType, topN, args.OutputFormat)
// 获取基线 profile
baselinePath, baselineCleanup, err := getProfileAsFile(args.BaselineProfileURI)
if err != nil {
return nil, nil, fmt.Errorf("failed to get baseline profile file: %w", err)
}
defer baselineCleanup()
baselineFile, err := os.Open(baselinePath)
if err != nil {
return nil, nil, fmt.Errorf("failed to open baseline profile file '%s': %w", baselinePath, err)
}
defer baselineFile.Close()
baselineProf, err := profile.Parse(baselineFile)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse baseline profile file '%s': %w", baselinePath, err)
}
// 获取目标 profile
targetPath, targetCleanup, err := getProfileAsFile(args.TargetProfileURI)
if err != nil {
return nil, nil, fmt.Errorf("failed to get target profile file: %w", err)
}
defer targetCleanup()
targetFile, err := os.Open(targetPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to open target profile file '%s': %w", targetPath, err)
}
defer targetFile.Close()
targetProf, err := profile.Parse(targetFile)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse target profile file '%s': %w", targetPath, err)
}
// 执行比较
result, err := analyzer.CompareProfiles(baselineProf, targetProf, args.ProfileType, topN, args.OutputFormat)
if err != nil {
return nil, nil, fmt.Errorf("failed to compare profiles: %w", err)
}
log.Printf("Profile comparison completed successfully. Result length: %d", len(result))
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: result,
},
},
}, nil, nil
}
// AnalyzeHeapTimeSeriesArgs 定义 analyze_heap_time_series 工具的输入参数
type AnalyzeHeapTimeSeriesArgs struct {
ProfileURIs []string `json:"profile_uris" jsonschema:"多个 heap profile 的 URI 数组(按时间顺序),支持 'file://', 'http://', 'https://' 协议"`
Labels []string `json:"labels,omitempty" jsonschema:"每个时间点的标签数组(可选),长度必须与 profile_uris 相同"`
OutputFormat string `json:"output_format,omitempty" jsonschema:"输出格式 (text, markdown, json)"`
}
// handleAnalyzeHeapTimeSeries 处理内存时序分析的请求。
func handleAnalyzeHeapTimeSeries(_ context.Context, _ *mcp.CallToolRequest, args AnalyzeHeapTimeSeriesArgs) (*mcp.CallToolResult, any, error) {
if len(args.ProfileURIs) < 3 {
return nil, nil, fmt.Errorf("至少需要 3 个 profile 来进行时序分析,当前只有 %d 个", len(args.ProfileURIs))
}
// 设置默认值
if args.OutputFormat == "" {
args.OutputFormat = "markdown"
}
// 如果没有提供标签,生成默认标签
labels := args.Labels
if len(labels) == 0 {
labels = make([]string, len(args.ProfileURIs))
for i := range args.ProfileURIs {
labels[i] = fmt.Sprintf("T%d", i+1)
}
} else if len(labels) != len(args.ProfileURIs) {
return nil, nil, fmt.Errorf("标签数量 (%d) 与 profile 数量 (%d) 不匹配", len(labels), len(args.ProfileURIs))
}
log.Printf("Handling analyze_heap_time_series: profiles=%d, format=%s", len(args.ProfileURIs), args.OutputFormat)
// 解析所有 profile
profiles := make([]*profile.Profile, len(args.ProfileURIs))
for i, uri := range args.ProfileURIs {
filePath, cleanup, err := getProfileAsFile(uri)
if err != nil {
return nil, nil, fmt.Errorf("failed to get profile file #%d: %w", i+1, err)
}
defer cleanup()
file, err := os.Open(filePath)
if err != nil {
return nil, nil, fmt.Errorf("failed to open profile file #%d '%s': %w", i+1, filePath, err)
}
defer file.Close()
prof, err := profile.Parse(file)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse profile file #%d '%s': %w", i+1, filePath, err)
}
profiles[i] = prof
log.Printf("Successfully parsed profile #%d: %d samples", i+1, len(prof.Sample))
}
// 执行时序分析
result, err := analyzer.AnalyzeHeapTimeSeries(profiles, labels, args.OutputFormat)
if err != nil {
return nil, nil, fmt.Errorf("failed to analyze time series: %w", err)
}
log.Printf("Heap time series analysis completed successfully. Result length: %d", len(result))
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{
Text: result,
},
},
}, nil, nil
}
// getMimeTypeForFormat 根据输出格式返回对应的 MIME 类型
func getMimeTypeForFormat(format string) string {
switch format {
case "text":
return "text/plain"
case "markdown":
return "text/markdown"
case "json", "flamegraph-json":
return "application/json"
default:
return "text/plain"
}
}