Skip to content

Commit

Permalink
fix: pushgatway url (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
sugarnaoming authored Mar 11, 2021
1 parent fa97b72 commit d99488f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.14
require (
github.com/creack/pty v1.1.11
github.com/hashicorp/go-retryablehttp v0.6.6
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.9 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/peterbourgon/mergemap v0.0.0-20130613134717-e21c03b7a721
github.com/stretchr/testify v1.2.2
github.com/urfave/cli v1.22.2
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
gopkg.in/fatih/color.v1 v1.7.0
gopkg.in/myesui/uuid.v1 v1.0.0
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxC
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/myesui/uuid v1.0.0 h1:xCBmH4l5KuvLYc5L7AS7SZg9/jKdIFubM7OVoLqaQUI=
github.com/myesui/uuid v1.0.0/go.mod h1:2CDfNgU0LR8mIdO8vdWd8i9gWWxLlcoIGGpSNgafq84=
github.com/peterbourgon/mergemap v0.0.0-20130613134717-e21c03b7a721 h1:ArxMo6jAOO2KuRsepZ0hTaH4hZCi2CCW4P9PV59HHH0=
Expand All @@ -30,9 +29,10 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fatih/color.v1 v1.7.0 h1:bYGjb+HezBM6j/QmgBfgm1adxHpzzrss6bj4r9ROppk=
gopkg.in/fatih/color.v1 v1.7.0/go.mod h1:P7yosIhqIl/sX8J8UypY5M+dDpD2KmyfP5IRs5v/fo0=
Expand Down
13 changes: 11 additions & 2 deletions launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -82,7 +83,15 @@ func pushMetrics(status string, buildID int) error {
if strings.TrimSpace(os.Getenv("SD_PUSHGATEWAY_URL")) != "" && strings.TrimSpace(os.Getenv("CONTAINER_IMAGE")) != "" && strings.TrimSpace(os.Getenv("SD_PIPELINE_ID")) != "" && buildID > 0 {
timeout := time.Duration(pushgatewayUrlTimeout) * time.Second
client.HTTPClient.Timeout = timeout
url := "http://" + os.Getenv("SD_PUSHGATEWAY_URL") + "/metrics/job/containerd/instance/" + strconv.Itoa(buildID)
url, err := url.Parse(os.Getenv("SD_PUSHGATEWAY_URL"))
if err != nil {
log.Printf("pushMetrics: failed to parse url [%v], buildId:[%v], error:[%v]", os.Getenv("SD_PUSHGATEWAY_URL"), buildID, err)
return nil
}
if url.Scheme == "" {
url.Scheme = "http"
}
url.Path = url.Path + "/metrics/job/containerd/instance/" + strconv.Itoa(buildID)
defer client.HTTPClient.CloseIdleConnections()
image := os.Getenv("CONTAINER_IMAGE")
pipelineId := os.Getenv("SD_PIPELINE_ID")
Expand Down Expand Up @@ -119,7 +128,7 @@ sd_build_setup_time_secs{image_name="` + image + `",pipeline_id="` + pipelineId
`
body := strings.NewReader(data)
log.Printf("pushMetrics: post metrics to [%v]", url)
res, err := client.HTTPClient.Post(url, "", body)
res, err := client.HTTPClient.Post(url.String(), "", body)
if res != nil {
defer res.Body.Close()
}
Expand Down
8 changes: 8 additions & 0 deletions launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,14 @@ func TestPushMetrics(t *testing.T) {
t.Errorf("Push metrics expect to return [nil] but got [%v]", err)
}

// SD_PUSHGATEWAY_URL no protocol
os.Setenv("SD_PUSHGATEWAY_URL", "fake.pushgateway.url&200&0")
os.Setenv("SD_LAUNCHER_END_TS", strconv.FormatInt(ts, 10))
err = pushMetrics("success", 1)
if err != nil {
t.Errorf("Push metrics expect to return [nil] but got [%v]", err)
}

// build id 0
os.Setenv("SD_PUSHGATEWAY_URL", "http://fake.pushgateway.url&200&0")
err = pushMetrics("success", 0)
Expand Down

0 comments on commit d99488f

Please sign in to comment.