Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed the issue where multiple repos was not working as intended
  • Loading branch information
kpachhai committed Mar 25, 2019
commit 97c8fb59ac59c3b3fe8b01345bf49814e8251b34
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github-exporter
32 changes: 18 additions & 14 deletions exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func AddMetrics() map[string]*prometheus.Desc {
APIMetrics["CommitsHistory"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "commit", "count"),
"Total number of commits for given repository and given branch",
[]string{"branch", "author"}, nil,
[]string{"repo", "branch", "author"}, nil,
)
APIMetrics["LatestCommit"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "commit", "latest"),
"Latest Commit for a given repository and given branch",
[]string{"branch", "author", "date", "commithash"}, nil,
[]string{"repo", "branch", "author", "date", "commithash"}, nil,
)
APIMetrics["Limit"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "rate", "limit"),
Expand Down Expand Up @@ -79,25 +79,29 @@ func (e *Exporter) processMetrics(data []*Datum, commitData []*CommitDatum, rate
}

branch := e.Config.Branch
latestCommitAuthor, latestCommitDate, latestCommitHash := "", "", ""
totalCommits := make(map[string]float64)
for i, x := range commitData {
if i == 0 {
latestCommitDate = strings.Split(x.Commit.Author.Date, "T")[0]
latestCommitAuthor = x.Commit.Author.Name
latestCommitHash = x.CommitHash
}
latestCommits := make(map[string]*LatestCommitHistory)
totalCommits := make(map[string]*CommitHistory)
for _, x := range commitData {
shortenedRepo := strings.Replace(x.URL, "https://api.github.com/repos/", "", -1)
repo := shortenedRepo[:strings.Index(shortenedRepo, "/commits")]
author := x.Commit.Author.Name
if _, ok := latestCommits[repo]; !ok {
date := strings.Split(x.Commit.Author.Date, "T")[0]
hash := x.CommitHash
latestCommits[repo] = &LatestCommitHistory{author, date, hash}
}
if _, ok := totalCommits[author]; ok {
totalCommits[author]++
totalCommits[author].Count++
} else {
totalCommits[author] = 1.0
totalCommits[author] = &CommitHistory{repo, 1.0}
}
}
for author, val := range totalCommits {
ch <- prometheus.MustNewConstMetric(e.APIMetrics["CommitsHistory"], prometheus.GaugeValue, val, branch, author)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["CommitsHistory"], prometheus.GaugeValue, val.Count, val.Repo, branch, author)
}
for repo, val := range latestCommits {
ch <- prometheus.MustNewConstMetric(e.APIMetrics["LatestCommit"], prometheus.GaugeValue, 1.0, repo, branch, val.Author, val.Date, val.Hash)
}
ch <- prometheus.MustNewConstMetric(e.APIMetrics["LatestCommit"], prometheus.GaugeValue, 1.0, branch, latestCommitAuthor, latestCommitDate, latestCommitHash)

// Set Rate limit stats
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Limit"], prometheus.GaugeValue, rates.Limit)
Expand Down
11 changes: 11 additions & 0 deletions exporter/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ type CommitDatum struct {
} `json:"parents"`
}

type CommitHistory struct {
Repo string
Count float64
}

type LatestCommitHistory struct {
Author string
Date string
Hash string
}

// RateLimits is used to store rate limit data into a struct
// This data is later represented as a metric, captured at the end of a scrape
type RateLimits struct {
Expand Down