Skip to content

Commit 9970748

Browse files
authored
Merge pull request #11 from hairyhenderson/support-github-token-from-file
Support reading GITHUB_TOKEN from a file
2 parents 4e210a5 + bafdfef commit 9970748

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Prometheus GitHub Exporter
22

3-
Exposes basic metrics for your repositories from the GitHub API, to a Prometheus compatible endpoint.
3+
Exposes basic metrics for your repositories from the GitHub API, to a Prometheus compatible endpoint.
44

55
## Configuration
66

77
This exporter is setup to take input from environment variables:
88
* `BIND_PORT` The port you wish to run the container on, defaults to `9171`
9-
* `GITHUB_TOKEN` If supplied, enables the user to supply a github authentication token that allows the API to be quried more often. Optional, but reccomended.
10-
* `ORGS` If supplied, the exporter will enumerate all repositories for that orginisation.
9+
* `GITHUB_TOKEN` If supplied, enables the user to supply a github authentication token that allows the API to be queried more often. Optional, but recommended.
10+
* `GITHUB_TOKEN_FILE` If supplied _instead of_ `GITHUB_TOKEN`, enables the user to supply a path to a file containing a github authentication token that allows the API to be queried more often. Optional, but recommended.
11+
* `ORGS` If supplied, the exporter will enumerate all repositories for that organization.
1112
* `REPOS` If supplied, The images you wish to monitor, expected in the format "user/repo1, user/repo2". Can be across different Github users/orgs.
1213

1314
## Install and deploy

github_exporter.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,25 @@ def _collect_org_metrics(self, gauges, metrics):
5858
for repo in response_json:
5959
self._add_metrics(gauges, metrics, repo)
6060

61+
def _get_github_token(self):
62+
if os.getenv('GITHUB_TOKEN'):
63+
return os.getenv('GITHUB_TOKEN')
64+
elif os.getenv('GITHUB_TOKEN_FILE'):
65+
return open(os.getenv('GITHUB_TOKEN_FILE'), 'r').read().rstrip()
66+
else:
67+
return None
68+
6169
def _get_json(self, url: str):
6270
"""
6371
using github core api
6472
rate limit 5000 per hours
6573
"""
6674
print("Getting JSON Payload for " + url)
67-
if os.getenv('GITHUB_TOKEN'):
68-
payload = {"access_token":os.environ["GITHUB_TOKEN"]}
75+
gh_token = self._get_github_token()
76+
if gh_token:
77+
payload = {"access_token": gh_token}
6978
r = requests.get(url,params=payload)
70-
else:
79+
else:
7180
r = requests.get(url)
7281
result = json.loads(r.content.decode('UTF-8'))
7382
if result is None:
@@ -91,9 +100,10 @@ def _pagination(self, response: requests.Response, result):
91100

92101
def _check_api_limit(self):
93102
rate_limit_url = "https://api.github.com/rate_limit"
94-
if os.getenv('GITHUB_TOKEN'):
95-
print("Authentication token detected: " + os.getenv('GITHUB_TOKEN'))
96-
payload = {"access_token":os.environ["GITHUB_TOKEN"]}
103+
gh_token = self._get_github_token()
104+
if gh_token:
105+
print("Authentication token detected: " + gh_token)
106+
payload = {"access_token": gh_token}
97107
R = requests.get(rate_limit_url,params=payload)
98108
else:
99109
R = requests.get(rate_limit_url)
@@ -119,6 +129,6 @@ def sigterm_handler(_signo, _stack_frame):
119129
exit(1)
120130
start_http_server(int(os.getenv('BIND_PORT')))
121131
REGISTRY.register(GitHubCollector())
122-
132+
123133
signal.signal(signal.SIGTERM, sigterm_handler)
124-
while True: time.sleep(1)
134+
while True: time.sleep(1)

0 commit comments

Comments
 (0)