From c8f42bf6551eba25f2c4e9ace7bb425f85fc6155 Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Tue, 21 Jan 2020 08:51:36 -0800 Subject: [PATCH 1/7] adding prometheus metrics --- main.go | 6 +++++- metric/metric.go | 20 ++++++++++++++++++++ plugin/get.go | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 metric/metric.go diff --git a/main.go b/main.go index 2d30cc3..0397059 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/drone/drone-go/plugin/converter" "github.com/meltwater/drone-convert-pathschanged/plugin" + "github.com/meltwater/drone-convert-pathschanged/metric" _ "github.com/joho/godotenv/autoload" "github.com/kelseyhightower/envconfig" @@ -64,9 +65,12 @@ func main() { http.Handle("/", handler) http.HandleFunc("/healthz", healthz) logrus.Fatal(http.ListenAndServe(spec.Bind, nil)) + + githubapiHandler := metric.GithubApiCalls() + http.Handle("/githubapi", githubapiHandler) } func healthz(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Type", "text/plain") io.WriteString(w, "OK") } diff --git a/metric/metric.go b/metric/metric.go new file mode 100644 index 0000000..fc8469c --- /dev/null +++ b/metric/metric.go @@ -0,0 +1,20 @@ +package metric + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/meltwater/drone-convert-pathschanged/plugin" +) + +func GithubApiCalls(get plugin.apiRateLimit) { + prometheus.MustRegister( + prometheus.NewGaugeFunc(prometheus.GaugeOpts{ + Name: "github_api_calls" + Help: "Total number of github api calls per hour." + }, func() float64{ + i, _ := get.apiRateLimit(noContext) + return float64(i) + }), + + ) + +} \ No newline at end of file diff --git a/plugin/get.go b/plugin/get.go index 1eeeb92..4bbbeae 100644 --- a/plugin/get.go +++ b/plugin/get.go @@ -40,3 +40,21 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string return files, nil } + +func apiRateLimit () + newctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(newctx, ts) + + client := github.NewClient(tc) + + rateLimit, _, err := client.RateLimits(newctx) + if err != nil { + fmt.Printf("Promblem getting github rate limit info %v\n", err) + return + } + GithubApiCount = (rateLimit.Core.Limit - rateLimit.Core.Remaining) + return GithubApiCount, nil +} From de33429d72c2dd3fea879fa336c893ce2b930002 Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Tue, 21 Jan 2020 09:48:19 -0800 Subject: [PATCH 2/7] prometheus metrics --- plugin/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/get.go b/plugin/get.go index 4bbbeae..4321e5b 100644 --- a/plugin/get.go +++ b/plugin/get.go @@ -41,7 +41,7 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string return files, nil } -func apiRateLimit () +func apiRateLimit () { newctx := context.Background() ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, From d4b94f0e86a4f5b29d4fd1666f4963c28d88b954 Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Tue, 21 Jan 2020 09:54:16 -0800 Subject: [PATCH 3/7] pass token --- plugin/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/get.go b/plugin/get.go index 4321e5b..58d97a3 100644 --- a/plugin/get.go +++ b/plugin/get.go @@ -41,7 +41,7 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string return files, nil } -func apiRateLimit () { +func apiRateLimit (token string) ([]string, error) { newctx := context.Background() ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, From 52c44e13042a6c69941cef8da910a704c4c001f4 Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Tue, 21 Jan 2020 10:04:58 -0800 Subject: [PATCH 4/7] more fix --- plugin/get.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugin/get.go b/plugin/get.go index 58d97a3..6ef2bf0 100644 --- a/plugin/get.go +++ b/plugin/get.go @@ -52,9 +52,8 @@ func apiRateLimit (token string) ([]string, error) { rateLimit, _, err := client.RateLimits(newctx) if err != nil { - fmt.Printf("Promblem getting github rate limit info %v\n", err) - return + return nil, err } - GithubApiCount = (rateLimit.Core.Limit - rateLimit.Core.Remaining) + GithubApiCount = rateLimit.Core.Remaining return GithubApiCount, nil } From e2a786475c9a4220c4f9c6ff6a99984064e2155e Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Fri, 24 Jan 2020 12:04:41 -0800 Subject: [PATCH 5/7] prometheus metrics --- go.mod | 3 ++ go.sum | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 ++-- metric/metric.go | 23 +++++--------- plugin/get.go | 11 +++---- 5 files changed, 97 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 3d1119a..9ce4fb4 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,9 @@ require ( github.com/google/go-querystring v1.0.0 // indirect github.com/joho/godotenv v1.3.0 github.com/kelseyhightower/envconfig v1.4.0 + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/prometheus/client_golang v1.3.0 github.com/sirupsen/logrus v1.4.2 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 ) diff --git a/go.sum b/go.sum index 488d9ed..f0dfe56 100644 --- a/go.sum +++ b/go.sum @@ -1,28 +1,106 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/99designs/httpsignatures-go v0.0.0-20170731043157-88528bf4ca7e h1:rl2Aq4ZODqTDkeSqQBy+fzpZPamacO1Srp8zq7jf2Sc= github.com/99designs/httpsignatures-go v0.0.0-20170731043157-88528bf4ca7e/go.mod h1:Xa6lInWHNQnuWoF0YPSsx+INFA9qk7/7pTjwb3PInkY= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bmatcuk/doublestar v1.1.5 h1:2bNwBOmhyFEFcoB3tGvTD5xanq+4kyOZlB8wFYbMjkk= github.com/bmatcuk/doublestar v1.1.5/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE= +github.com/buildkite/yaml v2.1.0+incompatible h1:xirI+ql5GzfikVNDmt+yeiXpf/v1Gt03qXTtT5WXdr8= github.com/buildkite/yaml v2.1.0+incompatible/go.mod h1:UoU8vbcwu1+vjZq01+KrpSeLBgQQIjL/H7Y6KwikUrI= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/drone/drone-go v1.1.0 h1:2mritc5b7PhQWvILNyzaImZMRWVbMmmZ5Q0UDwwO7SI= github.com/drone/drone-go v1.1.0/go.mod h1:GxyeGClYohaKNYJv/ZpsmVHtMJ7WhoT+uDaJNcDIrk4= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= +github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.3.0 h1:miYCvYqFXtl/J9FIy8eNpBfYthAEFg+Ys0XyUVEcDsc= +github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.1.0 h1:ElTg5tNp4DqfV7UQjDqv2+RJlNzsDtvNAWccbItceIE= +github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.7.0 h1:L+1lyG48J1zAQXA3RBX/nG/B3gjlHq0zTt2tlbJLyCY= +github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8= +github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191220142924-d4481acd189f h1:68K/z8GLUxV76xGSqwTWw2gyk/jwn79LUL43rES2g8o= +golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 0397059..dbc2433 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "github.com/drone/drone-go/plugin/converter" "github.com/meltwater/drone-convert-pathschanged/plugin" - "github.com/meltwater/drone-convert-pathschanged/metric" + "github.com/prometheus/client_golang/prometheus/promhttp" _ "github.com/joho/godotenv/autoload" "github.com/kelseyhightower/envconfig" @@ -65,9 +65,7 @@ func main() { http.Handle("/", handler) http.HandleFunc("/healthz", healthz) logrus.Fatal(http.ListenAndServe(spec.Bind, nil)) - - githubapiHandler := metric.GithubApiCalls() - http.Handle("/githubapi", githubapiHandler) + http.Handle("/metrics", promhttp.Handler()) } func healthz(w http.ResponseWriter, r *http.Request) { diff --git a/metric/metric.go b/metric/metric.go index fc8469c..b34b5b9 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -1,20 +1,13 @@ package metric import ( - "github.com/prometheus/client_golang/prometheus" - "github.com/meltwater/drone-convert-pathschanged/plugin" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) -func GithubApiCalls(get plugin.apiRateLimit) { - prometheus.MustRegister( - prometheus.NewGaugeFunc(prometheus.GaugeOpts{ - Name: "github_api_calls" - Help: "Total number of github api calls per hour." - }, func() float64{ - i, _ := get.apiRateLimit(noContext) - return float64(i) - }), - - ) - -} \ No newline at end of file +var ( + GithubApiCount = promauto.NewCounter(prometheus.CounterOpts{ + Name: "github_api_calls", + Help: "Total number of github api calls per hour", + }) +) \ No newline at end of file diff --git a/plugin/get.go b/plugin/get.go index 6ef2bf0..907e955 100644 --- a/plugin/get.go +++ b/plugin/get.go @@ -4,7 +4,7 @@ import ( "context" "github.com/drone/drone-go/drone" - + "github.com/meltwater/drone-convert-pathschanged/metric" "github.com/google/go-github/github" "golang.org/x/oauth2" ) @@ -41,7 +41,7 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string return files, nil } -func apiRateLimit (token string) ([]string, error) { +func apiRateLimit(token string) (int, error) { newctx := context.Background() ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: token}, @@ -52,8 +52,9 @@ func apiRateLimit (token string) ([]string, error) { rateLimit, _, err := client.RateLimits(newctx) if err != nil { - return nil, err + return 1, err } - GithubApiCount = rateLimit.Core.Remaining - return GithubApiCount, nil + gitApiCalls := rateLimit.Core.Remaining + metric.GithubApiCount.Add(float64(gitApiCalls)) + return gitApiCalls, nil } From 7373018cf7543f254aff313b1c1e855a1fab2d83 Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Mon, 27 Jan 2020 08:14:40 -0800 Subject: [PATCH 6/7] test prometheus metrics --- main.go | 40 +++++++++++++++++++++++++++++++++++----- metric/metric.go | 13 ------------- plugin/get.go | 19 ------------------- plugin/plugin.go | 3 ++- 4 files changed, 37 insertions(+), 38 deletions(-) delete mode 100644 metric/metric.go diff --git a/main.go b/main.go index dbc2433..c9a61b1 100644 --- a/main.go +++ b/main.go @@ -6,12 +6,14 @@ package main import ( "io" + "context" "net/http" - + "github.com/google/go-github/github" + "golang.org/x/oauth2" "github.com/drone/drone-go/plugin/converter" + "github.com/prometheus/client_golang/prometheus" "github.com/meltwater/drone-convert-pathschanged/plugin" "github.com/prometheus/client_golang/prometheus/promhttp" - _ "github.com/joho/godotenv/autoload" "github.com/kelseyhightower/envconfig" "github.com/sirupsen/logrus" @@ -27,6 +29,13 @@ type spec struct { Token string `envconfig:"GITHUB_TOKEN"` } +var ( + githubApiCount = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "github_api_calls", + Help: "Total number of github api calls per hour", + },) +) func main() { spec := new(spec) err := envconfig.Process("", spec) @@ -59,16 +68,37 @@ func main() { spec.Secret, logrus.StandardLogger(), ) - + logrus.Infof("server listening on address %s", spec.Bind) - + ApiRateLimit(spec.Token) + http.Handle("/", handler) http.HandleFunc("/healthz", healthz) - logrus.Fatal(http.ListenAndServe(spec.Bind, nil)) http.Handle("/metrics", promhttp.Handler()) + prometheus.MustRegister(githubApiCount) + logrus.Fatal(http.ListenAndServe(spec.Bind, nil)) } func healthz(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") io.WriteString(w, "OK") } + +func ApiRateLimit(token string) { + go func() { + newctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(newctx, ts) + + client := github.NewClient(tc) + + rateLimit,_,err:= client.RateLimits(newctx) + if err != nil { + logrus.Fatalln("No metrics") + } + githubApiCount.Set(float64(rateLimit.Core.Limit - rateLimit.Core.Remaining)) + + }() +} \ No newline at end of file diff --git a/metric/metric.go b/metric/metric.go deleted file mode 100644 index b34b5b9..0000000 --- a/metric/metric.go +++ /dev/null @@ -1,13 +0,0 @@ -package metric - -import ( - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" -) - -var ( - GithubApiCount = promauto.NewCounter(prometheus.CounterOpts{ - Name: "github_api_calls", - Help: "Total number of github api calls per hour", - }) -) \ No newline at end of file diff --git a/plugin/get.go b/plugin/get.go index 907e955..4184ec3 100644 --- a/plugin/get.go +++ b/plugin/get.go @@ -4,7 +4,6 @@ import ( "context" "github.com/drone/drone-go/drone" - "github.com/meltwater/drone-convert-pathschanged/metric" "github.com/google/go-github/github" "golang.org/x/oauth2" ) @@ -40,21 +39,3 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string return files, nil } - -func apiRateLimit(token string) (int, error) { - newctx := context.Background() - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - ) - tc := oauth2.NewClient(newctx, ts) - - client := github.NewClient(tc) - - rateLimit, _, err := client.RateLimits(newctx) - if err != nil { - return 1, err - } - gitApiCalls := rateLimit.Core.Remaining - metric.GithubApiCount.Add(float64(gitApiCalls)) - return gitApiCalls, nil -} diff --git a/plugin/plugin.go b/plugin/plugin.go index 63c5056..ad66425 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -14,6 +14,7 @@ import ( "github.com/buildkite/yaml" "github.com/sirupsen/logrus" + ) type ( @@ -138,4 +139,4 @@ func (p *plugin) Convert(ctx context.Context, req *converter.Request) (*drone.Co Data: config, }, nil -} +} \ No newline at end of file From 7f351bb6b5d507e7fb236c32c0b485bdd055f2a9 Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Mon, 27 Jan 2020 08:28:13 -0800 Subject: [PATCH 7/7] modify metric name --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index c9a61b1..195aae9 100644 --- a/main.go +++ b/main.go @@ -32,8 +32,8 @@ type spec struct { var ( githubApiCount = prometheus.NewGauge( prometheus.GaugeOpts{ - Name: "github_api_calls", - Help: "Total number of github api calls per hour", + Name: "github_api_calls_remaining", + Help: "Total number of github api calls per hour remaining", },) ) func main() { @@ -98,7 +98,7 @@ func ApiRateLimit(token string) { if err != nil { logrus.Fatalln("No metrics") } - githubApiCount.Set(float64(rateLimit.Core.Limit - rateLimit.Core.Remaining)) + githubApiCount.Set(float64(rateLimit.Core.Remaining)) }() } \ No newline at end of file