Prometheus exporter for GitHub
We won’t cover further details how to properly setup Prometheus itself, we will only cover some basic setup based on docker-compose. But if you want to run this exporter without docker-compose you should be able to adopt that to your needs.
First of all we need to prepare a configuration for Prometheus that includes the exporter based on a static configuration with the container name as a hostname:
global:
scrape_interval: 1m
scrape_timeout: 10s
evaluation_interval: 1m
scrape_configs:
- job_name: github
static_configs:
- targets:
- github_exporter:9504
After preparing the configuration we need to create the docker-compose.yml
within the same folder, this docker-compose.yml
starts a simple
Prometheus instance together with the exporter. Don’t forget to
update the environment variables with the required credentials.
version: '2'
volumes:
prometheus:
services:
prometheus:
image: prom/prometheus:latest
restart: always
ports:
- 9090:9090
volumes:
- prometheus:/prometheus
- ./prometheus.yml:/etc/prometheus/prometheus.yml
github_exporter:
image: promhippie/github-exporter:latest
restart: always
environment:
- GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
Since our latest
tag always refers to the master
branch of the Git
repository you should always use some fixed version. You can see all available
tags at DockerHub or Quay, there you will see that we
also provide a manifest, you can easily start the exporter on various
architectures without any change to the image name. You should apply a change
like this to the docker-compose.yml
file:
github_exporter:
- image: promhippie/github-exporter:latest
+ image: promhippie/github-exporter:1.0.0
restart: always
environment:
- GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
If you want to access the exporter directly you should bind it to a local port,
otherwise only Prometheus will have access to the exporter. For
debugging purpose or just to discover all available metrics directly you can
apply this change to your docker-compose.yml
, after that you can access it
directly at http://localhost:9504/metrics:
github_exporter:
image: promhippie/github-exporter:latest
restart: always
+ ports:
+ - 127.0.0.1:9504:9504
environment:
- GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
It’s also possible to provide the token to access the GitHub API gets provided by a file, in case you are using some kind of secret provider. For this use case you can write the token to a file on any path and reference it with the following format:
github_exporter:
image: promhippie/github-exporter:latest
restart: always
environment:
- - GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
+ - GITHUB_EXPORTER_TOKEN=file://path/to/secret/file/with/token
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
Besides the file://
format we currently also support base64://
which expects
the token in a base64 encoded format. This functionality can be used for the
token and other secret values like the private key for GitHub App authentication
so far.
If you want to collect the metrics of all repositories within an organization you are able to use globbing, but be aware that all repositories matched by globbing won’t provide metrics for the number of subscribers, the number of repositories in the network, if squash merges are allowed, if rebase merges are allowed or merge commits are allowed. These metrics are only present for specific repositories like the example mentioned above.
github_exporter:
image: promhippie/github-exporter:latest
restart: always
environment:
- GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- - GITHUB_EXPORTER_REPO=promhippie/example
+ - GITHUB_EXPORTER_REPO=promhippie/*_exporter,promhippie/prometheus*
If you want to secure the access to the exporter you can provide a web config. You just need to provide a path to the config file in order to enable the support for it, for details about the config format look at the documentation section:
github_exporter:
image: promhippie/github-exporter:latest
restart: always
environment:
+ - GITHUB_EXPORTER_WEB_CONFIG=path/to/web-config.json
- GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
If you want to use the workflows exporter you are forced to expose the exporter on the internet as the exporter got to receive webhooks from GitHub. Otherwise you won’t be able to receive information about the workflows which could be transformed to metrics.
To enable the webhook endpoint you should prepare a random secret which gets used by the endpoint and the GitHub webhook, it can have any format and any length.
Make sure that the exporter is reachable on the /github
endpoint and add the
following environment variables, best would be to use some kind of reverse proxy
in front of the exporter which enforces connections via HTTPS or to properly
configure HTTPS access via web configuration.
github_exporter:
image: promhippie/github-exporter:latest
restart: always
environment:
+ - GITHUB_EXPORTER_COLLECTOR_WORKFLOWS=true
+ - GITHUB_EXPORTER_WEBHOOK_SECRET=your-prepared-random-secret
- GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
After you have enabled the workflow collector and made sure that the endpoint is reachable by GitHub you can look at the webhook section of this documentation to see how to configure the webhook on your GitHub organization or repository.
If you want to use a GitHub application instead of a personal access token please take a look at the application section and add the following environment variables after that:
github_exporter:
image: promhippie/github-exporter:latest
restart: always
environment:
+ - GITHUB_EXPORTER_APP_ID=your-application-id
+ - GITHUB_EXPORTER_INSTALLATION_ID=your-installation-id
+ - GITHUB_EXPORTER_PRIVATE_KEY=file://path/to/secret.pem
- - GITHUB_EXPORTER_TOKEN=bldyecdtysdahs76ygtbw51w3oeo6a4cvjwoitmb
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
If you prefer to provide the private key as a string instead you could also provide is in a base64 encoded format:
github_exporter:
image: promhippie/github-exporter:latest
restart: always
environment:
- GITHUB_EXPORTER_APP_ID=your-application-id
- GITHUB_EXPORTER_INSTALLATION_ID=your-installation-id
+ - GITHUB_EXPORTER_PRIVATE_KEY=base64://Q0VSVElGSUNBVEU=
- - GITHUB_EXPORTER_PRIVATE_KEY=file://path/to/secret.pem
- GITHUB_EXPORTER_LOG_PRETTY=true
- GITHUB_EXPORTER_ORG=promhippie
- GITHUB_EXPORTER_REPO=promhippie/example
Finally the exporter should be configured fine, let’s start this stack with
docker-compose, you just need to execute docker-compose up
within
the directory where you have stored the prometheus.yml
and
docker-compose.yml
.
That’s all, the exporter should be up and running. Have fun with it and hopefully you will gather interesting metrics and never run into issues. You can access the exporter at http://localhost:9504/metrics and Prometheus at http://localhost:9090.
info
false
0.0.0.0:9504
/metrics
false
10s
/github
5s
0
0
false
500
false
true
true
false
false
24h0m0s
owner, repo, workflow, event, name, status, branch, number, run
false
24h0m0s
owner, repo, name, title, branch, sha, identifier, run_id, run_attempt, labels, runner_id, runner_name, runner_group_id, runner_group_name, workflow_name
false
owner, id, name, os, status
If you want to secure the service by TLS or by some basic authentication you can
provide a YAML
configuration file which follows the Prometheus
toolkit format. You can see a full configuration example within the
toolkit documentation.
You can a rough list of available metrics below, additionally to these metrics you will always get the standard metrics exported by the Golang client of Prometheus. If you want to know more about these standard metrics take a look at the process collector and the Go collector.
For some collectors we have defined a dynamic option for the labeling of the metrics. That way it’s up to you to write high-cardinality labels to Prometheus or not. You can see some lists below for collectors that can be customized.
Currently we are covering the most famous installation methods on Kubernetes, you can choose between Kustomize and Helm.
We won’t cover the installation of Kustomize within this guide, to
get it installed and working please read the upstream documentation. After the
installation of Kustomize you just need to prepare a
kustomization.yml
wherever you like similar to this:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: github-exporter
resources:
- github.com/promhippie/github_exporter//deploy/kubernetes?ref=master
configMapGenerator:
- name: github-exporter
behavior: merge
literals: []
secretGenerator:
- name: github-exporter
behavior: merge
literals: []
After that you can simply execute kustomize build | kubectl apply -f -
to get
the manifest applied. Generally it’s best to use fixed versions of the container
images, this can be done quite easy, you just need to append this block to your
kustomization.yml
to use this specific version:
images:
- name: quay.io/promhippie/github-exporter
newTag: 1.1.0
After applying this manifest the exporter should be directly visible within your Prometheus instance if you are using the Prometheus Operator as these manifests are providing a ServiceMonitor.
We won’t cover the installation of Helm within this guide, to get it installed and working please read the upstream documentation. After the installation of Helm you just need to execute the following commands:
helm repo add promhippie https://promhippie.github.io/charts
helm show values promhippie/github-exporter
helm install github-exporter promhippie/github-exporter
You can also watch that available values and generally the details of the chart provided by us within our chart repository or on Artifacthub.
After applying this manifest the exporter should be directly visible within your Prometheus instance depending on your installation if you enabled the annotations or the service monitor.
As this project is built with Go you need to install Go first. The installation of Go is out of the scope of this document, please follow the official documentation. After the installation of Go you need to get the sources:
git clone https://github.com/promhippie/github_exporter.git
cd github_exporter/
All required tool besides Go itself are bundled, all you need is part of the
Makefile
:
make generate build
Finally you should have the binary within the bin/
folder now, give it a try
with ./bin/github_exporter -h
to see all available options.
For the configuration of a webhook on your GitHub repository or your organization you should just follow the defined steps, after that you should receive any webhook related to the defined actions on the exporter if you have followed the instructions from above.
If you want to configure a webhook for your organization just visit
https://github.com/organizations/ORGANIZATION/settings/hooks/new
where you got
to replace ORGANIZATION
with the name of your organization, after that follow
the steps from the screenshots.
If you want to configure a webhook for your repository just visit
https://github.com/ORGANIZATION/REPOSITORY/settings/hooks/new
where you got to
replace ORGANIZATION
with your username or organization and REPOSITORY
with
your repository name, after that follow the steps from the screenshots.
Payload URL should be the endpoint where GitHub can access the exporter
through your reverse proxy, or webserver, or whatever you have configured in
front of the exporter, something like https://exporter.example.com/github
.
Content type should be set to application/json
, but in theory both formats
should be correctly parsed by the exporter.
Secret gets the random password you should have prepared when you have configured the exporter, mentioned above.
Which events would you like to trigger this webhook got to be set to
Let me select individual events
where you just got to check the last item
Workflow runs
.
After hitting the Add webhook button you are ready to receive first webhooks by GitHub. It should also show that the initial test webhook have been executed successfully.
Instead of using personal access tokens you can register a GutHub App. Just head
over to https://github.com/organizations/ORGANIZATION/settings/apps/new
where
you got to replace ORGANIZATION
with the name of your organization.
Feel free to name the application howerever you like, I have named mine by the
organization and exporter, e.g. Promhippie Exporter
. For the description you
can write whatever you want or what sounds best for you:
Within the Identifying and authorizing users section I have unchecked everything, at least for me this have worked without any problem so far:
Within the Post installation and Webhook sections I have also unchecked everything as this won’t be used by the application:
The required permissions have been stripped down to the Administration
within Repository permissions set to read-only
and Administration as
well as Self-hosted runners within Organization permissions set to
read-only
. You are also able to run the exporter with lesser permissions but
than you will loose part of the metrics like the runner or storage related.
Finally I have set the installation to Only for this account which seem to be fine as nobody else got to install the application.
After the creation of the application itself you can already copy the App ID which you will need later to configure the app within the exporter.
Scroll down to Private keys and hit the Generate a private key button in order to download the required certificate which you will also need later on.
Since you are still missing the installation ID click on Install App on the
left sidebar and install the application for your organization, I have enabled
it far all my repositories. On the page where you are getting redirected to you
got to copy the installation ID from the URL, it’s the last numeric part of it.
As an example for me it’s something like
https://github.com/.../installations/43103110
where 43103110
shows the
installation ID you need for the exporter.