NewGithub returns a new instrumented oauth2 config for github. It tracks rate limits as well as just the external request counts. nolint:bodyclose
(name string, under OAuth2Config)
| 160 | // |
| 161 | //nolint:bodyclose |
| 162 | func (f *Factory) NewGithub(name string, under OAuth2Config) *Config { |
| 163 | cfg := f.New(name, under) |
| 164 | cfg.interceptors = append(cfg.interceptors, func(resp *http.Response, err error) { |
| 165 | limits, ok := githubRateLimits(resp, err) |
| 166 | if !ok { |
| 167 | return |
| 168 | } |
| 169 | labels := prometheus.Labels{ |
| 170 | "name": cfg.name, |
| 171 | "resource": limits.Resource, |
| 172 | } |
| 173 | // Default to -1 for "do not know" |
| 174 | resetIn := float64(-1) |
| 175 | if !limits.Reset.IsZero() { |
| 176 | now := time.Now() |
| 177 | if f.Now != nil { |
| 178 | now = f.Now() |
| 179 | } |
| 180 | resetIn = limits.Reset.Sub(now).Seconds() |
| 181 | if resetIn < 0 { |
| 182 | // If it just reset, just make it 0. |
| 183 | resetIn = 0 |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | f.metrics.rateLimit.With(labels).Set(float64(limits.Limit)) |
| 188 | f.metrics.rateLimitRemaining.With(labels).Set(float64(limits.Remaining)) |
| 189 | f.metrics.rateLimitUsed.With(labels).Set(float64(limits.Used)) |
| 190 | f.metrics.rateLimitReset.With(labels).Set(float64(limits.Reset.Unix())) |
| 191 | f.metrics.rateLimitResetIn.With(labels).Set(resetIn) |
| 192 | }) |
| 193 | return cfg |
| 194 | } |
| 195 | |
| 196 | type Config struct { |
| 197 | // Name is a human friendly name to identify the oauth2 provider. This should be |