基本介绍

GoFrame命令管理组件也支持跨进程的链路跟踪特性,特别是对于一些临时运行的进程特别有用。

框架整体的链路跟踪都是采用的OpenTelemetry规范。

使用示例

主进程

main.go

package main

import (
	"context"

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

var (
	Main = &gcmd.Command{
		Name:  "main",
		Brief: "main process",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.Log().Debug(ctx, `this is main process`)
			return gproc.ShellRun(ctx, `go run sub.go`)
		},
	}
)

func main() {
	Main.Run(gctx.New())
}

子进程

sub.go

package main

import (
	"context"

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

var (
	Sub = &gcmd.Command{
		Name:  "sub",
		Brief: "sub process",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.Log().Debug(ctx, `this is sub process`)
			return nil
		},
	}
)

func main() {
	Sub.Run(gctx.New())
}

执行结果

执行后,终端输出如下:

$ 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