From e7800e76dea27ed52bf28cc9b79cd9d848bbb25a Mon Sep 17 00:00:00 2001 From: apoorva-marisomaradhya Date: Thu, 30 Jan 2020 08:43:45 -0800 Subject: [PATCH] Prometheus metrics --- main.go | 35 ++--------------------------------- plugin/get.go | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index 195aae9..9749ffd 100644 --- a/main.go +++ b/main.go @@ -6,17 +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" + ) // spec provides the plugin settings. @@ -29,13 +26,6 @@ type spec struct { Token string `envconfig:"GITHUB_TOKEN"` } -var ( - githubApiCount = prometheus.NewGauge( - prometheus.GaugeOpts{ - Name: "github_api_calls_remaining", - Help: "Total number of github api calls per hour remaining", - },) -) func main() { spec := new(spec) err := envconfig.Process("", spec) @@ -70,35 +60,14 @@ func main() { ) logrus.Infof("server listening on address %s", spec.Bind) - ApiRateLimit(spec.Token) http.Handle("/", handler) http.HandleFunc("/healthz", healthz) - http.Handle("/metrics", promhttp.Handler()) - prometheus.MustRegister(githubApiCount) + http.Handle("/metrics", promhttp.Handler()) 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.Remaining)) - - }() } \ No newline at end of file diff --git a/plugin/get.go b/plugin/get.go index 4184ec3..d24c83b 100644 --- a/plugin/get.go +++ b/plugin/get.go @@ -6,6 +6,8 @@ import ( "github.com/drone/drone-go/drone" "github.com/google/go-github/github" "golang.org/x/oauth2" + "github.com/prometheus/client_golang/prometheus" + "github.com/sirupsen/logrus" ) func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string, error) { @@ -31,7 +33,12 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string } commitFiles = response.Files } - + rateLimit, _,err := client.RateLimits(newctx) + if err != nil { + logrus.Fatalln("No metrics") + } + //metrics.GithubApiCount.Set(float64(rateLimit.Core.Remaining)) + GithubApiCount.Set(float64(rateLimit.Core.Remaining)) var files []string for _, f := range commitFiles { files = append(files, *f.Filename) @@ -39,3 +46,14 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string return files, nil } + +var ( + GithubApiCount = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "github_api_calls_remaining", + Help: "Total number of github api calls per hour remaining", +}) +) +func init(){ + prometheus.MustRegister(GithubApiCount) +}