基本介绍

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

使用示例

主进程

main.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

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`)
}

执行结果

执行后,终端输出如下:

$ 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

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







Content Menu

  • No labels

1 Comment

  1. github.com/gogf/gf/v2 v2.3.3

    复制页面上的例子,为什么我这里主进程和子进程的traceId不一样呀?

    main.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.New()
    	g.Log().Debug(ctx, `this is main process`)
    	if err := gproc.ShellRun(ctx, `go run sub.go`); err != nil {
    		panic(err)
    	}
    }
    
    

    sub.go


    package main
    
    import (
    	"github.com/gogf/gf/v2/frame/g"
    	"github.com/gogf/gf/v2/os/gctx"
    )
    
    func main() {
    	ctx := gctx.New()
    	g.Log().Debug(ctx, `this is sub process`)
    }
    
    

    输出结果


    $ go run main.go
    2023-04-07 09:53:56.683 [DEBU] {cc99211b32845317bbe034455d43baab} this is main process
    2023-04-07 09:53:58.712 [DEBU] {f093079432845317943ee8125436a74f} this is sub process