From 1c5de84bf0d0196170e4a561c651127384cb128e Mon Sep 17 00:00:00 2001 From: huangshaojie Date: Thu, 15 Jul 2021 16:05:22 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E5=AE=9Aservice?= =?UTF-8?q?=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/build.go | 4 ++++ cmd/commonflags.go | 2 ++ pkg/cover/cover.go | 3 +++ pkg/cover/instrument.go | 4 ++++ 4 files changed, 13 insertions(+) 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..8aed9d6e 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") // bind to viper viper.BindPFlags(cmdset) } 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) From c54daf7f268530218f19354aae62a451b2a5f92c Mon Sep 17 00:00:00 2001 From: huangshaojie Date: Wed, 25 Aug 2021 10:52:26 +0800 Subject: [PATCH 2/3] change run and install --- cmd/install.go | 1 + cmd/run.go | 1 + 2 files changed, 2 insertions(+) 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 From fb2789ef6c4313dacff0de625d796216c936541c Mon Sep 17 00:00:00 2001 From: Socket <30768657+socket515@users.noreply.github.com> Date: Tue, 21 Sep 2021 09:42:24 +0800 Subject: [PATCH 3/3] Update commonflags.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 提供默认值 --- cmd/commonflags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commonflags.go b/cmd/commonflags.go index 8aed9d6e..aa02229c 100644 --- a/cmd/commonflags.go +++ b/cmd/commonflags.go @@ -61,7 +61,7 @@ func addCommonFlags(cmdset *pflag.FlagSet) { func addBuildFlags(cmdset *pflag.FlagSet) { addCommonFlags(cmdset) - cmdset.StringVar(&service, "service", "", "register service name") + cmdset.StringVar(&service, "service", "", "register service name. if not provided, using the binary file name") // bind to viper viper.BindPFlags(cmdset) }