Skip to content

Commit 2bcee01

Browse files
authored
add tracing feature for package gproc (#1923)
1 parent f0568b4 commit 2bcee01

File tree

22 files changed

+355
-164
lines changed

22 files changed

+355
-164
lines changed

cmd/gf/internal/cmd/cmd_build.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,14 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
206206
}
207207
packCmd := fmt.Sprintf(`gf pack %s %s`, in.PackSrc, in.PackDst)
208208
mlog.Print(packCmd)
209-
gproc.MustShellRun(packCmd)
209+
gproc.MustShellRun(ctx, packCmd)
210210
}
211211

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

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

288291
// getBuildInVarMapJson retrieves and returns the custom build-in variables in configuration
289292
// file as json.
290-
func (c cBuild) getBuildInVarStr(in cBuildInput) string {
293+
func (c cBuild) getBuildInVarStr(ctx context.Context, in cBuildInput) string {
291294
buildInVarMap := in.VarMap
292295
if buildInVarMap == nil {
293296
buildInVarMap = make(g.Map)
294297
}
295-
buildInVarMap["builtGit"] = c.getGitCommit()
298+
buildInVarMap["builtGit"] = c.getGitCommit(ctx)
296299
buildInVarMap["builtTime"] = gtime.Now().String()
297300
b, err := json.Marshal(buildInVarMap)
298301
if err != nil {
@@ -302,13 +305,13 @@ func (c cBuild) getBuildInVarStr(in cBuildInput) string {
302305
}
303306

304307
// getGitCommit retrieves and returns the latest git commit hash string if present.
305-
func (c cBuild) getGitCommit() string {
308+
func (c cBuild) getGitCommit(ctx context.Context) string {
306309
if gproc.SearchBinary("git") == "" {
307310
return ""
308311
}
309312
var (
310313
cmd = `git log -1 --format="%cd %H" --date=format:"%Y-%m-%d %H:%M:%S"`
311-
s, _ = gproc.ShellExec(cmd)
314+
s, _ = gproc.ShellExec(ctx, cmd)
312315
)
313316
mlog.Debug(cmd)
314317
if s != "" {

cmd/gf/internal/cmd/cmd_docker.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
8888
// Binary build.
8989
in.Build += " --exit"
9090
if in.Main != "" {
91-
if err = gproc.ShellRun(fmt.Sprintf(`gf build %s %s`, in.Main, in.Build)); err != nil {
91+
if err = gproc.ShellRun(ctx, fmt.Sprintf(`gf build %s %s`, in.Main, in.Build)); err != nil {
9292
return
9393
}
9494
}
9595

9696
// Shell executing.
9797
if in.Shell != "" && gfile.Exists(in.Shell) {
98-
if err = c.exeDockerShell(in.Shell); err != nil {
98+
if err = c.exeDockerShell(ctx, in.Shell); err != nil {
9999
return
100100
}
101101
}
@@ -116,7 +116,7 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
116116
}
117117
for i, dockerTag := range dockerTags {
118118
if i > 0 {
119-
err = gproc.ShellRun(fmt.Sprintf(`docker tag %s %s`, dockerTagBase, dockerTag))
119+
err = gproc.ShellRun(ctx, fmt.Sprintf(`docker tag %s %s`, dockerTagBase, dockerTag))
120120
if err != nil {
121121
return
122122
}
@@ -130,7 +130,7 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
130130
if in.Extra != "" {
131131
dockerBuildOptions = fmt.Sprintf(`%s %s`, dockerBuildOptions, in.Extra)
132132
}
133-
err = gproc.ShellRun(fmt.Sprintf(`docker build -f %s . %s`, in.File, dockerBuildOptions))
133+
err = gproc.ShellRun(ctx, fmt.Sprintf(`docker build -f %s . %s`, in.File, dockerBuildOptions))
134134
if err != nil {
135135
return
136136
}
@@ -144,18 +144,18 @@ func (c cDocker) Index(ctx context.Context, in cDockerInput) (out *cDockerOutput
144144
if dockerTag == "" {
145145
continue
146146
}
147-
err = gproc.ShellRun(fmt.Sprintf(`docker push %s`, dockerTag))
147+
err = gproc.ShellRun(ctx, fmt.Sprintf(`docker push %s`, dockerTag))
148148
if err != nil {
149149
return
150150
}
151151
}
152152
return
153153
}
154154

155-
func (c cDocker) exeDockerShell(shellFilePath string) error {
155+
func (c cDocker) exeDockerShell(ctx context.Context, shellFilePath string) error {
156156
if gfile.ExtName(shellFilePath) == "sh" && runtime.GOOS == "windows" {
157157
mlog.Debugf(`ignore shell file "%s", as it cannot be run on windows system`, shellFilePath)
158158
return nil
159159
}
160-
return gproc.ShellRun(gfile.GetContents(shellFilePath))
160+
return gproc.ShellRun(ctx, gfile.GetContents(shellFilePath))
161161
}

cmd/gf/internal/cmd/cmd_env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type cEnvInput struct {
2626
type cEnvOutput struct{}
2727

2828
func (c cEnv) Index(ctx context.Context, in cEnvInput) (out *cEnvOutput, err error) {
29-
result, err := gproc.ShellExec("go env")
29+
result, err := gproc.ShellExec(ctx, "go env")
3030
if err != nil {
3131
mlog.Fatal(err)
3232
}

cmd/gf/internal/cmd/cmd_gen_pb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (c cGenPb) Pb(ctx context.Context, in cGenPbInput) (out *cGenPbOutput, err
5757
parsingCommand += " -I" + goPathSrc
5858
}
5959
mlog.Print(parsingCommand)
60-
if output, err := gproc.ShellExec(parsingCommand); err != nil {
60+
if output, err := gproc.ShellExec(ctx, parsingCommand); err != nil {
6161
mlog.Print(output)
6262
mlog.Fatal(err)
6363
}

cmd/gf/internal/cmd/cmd_gen_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (c cGenService) Service(ctx context.Context, in cGenServiceInput) (out *cGe
8080
`%s gen service -packages=%s`,
8181
gfile.SelfName(), gfile.Basename(watchFileDir),
8282
)
83-
err = gproc.ShellRun(command)
83+
err = gproc.ShellRun(ctx, command)
8484
return
8585
}
8686

cmd/gf/internal/cmd/cmd_init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err
9898
if in.Name != "." {
9999
updateCommand = fmt.Sprintf(`cd %s && %s`, in.Name, updateCommand)
100100
}
101-
if err = gproc.ShellRun(updateCommand); err != nil {
101+
if err = gproc.ShellRun(ctx, updateCommand); err != nil {
102102
mlog.Fatal(err)
103103
}
104104
}

cmd/gf/internal/cmd/cmd_run.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
9999
gtimer.SetTimeout(ctx, 1500*gtime.MS, func(ctx context.Context) {
100100
defer dirty.Set(false)
101101
mlog.Printf(`go file changes: %s`, event.String())
102-
app.Run()
102+
app.Run(ctx)
103103
})
104104
})
105105
if err != nil {
106106
mlog.Fatal(err)
107107
}
108-
go app.Run()
108+
go app.Run(ctx)
109109
select {}
110110
}
111111

112-
func (app *cRunApp) Run() {
112+
func (app *cRunApp) Run(ctx context.Context) {
113113
// Rebuild and run the codes.
114114
renamePath := ""
115115
mlog.Printf("build: %s", app.File)
@@ -132,7 +132,7 @@ func (app *cRunApp) Run() {
132132
app.File,
133133
)
134134
mlog.Print(buildCommand)
135-
result, err := gproc.ShellExec(buildCommand)
135+
result, err := gproc.ShellExec(ctx, buildCommand)
136136
if err != nil {
137137
mlog.Printf("build error: \n%s%s", result, err.Error())
138138
return
@@ -154,7 +154,7 @@ func (app *cRunApp) Run() {
154154
} else {
155155
process = gproc.NewProcessCmd(outputPath, gstr.SplitAndTrim(" ", app.Args))
156156
}
157-
if pid, err := process.Start(); err != nil {
157+
if pid, err := process.Start(ctx); err != nil {
158158
mlog.Printf("build running error: %s", err.Error())
159159
} else {
160160
mlog.Printf("build running pid: %d", pid)

cmd/gf/internal/utility/utils/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/gogf/gf/cmd/gf/v2/internal/consts"
@@ -31,7 +32,7 @@ func GoFmt(path string) {
3132
mlog.Fatal(`command "gofmt" not found`)
3233
}
3334
var command = fmt.Sprintf(`%s -w %s`, gofmtPath, path)
34-
result, err := gproc.ShellExec(command)
35+
result, err := gproc.ShellExec(context.Background(), command)
3536
if err != nil {
3637
mlog.Fatalf(`error executing command "%s": %s`, command, result)
3738
}
@@ -43,7 +44,7 @@ func GoImports(path string) {
4344
mlog.Fatal(`command "goimports" not found`)
4445
}
4546
var command = fmt.Sprintf(`%s -w %s`, goimportsPath, path)
46-
result, err := gproc.ShellExec(command)
47+
result, err := gproc.ShellExec(context.Background(), command)
4748
if err != nil {
4849
mlog.Fatalf(`error executing command "%s": %s`, command, result)
4950
}

example/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ require (
77
github.com/gogf/gf/contrib/registry/etcd/v2 v2.1.0-rc3.0.20220523034830-510fa3faf03f
88
github.com/gogf/gf/contrib/registry/polaris/v2 v2.0.0-rc2
99
github.com/gogf/gf/contrib/trace/jaeger/v2 v2.0.0-rc2
10-
github.com/gogf/gf/v2 v2.1.0-rc3.0.20220523034830-510fa3faf03f
11-
github.com/gogf/katyusha v0.4.0
10+
github.com/gogf/gf/v2 v2.1.0-rc4.0.20220620123459-52056644d499
11+
github.com/gogf/katyusha v0.4.1-0.20220620125113-f55d6f739773
1212
github.com/gogo/protobuf v1.3.2
1313
github.com/golang/protobuf v1.5.2
1414
github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f

example/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg78
113113
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
114114
github.com/gogf/katyusha v0.4.0 h1:mQVfXHhzC+UQf11Q8HAk9IOhQZ1VMXqGUqezyywZUOs=
115115
github.com/gogf/katyusha v0.4.0/go.mod h1:nqsIWBsImnq9+OLlfB6iNef6ZLRyR2L1Bnk9h2aZvKs=
116+
github.com/gogf/katyusha v0.4.1-0.20220620125113-f55d6f739773 h1:YQBLawktoymYtPGs9idE9JS5Wqd3SjIzUEZOPKCdSw0=
117+
github.com/gogf/katyusha v0.4.1-0.20220620125113-f55d6f739773/go.mod h1:Z0GCeHXz1UI0HtA0K45c6TzEGM4DL/PLatS747/WarI=
116118
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
117119
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
118120
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=

example/trace/processes/gcmd/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/gogf/gf/v2/frame/g"
7+
"github.com/gogf/gf/v2/os/gcmd"
8+
"github.com/gogf/gf/v2/os/gctx"
9+
"github.com/gogf/gf/v2/os/gproc"
10+
)
11+
12+
var (
13+
Main = &gcmd.Command{
14+
Name: "main",
15+
Brief: "main process",
16+
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
17+
g.Log().Debug(ctx, `this is main process`)
18+
return gproc.ShellRun(ctx, `go run sub/sub.go`)
19+
},
20+
}
21+
)
22+
23+
func main() {
24+
Main.Run(gctx.New())
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/gogf/gf/v2/frame/g"
7+
"github.com/gogf/gf/v2/os/gcmd"
8+
"github.com/gogf/gf/v2/os/gctx"
9+
)
10+
11+
var (
12+
Sub = &gcmd.Command{
13+
Name: "sub",
14+
Brief: "sub process",
15+
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
16+
g.Log().Debug(ctx, `this is sub process`)
17+
return nil
18+
},
19+
}
20+
)
21+
22+
func main() {
23+
Sub.Run(gctx.New())
24+
}

example/trace/processes/gproc/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/gogf/gf/v2/frame/g"
5+
"github.com/gogf/gf/v2/os/gctx"
6+
"github.com/gogf/gf/v2/os/gproc"
7+
)
8+
9+
func main() {
10+
ctx := gctx.New()
11+
g.Log().Debug(ctx, `this is main process`)
12+
if err := gproc.ShellRun(ctx, `go run sub/sub.go`); err != nil {
13+
panic(err)
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"github.com/gogf/gf/v2/frame/g"
5+
"github.com/gogf/gf/v2/os/gctx"
6+
)
7+
8+
func main() {
9+
ctx := gctx.New()
10+
g.Log().Debug(ctx, `this is sub process`)
11+
}

net/ghttp/ghttp_server_admin_process.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func forkReloadProcess(ctx context.Context, newExeFilePath ...string) error {
144144
}
145145
buffer, _ := gjson.Encode(sfm)
146146
p.Env = append(p.Env, adminActionReloadEnvKey+"="+string(buffer))
147-
if _, err := p.Start(); err != nil {
147+
if _, err := p.Start(ctx); err != nil {
148148
glog.Errorf(
149149
ctx,
150150
"%d: fork process failed, error:%s, %s",
@@ -169,7 +169,7 @@ func forkRestartProcess(ctx context.Context, newExeFilePath ...string) error {
169169
env := os.Environ()
170170
env = append(env, adminActionRestartEnvKey+"=1")
171171
p := gproc.NewProcess(path, os.Args, env)
172-
if _, err := p.Start(); err != nil {
172+
if _, err := p.Start(ctx); err != nil {
173173
glog.Errorf(
174174
ctx,
175175
`%d: fork process failed, error:%s, are you running using "go run"?`,

os/genv/genv.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package genv
99

1010
import (
11+
"fmt"
1112
"os"
1213
"strings"
1314

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

2627
// Map returns a copy of strings representing the environment as a map.
2728
func Map() map[string]string {
28-
m := make(map[string]string)
29-
i := 0
30-
for _, s := range os.Environ() {
31-
i = strings.IndexByte(s, '=')
32-
m[s[0:i]] = s[i+1:]
33-
}
34-
return m
29+
return MapFromEnv(os.Environ())
3530
}
3631

3732
// Get creates and returns a Var with the value of the environment variable
@@ -117,3 +112,28 @@ func Build(m map[string]string) []string {
117112
}
118113
return array
119114
}
115+
116+
// MapFromEnv converts environment variables from slice to map.
117+
func MapFromEnv(envs []string) map[string]string {
118+
m := make(map[string]string)
119+
i := 0
120+
for _, s := range envs {
121+
i = strings.IndexByte(s, '=')
122+
m[s[0:i]] = s[i+1:]
123+
}
124+
return m
125+
}
126+
127+
// MapToEnv converts environment variables from map to slice.
128+
func MapToEnv(m map[string]string) []string {
129+
envs := make([]string, 0)
130+
for k, v := range m {
131+
envs = append(envs, fmt.Sprintf(`%s=%s`, k, v))
132+
}
133+
return envs
134+
}
135+
136+
// Filter filters repeated items from given environment variables.
137+
func Filter(envs []string) []string {
138+
return MapToEnv(MapFromEnv(envs))
139+
}

0 commit comments

Comments
 (0)