Skip to content

Commit 54bddeb

Browse files
authored
Merge pull request #48 from adebasi/master
Allow to specify no targets
2 parents 25f7e0c + 410057a commit 54bddeb

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ Exposes basic metrics for your repositories from the GitHub API, to a Prometheus
66

77
## Configuration
88

9-
This exporter is setup to take input from environment variables:
9+
This exporter is setup to take input from environment variables. All variables are optional:
1010

11-
### Required
1211
* `ORGS` If supplied, the exporter will enumerate all repositories for that organization. Expected in the format "org1, org2".
1312
* `REPOS` If supplied, The repos you wish to monitor, expected in the format "user/repo1, user/repo2". Can be across different Github users/orgs.
1413
* `USERS` If supplied, the exporter will enumerate all repositories for that users. Expected in
1514
the format "user1, user2".
16-
17-
At least one of those 3 options should be provided.
18-
19-
### Optional
2015
* `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.
2116
* `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.
2217
* `API_URL` Github API URL, shouldn't need to change this. Defaults to `https://api.github.com`

config/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ func getScrapeURLs(apiURL, repos, orgs, users string) ([]string, error) {
6565

6666
opts := "?&per_page=100" // Used to set the Github API to return 100 results per page (max)
6767

68-
// User input validation, check that either repositories or organisations have been passed in
6968
if len(repos) == 0 && len(orgs) == 0 && len(users) == 0 {
70-
return urls, fmt.Errorf("No targets specified")
69+
log.Info("No targets specified. Only rate limit endpoint will be scraped")
7170
}
7271

7372
// Append repositories to the array

exporter/gather.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
)
1111

1212
// gatherData - Collects the data from the API and stores into struct
13-
func (e *Exporter) gatherData() ([]*Datum, *RateLimits, error) {
13+
func (e *Exporter) gatherData() ([]*Datum, error) {
1414

1515
data := []*Datum{}
1616

1717
responses, err := asyncHTTPGets(e.TargetURLs, e.APIToken)
1818

1919
if err != nil {
20-
return data, nil, err
20+
return data, err
2121
}
2222

2323
for _, response := range responses {
@@ -46,26 +46,19 @@ func (e *Exporter) gatherData() ([]*Datum, *RateLimits, error) {
4646
log.Infof("API data fetched for repository: %s", response.url)
4747
}
4848

49-
// Check the API rate data and store as a metric
50-
rates, err := getRates(e.APIURL, e.APIToken)
51-
52-
if err != nil {
53-
log.Errorf("Unable to obtain rate limit data from API, Error: %s", err)
54-
}
55-
5649
//return data, rates, err
57-
return data, rates, nil
50+
return data, nil
5851

5952
}
6053

6154
// getRates obtains the rate limit data for requests against the github API.
6255
// Especially useful when operating without oauth and the subsequent lower cap.
63-
func getRates(baseURL string, token string) (*RateLimits, error) {
56+
func (e *Exporter) getRates() (*RateLimits, error) {
6457

6558
rateEndPoint := ("/rate_limit")
66-
url := fmt.Sprintf("%s%s", baseURL, rateEndPoint)
59+
url := fmt.Sprintf("%s%s", e.APIURL, rateEndPoint)
6760

68-
resp, err := getHTTPResponse(url, token)
61+
resp, err := getHTTPResponse(url, e.APIToken)
6962
if err != nil {
7063
return &RateLimits{}, err
7164
}

exporter/prometheus.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
1818
// This function is called when a scrape is peformed on the /metrics page
1919
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
2020

21+
data := []*Datum{}
22+
var err error
2123
// Scrape the Data from Github
22-
var data, rates, err = e.gatherData()
24+
if len(e.TargetURLs) > 0 {
25+
data, err = e.gatherData()
26+
if err != nil {
27+
log.Errorf("Error gathering Data from remote API: %v", err)
28+
return
29+
}
30+
}
2331

32+
rates, err := e.getRates()
2433
if err != nil {
25-
log.Errorf("Error gathering Data from remote API: %v", err)
34+
log.Errorf("Error gathering Rates from remote API: %v", err)
2635
return
2736
}
2837

0 commit comments

Comments
 (0)