diff --git a/cmd/build.go b/cmd/build.go index 26b14bb0..0e91b66e 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -39,6 +39,9 @@ goc build . # Build the current binary with cover variables injected, and set the registry center to http://127.0.0.1:7777. goc build --center=http://127.0.0.1:7777 +# Build the current binary with cover variables injected, and set the registry name. +goc build --service=srv1 + # Build the current binary with cover variables injected, and redirect output to /to/this/path. goc build --output /to/this/path @@ -77,6 +80,7 @@ func runBuild(args []string, wd string) { Target: gocBuild.TmpDir, Mode: coverMode.String(), AgentPort: agentPort.String(), + Service: service, Center: center, Singleton: singleton, IsMod: gocBuild.IsMod, diff --git a/cmd/commonflags.go b/cmd/commonflags.go index 7f3a6d18..aa02229c 100644 --- a/cmd/commonflags.go +++ b/cmd/commonflags.go @@ -27,6 +27,7 @@ import ( var ( target string center string + service string agentPort AgentPort debugGoc bool debugInCISyncFile string @@ -60,6 +61,7 @@ func addCommonFlags(cmdset *pflag.FlagSet) { func addBuildFlags(cmdset *pflag.FlagSet) { addCommonFlags(cmdset) + cmdset.StringVar(&service, "service", "", "register service name. if not provided, using the binary file name") // bind to viper viper.BindPFlags(cmdset) } diff --git a/cmd/install.go b/cmd/install.go index edc3c575..2a63f930 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -70,6 +70,7 @@ func runInstall(args []string, wd string) { Target: gocBuild.TmpDir, Mode: coverMode.String(), AgentPort: agentPort.String(), + Service: service, Center: center, Singleton: singleton, IsMod: gocBuild.IsMod, diff --git a/cmd/run.go b/cmd/run.go index 6fde9cee..f871d6d9 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -72,6 +72,7 @@ goc run . [--buildflags] [--exec] [--arguments] Center: gocServer, Singleton: singleton, AgentPort: "", + Service: service, IsMod: gocBuild.IsMod, ModRootPath: gocBuild.ModRootPath, OneMainPackage: true, // go run is similar with go build, build only one main package diff --git a/pkg/cover/cover.go b/pkg/cover/cover.go index 2639ce2b..b87c3f91 100644 --- a/pkg/cover/cover.go +++ b/pkg/cover/cover.go @@ -52,6 +52,7 @@ type TestCover struct { Mode string AgentPort string Center string // cover profile host center + Service string Singleton bool MainPkgCover *PackageCover DepsCover []*PackageCover @@ -139,6 +140,7 @@ type CoverInfo struct { Args string Mode string AgentPort string + Service string Center string Singleton bool } @@ -190,6 +192,7 @@ func Execute(coverInfo *CoverInfo) error { Mode: mode, AgentPort: agentPort, Center: center, + Service: coverInfo.Service, Singleton: singleton, MainPkgCover: mainCover, GlobalCoverVarImportPath: globalCoverVarImportPath, diff --git a/pkg/cover/instrument.go b/pkg/cover/instrument.go index a559fea2..4ffb4771 100644 --- a/pkg/cover/instrument.go +++ b/pkg/cover/instrument.go @@ -223,6 +223,10 @@ func registerHandlers() { func registerSelf(address string) ([]byte, error) { selfName := filepath.Base(os.Args[0]) + serviceName := {{.Service | printf "%q"}} + if serviceName != "" { + selfName = serviceName + } req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/cover/register?name=%s&address=%s", {{.Center | printf "%q"}}, selfName, address), nil) if err != nil { log.Fatalf("http.NewRequest failed: %v", err)