Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rawkode committed Mar 15, 2019
1 parent 51cc0fe commit 5d1c02d
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,7 @@
[[constraint]]
name = "github.com/go-logfmt/logfmt"
version = "0.4.0"

[[constraint]]
name = "github.com/google/go-github"
version = "24.0.1"
1 change: 1 addition & 0 deletions plugins/inputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/filecount"
_ "github.com/influxdata/telegraf/plugins/inputs/filestat"
_ "github.com/influxdata/telegraf/plugins/inputs/fluentd"
_ "github.com/influxdata/telegraf/plugins/inputs/github"
_ "github.com/influxdata/telegraf/plugins/inputs/graylog"
_ "github.com/influxdata/telegraf/plugins/inputs/haproxy"
_ "github.com/influxdata/telegraf/plugins/inputs/hddtemp"
Expand Down
35 changes: 35 additions & 0 deletions plugins/inputs/github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# GitHub Input Plugin

The [GitHub](https://www.github.com) input plugin gathers statistics from GitHub repositories.

### Configuration:

```toml
[[inputs.github]]
repositories = [
"owner/repository",
]
```

### Metrics:

For more details about collected metrics reference the [HAProxy CSV format
documentation](https://cbonte.github.io/haproxy-dconv/1.8/management.html#9.1).

- github
- tags:
- `full_name` - The full name of the repository, including owner/organization
- `name` - The repository name
- `owner` - The owner of the repository
- `language` - The primary language of the repository
- fields:
- `stars` (int)
- `forks` (int)
- `open_issues` (int)
- `size` (int)

### Example Output:

```
github,full_name=influxdata/telegraf,language=Go,license=MIT\ License,name=telegraf,owner=influxdata stars=6401i,forks=2421i,open_issues=722i,size=22611i 1552651811000000000
```
128 changes: 128 additions & 0 deletions plugins/inputs/github/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package github

import (
"context"
"log"
"strings"
"sync"
"time"

gh "github.com/google/go-github/github"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
"golang.org/x/oauth2"
)

// GitHub - plugin main structure
type GitHub struct {
Repositories []string
apiKey string
httpTimeout internal.Duration
githubClient *gh.Client
}

const sampleConfig = `
## Specify a list of repositories
repositories = ["influxdata/influxdb"]
## Timeout for GitHub API requests
http_timeout = "5s"
`

// NewGitHub returns a new instance of the GitHub input plugin
func NewGitHub() *GitHub {
return &GitHub{
httpTimeout: internal.Duration{Duration: time.Second * 5},
}
}

// SampleConfig returns sample configuration for this plugin.
func (github *GitHub) SampleConfig() string {
return sampleConfig
}

// Description returns the plugin description.
func (github *GitHub) Description() string {
return "Read repository information from GitHub, including forks, stars, and more."
}

// Create HTTP Client
func (github *GitHub) createGitHubClient() (*gh.Client, error) {
var githubClient *gh.Client

if github.apiKey != "" {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: github.apiKey},
)
tc := oauth2.NewClient(ctx, ts)

githubClient = gh.NewClient(tc)
} else {
githubClient = gh.NewClient(nil)
}

return githubClient, nil
}

// Gather GitHub Metrics
func (github *GitHub) Gather(acc telegraf.Accumulator) error {
if github.githubClient == nil {
githubClient, err := github.createGitHubClient()

if err != nil {
return err
}

github.githubClient = githubClient
}

var wg sync.WaitGroup
wg.Add(len(github.Repositories))

for _, repository := range github.Repositories {
go func(s string, acc telegraf.Accumulator) {
defer wg.Done()

ctx := context.Background()

splits := strings.Split(s, "/")

if len(splits) != 2 {
log.Printf("E! [github]: Error in plugin: %v is not of format 'owner/repository'", s)
return
}

repository, _, _ := github.githubClient.Repositories.Get(ctx, splits[0], splits[1])

fields := make(map[string]interface{})

tags := map[string]string{
"full_name": *repository.FullName,
"owner": *repository.Owner.Login,
"name": *repository.Name,
"language": *repository.Language,
"license": *repository.License.Name,
}

fields["stars"] = repository.StargazersCount
fields["forks"] = repository.ForksCount
fields["open_issues"] = repository.OpenIssuesCount
fields["size"] = repository.Size

now := time.Now()

acc.AddFields("github", fields, tags, now)
}(repository, acc)
}

wg.Wait()
return nil
}

func init() {
inputs.Add("github", func() telegraf.Input {
return &GitHub{}
})
}
1 change: 1 addition & 0 deletions plugins/inputs/github/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package github

0 comments on commit 5d1c02d

Please sign in to comment.