Skip to content

Commit

Permalink
Merge pull request #16 from meltwater/prometheusMetrics
Browse files Browse the repository at this point in the history
Prometheus metrics
  • Loading branch information
apoorva-marisomaradhya authored Jan 30, 2020
2 parents 78e7c5f + e7800e7 commit ec9808e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
35 changes: 2 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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))

}()
}
20 changes: 19 additions & 1 deletion plugin/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -31,11 +33,27 @@ 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)
}

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)
}

0 comments on commit ec9808e

Please sign in to comment.