Skip to content

Commit

Permalink
add tracing feature for package gproc (#1923)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Jun 21, 2022
1 parent f0568b4 commit 2bcee01
Show file tree
Hide file tree
Showing 22 changed files with 355 additions and 164 deletions.
17 changes: 10 additions & 7 deletions cmd/gf/internal/cmd/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,14 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
}
packCmd := fmt.Sprintf(`gf pack %s %s`, in.PackSrc, in.PackDst)
mlog.Print(packCmd)
gproc.MustShellRun(packCmd)
gproc.MustShellRun(ctx, packCmd)
}

// Injected information by building flags.
ldFlags := fmt.Sprintf(`-X 'github.com/gogf/gf/v2/os/gbuild.builtInVarStr=%v'`, c.getBuildInVarStr(in))
ldFlags := fmt.Sprintf(
`-X 'github.com/gogf/gf/v2/os/gbuild.builtInVarStr=%v'`,
c.getBuildInVarStr(ctx, in),
)

// start building
mlog.Print("start building...")
Expand Down Expand Up @@ -261,7 +264,7 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
// It's not necessary printing the complete command string.
cmdShow, _ := gregex.ReplaceString(`\s+(-ldflags ".+?")\s+`, " ", cmd)
mlog.Print(cmdShow)
if result, err := gproc.ShellExec(cmd); err != nil {
if result, err := gproc.ShellExec(ctx, cmd); err != nil {
mlog.Printf(
"failed to build, os:%s, arch:%s, error:\n%s\n\n%s\n",
system, arch, gstr.Trim(result),
Expand All @@ -287,12 +290,12 @@ buildDone:

// getBuildInVarMapJson retrieves and returns the custom build-in variables in configuration
// file as json.
func (c cBuild) getBuildInVarStr(in cBuildInput) string {
func (c cBuild) getBuildInVarStr(ctx context.Context, in cBuildInput) string {
buildInVarMap := in.VarMap
if buildInVarMap == nil {
buildInVarMap = make(g.Map)
}
buildInVarMap["builtGit"] = c.getGitCommit()
buildInVarMap["builtGit"] = c.getGitCommit(ctx)
buildInVarMap["builtTime"] = gtime.Now().String()
b, err := json.Marshal(buildInVarMap)
if err != nil {
Expand All @@ -302,13 +305,13 @@ func (c cBuild) getBuildInVarStr(in cBuildInput) string {
}

// getGitCommit retrieves and returns the latest git commit hash string if present.
func (c cBuild) getGitCommit() string {
func (c cBuild) getGitCommit(ctx context.Context) string {
if gproc.SearchBinary("git") == "" {
return ""
}
var (
cmd = `git log -1 --format="%cd %H" --date=format:"%Y-%m-%d %H:%M:%S"`
s, _ = gproc.ShellExec(cmd)
s, _ = gproc.ShellExec(ctx, cmd)
)
mlog.Debug(cmd)
if s != "" {
Expand Down
14 changes: 7 additions & 7 deletions cmd/gf/internal/cmd/cmd_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
// Binary build.
in.Build += " --exit"
if in.Main != "" {
if err = gproc.ShellRun(fmt.Sprintf(`gf build %s %s`, in.Main, in.Build)); err != nil {
if err = gproc.ShellRun(ctx, fmt.Sprintf(`gf build %s %s`, in.Main, in.Build)); err != nil {
return
}
}

// Shell executing.
if in.Shell != "" && gfile.Exists(in.Shell) {
if err = c.exeDockerShell(in.Shell); err != nil {
if err = c.exeDockerShell(ctx, in.Shell); err != nil {
return
}
}
Expand All @@ -116,7 +116,7 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
}
for i, dockerTag := range dockerTags {
if i > 0 {
err = gproc.ShellRun(fmt.Sprintf(`docker tag %s %s`, dockerTagBase, dockerTag))
err = gproc.ShellRun(ctx, fmt.Sprintf(`docker tag %s %s`, dockerTagBase, dockerTag))
if err != nil {
return
}
Expand All @@ -130,7 +130,7 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
if in.Extra != "" {
dockerBuildOptions = fmt.Sprintf(`%s %s`, dockerBuildOptions, in.Extra)
}
err = gproc.ShellRun(fmt.Sprintf(`docker build -f %s . %s`, in.File, dockerBuildOptions))
err = gproc.ShellRun(ctx, fmt.Sprintf(`docker build -f %s . %s`, in.File, dockerBuildOptions))
if err != nil {
return
}
Expand All @@ -144,18 +144,18 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
if dockerTag == "" {
continue
}
err = gproc.ShellRun(fmt.Sprintf(`docker push %s`, dockerTag))
err = gproc.ShellRun(ctx, fmt.Sprintf(`docker push %s`, dockerTag))
if err != nil {
return
}
}
return
}

func (c cDocker) exeDockerShell(shellFilePath string) error {
func (c cDocker) exeDockerShell(ctx context.Context, shellFilePath string) error {
if gfile.ExtName(shellFilePath) == "sh" && runtime.GOOS == "windows" {
mlog.Debugf(`ignore shell file "%s", as it cannot be run on windows system`, shellFilePath)
return nil
}
return gproc.ShellRun(gfile.GetContents(shellFilePath))
return gproc.ShellRun(ctx, gfile.GetContents(shellFilePath))
}
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/cmd_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type cEnvInput struct {
type cEnvOutput struct{}

func (c cEnv) Index(ctx context.Context, in cEnvInput) (out *cEnvOutput, err error) {
result, err := gproc.ShellExec("go env")
result, err := gproc.ShellExec(ctx, "go env")
if err != nil {
mlog.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/cmd_gen_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c cGenPb) Pb(ctx context.Context, in cGenPbInput) (out *cGenPbOutput, err
parsingCommand += " -I" + goPathSrc
}
mlog.Print(parsingCommand)
if output, err := gproc.ShellExec(parsingCommand); err != nil {
if output, err := gproc.ShellExec(ctx, parsingCommand); err != nil {
mlog.Print(output)
mlog.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/cmd_gen_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c cGenService) Service(ctx context.Context, in cGenServiceInput) (out *cGe
`%s gen service -packages=%s`,
gfile.SelfName(), gfile.Basename(watchFileDir),
)
err = gproc.ShellRun(command)
err = gproc.ShellRun(ctx, command)
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err
if in.Name != "." {
updateCommand = fmt.Sprintf(`cd %s && %s`, in.Name, updateCommand)
}
if err = gproc.ShellRun(updateCommand); err != nil {
if err = gproc.ShellRun(ctx, updateCommand); err != nil {
mlog.Fatal(err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/gf/internal/cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
gtimer.SetTimeout(ctx, 1500*gtime.MS, func(ctx context.Context) {
defer dirty.Set(false)
mlog.Printf(`go file changes: %s`, event.String())
app.Run()
app.Run(ctx)
})
})
if err != nil {
mlog.Fatal(err)
}
go app.Run()
go app.Run(ctx)
select {}
}

func (app *cRunApp) Run() {
func (app *cRunApp) Run(ctx context.Context) {
// Rebuild and run the codes.
renamePath := ""
mlog.Printf("build: %s", app.File)
Expand All @@ -132,7 +132,7 @@ func (app *cRunApp) Run() {
app.File,
)
mlog.Print(buildCommand)
result, err := gproc.ShellExec(buildCommand)
result, err := gproc.ShellExec(ctx, buildCommand)
if err != nil {
mlog.Printf("build error: \n%s%s", result, err.Error())
return
Expand All @@ -154,7 +154,7 @@ func (app *cRunApp) Run() {
} else {
process = gproc.NewProcessCmd(outputPath, gstr.SplitAndTrim(" ", app.Args))
}
if pid, err := process.Start(); err != nil {
if pid, err := process.Start(ctx); err != nil {
mlog.Printf("build running error: %s", err.Error())
} else {
mlog.Printf("build running pid: %d", pid)
Expand Down
5 changes: 3 additions & 2 deletions cmd/gf/internal/utility/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"context"
"fmt"

"github.com/gogf/gf/cmd/gf/v2/internal/consts"
Expand Down Expand Up @@ -31,7 +32,7 @@ func GoFmt(path string) {
mlog.Fatal(`command "gofmt" not found`)
}
var command = fmt.Sprintf(`%s -w %s`, gofmtPath, path)
result, err := gproc.ShellExec(command)
result, err := gproc.ShellExec(context.Background(), command)
if err != nil {
mlog.Fatalf(`error executing command "%s": %s`, command, result)
}
Expand All @@ -43,7 +44,7 @@ func GoImports(path string) {
mlog.Fatal(`command "goimports" not found`)
}
var command = fmt.Sprintf(`%s -w %s`, goimportsPath, path)
result, err := gproc.ShellExec(command)
result, err := gproc.ShellExec(context.Background(), command)
if err != nil {
mlog.Fatalf(`error executing command "%s": %s`, command, result)
}
Expand Down
4 changes: 2 additions & 2 deletions example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/gogf/gf/contrib/registry/etcd/v2 v2.1.0-rc3.0.20220523034830-510fa3faf03f
github.com/gogf/gf/contrib/registry/polaris/v2 v2.0.0-rc2
github.com/gogf/gf/contrib/trace/jaeger/v2 v2.0.0-rc2
github.com/gogf/gf/v2 v2.1.0-rc3.0.20220523034830-510fa3faf03f
github.com/gogf/katyusha v0.4.0
github.com/gogf/gf/v2 v2.1.0-rc4.0.20220620123459-52056644d499
github.com/gogf/katyusha v0.4.1-0.20220620125113-f55d6f739773
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2
github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f
Expand Down
2 changes: 2 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg78
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogf/katyusha v0.4.0 h1:mQVfXHhzC+UQf11Q8HAk9IOhQZ1VMXqGUqezyywZUOs=
github.com/gogf/katyusha v0.4.0/go.mod h1:nqsIWBsImnq9+OLlfB6iNef6ZLRyR2L1Bnk9h2aZvKs=
github.com/gogf/katyusha v0.4.1-0.20220620125113-f55d6f739773 h1:YQBLawktoymYtPGs9idE9JS5Wqd3SjIzUEZOPKCdSw0=
github.com/gogf/katyusha v0.4.1-0.20220620125113-f55d6f739773/go.mod h1:Z0GCeHXz1UI0HtA0K45c6TzEGM4DL/PLatS747/WarI=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down
25 changes: 25 additions & 0 deletions example/trace/processes/gcmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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/sub.go`)
},
}
)

func main() {
Main.Run(gctx.New())
}
24 changes: 24 additions & 0 deletions example/trace/processes/gcmd/sub/sub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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())
}
15 changes: 15 additions & 0 deletions example/trace/processes/gproc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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/sub.go`); err != nil {
panic(err)
}
}
11 changes: 11 additions & 0 deletions example/trace/processes/gproc/sub/sub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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`)
}
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_server_admin_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func forkReloadProcess(ctx context.Context, newExeFilePath ...string) error {
}
buffer, _ := gjson.Encode(sfm)
p.Env = append(p.Env, adminActionReloadEnvKey+"="+string(buffer))
if _, err := p.Start(); err != nil {
if _, err := p.Start(ctx); err != nil {
glog.Errorf(
ctx,
"%d: fork process failed, error:%s, %s",
Expand All @@ -169,7 +169,7 @@ func forkRestartProcess(ctx context.Context, newExeFilePath ...string) error {
env := os.Environ()
env = append(env, adminActionRestartEnvKey+"=1")
p := gproc.NewProcess(path, os.Args, env)
if _, err := p.Start(); err != nil {
if _, err := p.Start(ctx); err != nil {
glog.Errorf(
ctx,
`%d: fork process failed, error:%s, are you running using "go run"?`,
Expand Down
34 changes: 27 additions & 7 deletions os/genv/genv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package genv

import (
"fmt"
"os"
"strings"

Expand All @@ -25,13 +26,7 @@ func All() []string {

// Map returns a copy of strings representing the environment as a map.
func Map() map[string]string {
m := make(map[string]string)
i := 0
for _, s := range os.Environ() {
i = strings.IndexByte(s, '=')
m[s[0:i]] = s[i+1:]
}
return m
return MapFromEnv(os.Environ())
}

// Get creates and returns a Var with the value of the environment variable
Expand Down Expand Up @@ -117,3 +112,28 @@ func Build(m map[string]string) []string {
}
return array
}

// MapFromEnv converts environment variables from slice to map.
func MapFromEnv(envs []string) map[string]string {
m := make(map[string]string)
i := 0
for _, s := range envs {
i = strings.IndexByte(s, '=')
m[s[0:i]] = s[i+1:]
}
return m
}

// MapToEnv converts environment variables from map to slice.
func MapToEnv(m map[string]string) []string {
envs := make([]string, 0)
for k, v := range m {
envs = append(envs, fmt.Sprintf(`%s=%s`, k, v))
}
return envs
}

// Filter filters repeated items from given environment variables.
func Filter(envs []string) []string {
return MapToEnv(MapFromEnv(envs))
}
Loading

0 comments on commit 2bcee01

Please sign in to comment.