Skip to content
Permalink
Browse files

measure GitHub API rate limit

This will provide visibility on how much GitHub API v3 and v4
rate limit is remaining at various points in time, and how it
changes when services are added or modified.
  • Loading branch information...
dmitshur committed Sep 3, 2019
1 parent 3b3cbfc commit 05a34a92fb7658116c5bb9d995cefb8d7dbb09b3
Showing with 41 additions and 0 deletions.
  1. +2 −0 main.go
  2. +10 −0 metrics.go
  3. +29 −0 users.go
@@ -228,6 +228,8 @@ func run(ctx context.Context, cancel context.CancelFunc, storeDir, stateFile, an

if *metricsHTTPFlag != "" {
initMetrics(cancel, *metricsHTTPFlag)
go measureGitHubV3RateLimit()
go measureGitHubV4RateLimit()
}

staticFiles := cookieAuth{httpgzip.FileServer(
@@ -11,22 +11,32 @@ import (

type prometheusMetrics struct {
goGetRequestsTotal *prometheus.CounterVec
githubRateLimit *prometheus.GaugeVec
}

var metrics = &prometheusMetrics{
goGetRequestsTotal: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "home_go_get_requests_total",
Help: "Total number of ?go-get=1 requests.",
}, []string{"path"}),
githubRateLimit: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "home_github_rate_limit",
Help: "Remaining requests the GitHub client can make this hour.",
}, []string{"client"}),
}

func (m *prometheusMetrics) IncGoGetRequestsTotal(importPath string) {
m.goGetRequestsTotal.With(prometheus.Labels{"path": importPath}).Inc()
}

func (m *prometheusMetrics) SetGitHubRateLimit(clientName string, remaining int) {
m.githubRateLimit.With(prometheus.Labels{"client": clientName}).Set(float64(remaining))
}

func initMetrics(cancel context.CancelFunc, httpAddr string) {
r := prometheus.NewRegistry()
r.MustRegister(metrics.goGetRequestsTotal)
r.MustRegister(metrics.githubRateLimit)

mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
@@ -36,6 +36,35 @@ var dmitshurPublicRepoGHV3, dmitshurPublicRepoGHV4 = func() (*githubv3.Client, *
githubv4.NewClient(&http.Client{Transport: authTransport, Timeout: 10 * time.Second})
}()

// measureGitHubV3RateLimit measures and reports GitHub API v3 rate limit.
func measureGitHubV3RateLimit() {
for {
rate, _, err := dmitshurPublicRepoGHV3.RateLimits(context.Background())
if err != nil {
log.Println("dmitshurPublicRepoGHV3.RateLimits:", err)
time.Sleep(time.Minute)
continue
}
metrics.SetGitHubRateLimit("dmitshur-v3", rate.Core.Remaining)
time.Sleep(time.Minute)
}
}

// measureGitHubV4RateLimit measures and reports GitHub API v4 rate limit.
func measureGitHubV4RateLimit() {
for {
var q struct{ RateLimit struct{ Remaining int } }
err := dmitshurPublicRepoGHV4.Query(context.Background(), &q, nil)
if err != nil {
log.Println("dmitshurPublicRepoGHV4.Query for RateLimit:", err)
time.Sleep(time.Minute)
continue
}
metrics.SetGitHubRateLimit("dmitshur-v4", q.RateLimit.Remaining)
time.Sleep(time.Minute)
}
}

type userCreator interface {
// Create creates the specified user.
// It returns os.ErrExist if the user already exists.

0 comments on commit 05a34a9

Please sign in to comment.
You can’t perform that action at this time.