---
title: "进程管理-链路跟踪"
url: "https://goframe.org/docs/components/os-gproc-tracing"
markdown_url: "https://goframe.org/docs/components/os-gproc-tracing.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "组件列表"
category: "组件列表 / 系统相关 / 进程管理-gproc"
published: "2024-10-21"
updated: "2024-11-11"
description: "使用GoFrame框架进行进程管理和链路跟踪的方法。通过使用OpenTelemetry规范，支持跨进程的链路跟踪特性，非常适用于临时运行的进程。示例代码展示了如何在主进程中启动子进程并进行链路信息传递，展示了GoFrame框架在进程管理中的强大功能。"
keywords: ["GoFrame", "GoFrame框架", "进程管理", "链路跟踪", "OpenTelemetry", "跨进程", "主进程", "子进程", "gproc", "os-gproc-tracing"]
---

# 进程管理-链路跟踪

## 基本介绍

进程管理组件支持跨进程的链路跟踪特性，特别是对于一些临时运行的进程特别有用。框架整体的链路跟踪都是采用的 `OpenTelemetry` 规范。

## 使用示例

### 主进程

`main.go`

```go
package main

import (
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/os/gproc"
)

func main() {
    ctx := gctx.GetInitCtx()
    g.Log().Debug(ctx, `this is main process`)
    if err := gproc.ShellRun(ctx, `go run sub.go`); err != nil {
        panic(err)
    }
}
```

### 子进程

`sub.go`

```go
package main

import (
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
)

func main() {
    ctx := gctx.GetInitCtx()
    g.Log().Debug(ctx, `this is sub process`)
}
```

### 执行结果

执行后，终端输出如下：

```bash
$ go run main.go
2022-06-21 20:35:06.196 [DEBU] {00698a61e2a2fa1661da5d7993d72e8c} this is main process
2022-06-21 20:35:07.482 [DEBU] {00698a61e2a2fa1661da5d7993d72e8c} this is sub process
```

可以看到，链路信息已经自动传递给了子进程。
