Skip to content

Commit

Permalink
measure GitHub API rate limit
Browse files Browse the repository at this point in the history
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 9, 2019
1 parent 3b3cbfc commit 05a34a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions metrics.go
Expand Up @@ -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{}))
Expand Down
29 changes: 29 additions & 0 deletions users.go
Expand Up @@ -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.
Expand Down

0 comments on commit 05a34a9

Please sign in to comment.