Git Product home page Git Product logo

gh-rate-limit-exporter's Introduction

gh-rate-limit-exporter

Build & Test CodeQL Build Image Go Report Card

The exporter you longed for to expose GitHub API rate limits as Prometheus metrics.

Why bother? Building your pipelines with GitHub Actions will most likely require you to integrate with GitHub API. The more your business and pipelines grow the more you send requests toward GitHub API. Using GitHub API comes free only until a certain limit and beyond that limit, you will be throttled. This tool aims to give you the visibility on how far you are from the limits with your GitHub Apps and GitHub PATs (autheticated users). Throttling is applied at the authenticated user level.

This tool supports exporting the rate limits for GitHub App credentials and the classic and fine-grained PATs.

To learn more about GitHub API rate limits go here.

How to use

By default, the exporter expects to find credentials (GitHub App or PAT) in credentials.yml in current working directory.

my-github-app-name:
  type: gh-app
  appId: <app id (integer) goes here>
  installationId: <installation id (integer) goes here>
  key: <base64 encoded private key goes here>
my-pat-name:
  type: gh-pat
  token: <PAT goes here>

You can have as many GitHub credentials in credentials.yml as you want. gh-rate-limit-exporter will fetch the rate limit usage for every credential and exposes it on http://localhost:8080/metrics.

But I do not want to store my GitHub credentials in credentials.yml!

In this case you need to create a new Go module and write a bit of code in Go. For the sake of example let's assume that you want to consume the credentials directly from the process memory. For that do the following.

  • Create a new Go module
  • go get github.com/ragnarpa/gh-rate-limit-exporter
  • Create main.go with following content
package main

import (
	"github.com/ragnarpa/gh-rate-limit-exporter/logger"
	"github.com/ragnarpa/gh-rate-limit-exporter/metrics"
	"github.com/ragnarpa/gh-rate-limit-exporter/pkg/exporter"
	"github.com/ragnarpa/gh-rate-limit-exporter/server"
	"go.uber.org/fx"
)

type InMemoryCredentialSource struct{}

func (s *InMemoryCredentialSource) Credentials() []*exporter.Credential {
	return []*exporter.Credential{
		{
			Type:    exporter.GitHubPAT,
			AppName: "app-name",
			PAT:     &exporter.PAT{Token: "dG9rZW4K"},
		},
	}
}

func main() {
	fx.New(
		// Replace the default implementation (credentials.yml) provided by
		// gh-rate-limit-exporter with your own in-memory credential Source.
		fx.Replace(
			fx.Annotate(
				&InMemoryCredentialSource{},
				fx.As(new(exporter.CredentialSource)),
			),
		),
		logger.Module(),
		metrics.Module(),
		exporter.Module(),
		server.Module(),
		fx.NopLogger,
	).Run()
}

After running your Go program (go run main.go) you should see ... 401 Bad credentials ... error messages in the logs.

The 401 error is expected as the example is using a made-up credential.

This of course, is a very simplistic example and it should be used only for toying on your workstation locally.

If you would like to consume credentials from some sort of credential provider, then feel free to write an implementation that would match the exporter.CredentialSource interface and inject it in a similar way as it is done with InMemoryCredentialSource in the example. If the implementation is generic enough and the community would benefit from it, then please consider creating a pull request.

Container image

You can build the container image with the default implementation (credentials.yml) and then run the exporter within a container.

docker build -t gh-rate-limit-exporter .
docker run --rm -v /path/to/credentials.yml:/home/nonroot/credentials.yml -p 8080:8080 gh-rate-limit-exporter 

Navigate to http://localhost:8080/metrics and after 30 seconds you should see GitHub API rate limit usage exposed as Prometheus metrics.

Metrics

  • gh_rate_limit_exporter_rate_limit_remaining - the amount of requests you can perform within the time unit the rate limit is applied on
  • gh_rate_limit_exporter_rate_limit_total - the upper limit of requests within the time unit the rate limit is applied on
  • gh_rate_limit_exporter_rate_limit_usage - (total - remaining) / total

To find out how to scrape Prometheus metrics, please go here.

Development

# With live-reloading (https://github.com/cosmtrek/air).
air -c .air.toml
# Open your favourite IDE and start coding ...
# Trigger the collection.
curl http://localhost:8080/metrics

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.