# 使用 profiler 标签

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/help/idea/llms.txt).

> **Note:**
> pprof 配置文件中的   图   选项卡需要捆绑的   图   插件。 如果该选项卡显示插件要求消息，请按 `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS)) 打开设置，并导航到  `插件 | 已安装` 。 启用   图   插件，如有提示请重启 IDE。

Goroutine 是与其他函数／方法或方法并发运行的函数／方法或方法。 要创建一个 goroutine，使用  `go`  关键字，后跟函数调用（例如，  `go func(p string, rid int64)` ）。 但使用大量 goroutine 会使程序更难调试。 要区分 goroutines，您可以使用自定义数据为 goroutines 添加标签。

自 Go 1.9 起，您可以记录附加信息，以提供更多关于执行路径的上下文。 您可以将任何标签集记录为分析数据的一部分，并在以后使用这些标签来检查 profiler 输出。

例如，您有一个处理某处创建的事件的队列处理程序。 处理程序可以设置标签，以识别这些事件的创建位置。

在调试和核心转储分析期间，上下文信息可能会有所帮助。 例如，您可以使用此信息更轻松地找到特定的 goroutine。

## 添加标签

`runtime/pprof`  包含多个新功能，您可以使用这些功能添加标签。 最受欢迎的是  `Do`  功能。  `Do`  函数接收上下文，添加标签到此上下文，并将新上下文传递给 f 函数。

```GO
func Do(ctx context.Context, labels LabelSet, f func(context.Context))
```

`Do` 函数仅为当前goroutine写标签。 如果您在 f 函数中创建新的 goroutines，可以将 context 作为参数传递。

```GO
func main() {
    ctx := context.Background()
    for i := 0; i < 10; i++ {
        labels := pprof.Labels("path", "/api/profile", "userId", strconv.Itoa(rand.Intn(100)))
        go pprof.Do(ctx, labels, f)
    }
    time.Sleep(time.Second)
}
```

## 在 IntelliJ IDEA 中查看标签

出于说明目的， [从 GitHub 复制以下代码示例](https://github.com/apronichev/documentation-code-examples/blob/master/debuggerProfilerLabels/main.go)。

在调用  `println("ok")`  的地方设置断点。 要设置断点，请点击第 21 行的标记栏。 为  `main`  函数运行调试。 要开始调试，请点击   运行   图标 ( ![运行图标](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.actions.execute.svg)) 在  `main`  函数附近的边栏，并选择   调试 <run_debug_configuration_name>  。 从   Goroutines  列表中，观察可用的goroutines。

![没有标签的 goroutine 列表](https://resources.jetbrains.com.cn/help/img/idea/2026.3/go_list_of_goroutines_without_labels.png)

请按 `Ctrl+F2` (Windows), `⌘ F2` (macOS), `⌘ F2` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ .` (macOS System Shortcuts), `Ctrl+F2` (XWin), `Ctrl+F2` (GNOME), `Ctrl+2` (KDE), `Ctrl+F2` (Emacs), `Ctrl+F2` (Sublime Text), `Ctrl+F2` (Sublime Text (macOS)), `Shift+F5` (Visual Studio), `⇧ F5` (Visual Studio (macOS)), `Shift+F5` (NetBeans), `Ctrl+F2` (Eclipse), `⌘ F2` (Eclipse (macOS)) 停止调试。 删除  `f(ctx)` 调用并取消注释  `Do` 函数。 通过按 `⌃ ⇧ D` (Windows), `⌃ ⇧ D` (macOS), `⌃ ⇧ F9` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ D` (macOS System Shortcuts), `⌃ ⇧ D` (XWin), `⌃ ⇧ D` (GNOME), `⌃ ⇧ D` (KDE), `⌃ ⇧ D` (Emacs), `⌃ ⇧ D` (Sublime Text), `⌃ ⇧ D` (Sublime Text (macOS)), `⌃ ⇧ D` (Visual Studio), `⌃ ⇧ D` (Visual Studio (macOS)), `Ctrl+Shift+F5` (NetBeans), `⌃ ⇧ D` (Eclipse), `⌃ ⇧ D` (Eclipse (macOS)) 重新运行调试过程。 请探索   Goroutines   列表。 Goroutine 名称包括以下信息：  `/api/profile, userId: <some number>` 。 您可以使用此信息在调试或核心转储分析期间找到特定的 goroutine。

![标记的 goroutine 列表](https://resources.jetbrains.com.cn/help/img/idea/2026.3/go_list_of_goroutines_with_labels.png)

## 实用链接

* [如何在调试期间查找 Goroutine](https://blog.jetbrains.com/go/2020/03/03/how-to-find-goroutines-during-debugging/)：阅读教程，了解如何在 IntelliJ IDEA 中使用 profiler 标签。

* [rakyll.org 上的 Go Profiler 标签](https://rakyll.org/profiler-labels/)：阅读更多有关 Profiler 标签的信息。

