Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github_repo_open_issues and github_repo_pull_request_count separately #46

Merged
merged 16 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- "1.12"
- "1.14"

script:
- diff -u <(echo -n) <(gofmt -s -d ./)
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.12-stretch as build
FROM golang:1.14.0-stretch as build
LABEL maintainer="Infinity Works"

ENV GO111MODULE=on
Expand All @@ -10,7 +10,7 @@ RUN go mod download \
&& go test ./... \
&& CGO_ENABLED=0 GOOS=linux go build -o /bin/main

FROM alpine:3.10
FROM alpine:3.11.3

RUN apk --no-cache add ca-certificates \
&& addgroup exporter \
Expand Down
18 changes: 18 additions & 0 deletions exporter/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (e *Exporter) gatherData() ([]*Datum, *RateLimits, error) {
if strings.Contains(response.url, "/repos/") {
getReleases(e, response.url, &d.Releases)
}
// Get PRs
if strings.Contains(response.url, "/repos/") {
getPRs(e, response.url, &d.Pulls)
}
json.Unmarshal(response.body, &d)
data = append(data, d)
}
Expand Down Expand Up @@ -111,6 +115,20 @@ func getReleases(e *Exporter, url string, data *[]Release) {
json.Unmarshal(releasesResponse[0].body, &data)
}

func getPRs(e *Exporter, url string, data *[]Pull) {
i := strings.Index(url, "?")
baseURL := url[:i]
pullsURL := baseURL + "/pulls"
pullsResponse, err := asyncHTTPGets([]string{pullsURL}, e.APIToken)

if err != nil {
log.Errorf("Unable to obtain pull requests from API, Error: %s", err)
}
fmt.Println(&data)

json.Unmarshal(pullsResponse[0].body, &data)
}

// isArray simply looks for key details that determine if the JSON response is an array or not.
func isArray(body []byte) bool {

Expand Down
15 changes: 14 additions & 1 deletion exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func AddMetrics() map[string]*prometheus.Desc {
"Total number of open issues for given repository",
[]string{"repo", "user", "private", "fork", "archived", "license", "language"}, nil,
)
APIMetrics["PullRequestCount"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "pull_request_count"),
"Total number of pull requests for given repository",
[]string{"repo"}, nil,
)
APIMetrics["Watchers"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "watchers"),
"Total number of watchers/subscribers for given repository",
Expand Down Expand Up @@ -64,7 +69,6 @@ func (e *Exporter) processMetrics(data []*Datum, rates *RateLimits, ch chan<- pr
for _, x := range data {
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Stars"], prometheus.GaugeValue, x.Stars, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Forks"], prometheus.GaugeValue, x.Forks, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["OpenIssues"], prometheus.GaugeValue, x.OpenIssues, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Watchers"], prometheus.GaugeValue, x.Watchers, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Size"], prometheus.GaugeValue, x.Size, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)

Expand All @@ -73,6 +77,15 @@ func (e *Exporter) processMetrics(data []*Datum, rates *RateLimits, ch chan<- pr
ch <- prometheus.MustNewConstMetric(e.APIMetrics["ReleaseDownloads"], prometheus.GaugeValue, float64(asset.Downloads), x.Name, x.Owner.Login, release.Name, asset.Name, asset.CreatedAt)
}
}
prCount := 0
for range x.Pulls {
prCount += 1
}
// issueCount = x.OpenIssue - prCount
ch <- prometheus.MustNewConstMetric(e.APIMetrics["OpenIssues"], prometheus.GaugeValue, (x.OpenIssues - float64(prCount)), x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)

// prCount
ch <- prometheus.MustNewConstMetric(e.APIMetrics["PullRequestCount"], prometheus.GaugeValue, float64(prCount), x.Name)
}

// Set Rate limit stats
Expand Down
8 changes: 8 additions & 0 deletions exporter/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ type Datum struct {
Watchers float64 `json:"subscribers_count"`
Size float64 `json:"size"`
Releases []Release
Pulls []Pull
}

type Release struct {
Name string `json:"name"`
Assets []Asset `json:"assets"`
}

type Pull struct {
Url string `json:"url"`
User struct {
Login string `json:"login"`
} `json:"user"`
}

type Asset struct {
Name string `json:"name"`
Size int64 `json:"size"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/infinityworks/github-exporter

go 1.12
go 1.14

require (
github.com/fatih/structs v1.1.0
Expand Down
13 changes: 13 additions & 0 deletions test/github_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ func TestGithubExporter(t *testing.T) {
githubRepos(),
githubRateLimit(),
githubReleases(),
githubPulls(),
).
Get("/metrics").
Expect(t).
Assert(bodyContains(`github_rate_limit 60`)).
Assert(bodyContains(`github_rate_remaining 60`)).
Assert(bodyContains(`github_rate_reset 1.566853865e+09`)).
Assert(bodyContains(`github_repo_forks{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 10`)).
Assert(bodyContains(`github_repo_pull_request_count{repo="myRepo"} 3`)).
Assert(bodyContains(`github_repo_open_issues{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 2`)).
Assert(bodyContains(`github_repo_size_kb{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 946`)).
Assert(bodyContains(`github_repo_stars{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 120`)).
Expand Down Expand Up @@ -105,6 +107,17 @@ func githubReleases() *apitest.Mock {
End()
}

func githubPulls() *apitest.Mock {
return apitest.NewMock().
Get("https://api.github.com/repos/myOrg/myRepo/pulls").
Header("Authorization", "token 12345").
RespondWith().
Times(2).
Body(readFile("testdata/pulls_response.json")).
Status(http.StatusOK).
End()
}

func readFile(path string) string {
bytes, err := ioutil.ReadFile(path)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions test/testdata/my_repo_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 2,
"open_issues_count": 5,
"license": {
"key": "mit",
"name": "MIT License",
Expand All @@ -94,9 +94,9 @@
"node_id": "MDc6TGljZW5zZTEz"
},
"forks": 10,
"open_issues": 2,
"open_issues": 5,
"watchers": 120,
"default_branch": "master",
"network_count": 10,
"subscribers_count": 5
}
}
Loading