1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,100 @@
# GitHub Input Plugin
This plugin gathers information from projects and repositories hosted on
[GitHub][github].
> [!NOTE]
> Telegraf also contains the [webhook input plugin][webhook] which can be used
> as an alternative method for collecting repository information.
⭐ Telegraf v1.11.0
🏷️ applications
💻 all
[github]: https://www.github.com
[webhook]: /plugins/inputs/webhooks/github
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
In addition to the plugin-specific configuration settings, plugins support
additional global and plugin configuration settings. These settings are used to
modify metrics, tags, and field or create aliases and configure ordering, etc.
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
## Configuration
```toml @sample.conf
# Gather repository information from GitHub hosted repositories.
[[inputs.github]]
## List of repositories to monitor
repositories = [
"influxdata/telegraf",
"influxdata/influxdb"
]
## Github API access token. Unauthenticated requests are limited to 60 per hour.
# access_token = ""
## Github API enterprise url. Github Enterprise accounts must specify their base url.
# enterprise_base_url = ""
## Timeout for HTTP requests.
# http_timeout = "5s"
## List of additional fields to query.
## NOTE: Getting those fields might involve issuing additional API-calls, so please
## make sure you do not exceed the rate-limit of GitHub.
##
## Available fields are:
## - pull-requests -- number of open and closed pull requests (2 API-calls per repository)
# additional_fields = []
```
## Metrics
- github_repository
- tags:
- name - The repository name
- owner - The owner of the repository
- language - The primary language of the repository
- license - The license set for the repository
- fields:
- forks (int)
- open_issues (int)
- networks (int)
- size (int)
- subscribers (int)
- stars (int)
- watchers (int)
When the [internal][internal] input is enabled:
- internal_github
- tags:
- access_token - obfuscated reference to access token or "Unauthenticated"
- fields:
- limit - How many requests you are limited to (per hour)
- remaining - How many requests you have remaining (per hour)
- blocks - How many requests have been blocked due to rate limit
When specifying `additional_fields` the plugin will collect the specified
properties. **NOTE:** Querying this additional fields might require to perform
additional API-calls. Please make sure you don't exceed the query rate-limit by
specifying too many additional fields. In the following we list the available
options with the required API-calls and the resulting fields
- "pull-requests" (2 API-calls per repository)
- fields:
- open_pull_requests (int)
- closed_pull_requests (int)
## Example Output
```text
github_repository,language=Go,license=MIT\ License,name=telegraf,owner=influxdata forks=2679i,networks=2679i,open_issues=794i,size=23263i,stars=7091i,subscribers=316i,watchers=7091i 1563901372000000000
internal_github,access_token=Unauthenticated closed_pull_requests=3522i,rate_limit_remaining=59i,rate_limit_limit=60i,rate_limit_blocks=0i,open_pull_requests=260i 1552653551000000000
```
[internal]: /plugins/inputs/internal

View file

@ -0,0 +1,229 @@
//go:generate ../../../tools/readme_config_includer/generator
package github
import (
"context"
_ "embed"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/selfstat"
)
//go:embed sample.conf
var sampleConfig string
type GitHub struct {
Repositories []string `toml:"repositories"`
AccessToken string `toml:"access_token"`
AdditionalFields []string `toml:"additional_fields"`
EnterpriseBaseURL string `toml:"enterprise_base_url"`
HTTPTimeout config.Duration `toml:"http_timeout"`
githubClient *github.Client
obfuscatedToken string
rateLimit selfstat.Stat
rateLimitErrors selfstat.Stat
rateRemaining selfstat.Stat
}
func (*GitHub) SampleConfig() string {
return sampleConfig
}
func (g *GitHub) Gather(acc telegraf.Accumulator) error {
ctx := context.Background()
if g.githubClient == nil {
githubClient, err := g.createGitHubClient(ctx)
if err != nil {
return err
}
g.githubClient = githubClient
tokenTags := map[string]string{
"access_token": g.obfuscatedToken,
}
g.rateLimitErrors = selfstat.Register("github", "rate_limit_blocks", tokenTags)
g.rateLimit = selfstat.Register("github", "rate_limit_limit", tokenTags)
g.rateRemaining = selfstat.Register("github", "rate_limit_remaining", tokenTags)
}
var wg sync.WaitGroup
wg.Add(len(g.Repositories))
for _, repository := range g.Repositories {
go func(repositoryName string, acc telegraf.Accumulator) {
defer wg.Done()
owner, repository, err := splitRepositoryName(repositoryName)
if err != nil {
acc.AddError(err)
return
}
repositoryInfo, response, err := g.githubClient.Repositories.Get(ctx, owner, repository)
g.handleRateLimit(response, err)
if err != nil {
acc.AddError(err)
return
}
now := time.Now()
tags := getTags(repositoryInfo)
fields := getFields(repositoryInfo)
for _, field := range g.AdditionalFields {
switch field {
case "pull-requests":
// Pull request properties
addFields, err := g.getPullRequestFields(ctx, owner, repository)
if err != nil {
acc.AddError(err)
continue
}
for k, v := range addFields {
fields[k] = v
}
default:
acc.AddError(fmt.Errorf("unknown additional field %q", field))
continue
}
}
acc.AddFields("github_repository", fields, tags, now)
}(repository, acc)
}
wg.Wait()
return nil
}
func (g *GitHub) createGitHubClient(ctx context.Context) (*github.Client, error) {
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
Timeout: time.Duration(g.HTTPTimeout),
}
g.obfuscatedToken = "Unauthenticated"
if g.AccessToken != "" {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: g.AccessToken},
)
oauthClient := oauth2.NewClient(ctx, tokenSource)
_ = context.WithValue(ctx, oauth2.HTTPClient, oauthClient)
g.obfuscatedToken = g.AccessToken[0:4] + "..." + g.AccessToken[len(g.AccessToken)-3:]
return g.newGithubClient(oauthClient)
}
return g.newGithubClient(httpClient)
}
func (g *GitHub) newGithubClient(httpClient *http.Client) (*github.Client, error) {
if g.EnterpriseBaseURL != "" {
return github.NewEnterpriseClient(g.EnterpriseBaseURL, "", httpClient)
}
return github.NewClient(httpClient), nil
}
func (g *GitHub) handleRateLimit(response *github.Response, err error) {
var rlErr *github.RateLimitError
if err == nil {
g.rateLimit.Set(int64(response.Rate.Limit))
g.rateRemaining.Set(int64(response.Rate.Remaining))
} else if errors.As(err, &rlErr) {
g.rateLimitErrors.Incr(1)
}
}
func splitRepositoryName(repositoryName string) (owner, repository string, err error) {
splits := strings.SplitN(repositoryName, "/", 2)
if len(splits) != 2 {
return "", "", fmt.Errorf("%v is not of format 'owner/repository'", repositoryName)
}
return splits[0], splits[1], nil
}
func getLicense(rI *github.Repository) string {
if licenseName := rI.GetLicense().GetName(); licenseName != "" {
return licenseName
}
return "None"
}
func getTags(repositoryInfo *github.Repository) map[string]string {
return map[string]string{
"owner": repositoryInfo.GetOwner().GetLogin(),
"name": repositoryInfo.GetName(),
"language": repositoryInfo.GetLanguage(),
"license": getLicense(repositoryInfo),
}
}
func getFields(repositoryInfo *github.Repository) map[string]interface{} {
return map[string]interface{}{
"stars": repositoryInfo.GetStargazersCount(),
"subscribers": repositoryInfo.GetSubscribersCount(),
"watchers": repositoryInfo.GetWatchersCount(),
"networks": repositoryInfo.GetNetworkCount(),
"forks": repositoryInfo.GetForksCount(),
"open_issues": repositoryInfo.GetOpenIssuesCount(),
"size": repositoryInfo.GetSize(),
}
}
func (g *GitHub) getPullRequestFields(ctx context.Context, owner, repo string) (map[string]interface{}, error) {
options := github.SearchOptions{
TextMatch: false,
ListOptions: github.ListOptions{
PerPage: 100,
Page: 1,
},
}
classes := []string{"open", "closed"}
fields := make(map[string]interface{})
for _, class := range classes {
q := fmt.Sprintf("repo:%s/%s is:pr is:%s", owner, repo, class)
searchResult, response, err := g.githubClient.Search.Issues(ctx, q, &options)
g.handleRateLimit(response, err)
if err != nil {
return fields, err
}
f := class + "_pull_requests"
fields[f] = searchResult.GetTotal()
}
return fields, nil
}
func init() {
inputs.Add("github", func() telegraf.Input {
return &GitHub{
HTTPTimeout: config.Duration(time.Second * 5),
}
})
}

View file

@ -0,0 +1,140 @@
package github
import (
"net/http"
"testing"
gh "github.com/google/go-github/v32/github"
"github.com/stretchr/testify/require"
)
func TestNewGithubClient(t *testing.T) {
httpClient := &http.Client{}
g := &GitHub{}
client, err := g.newGithubClient(httpClient)
require.NoError(t, err)
require.Contains(t, client.BaseURL.String(), "api.github.com")
g.EnterpriseBaseURL = "api.example.com/"
enterpriseClient, err := g.newGithubClient(httpClient)
require.NoError(t, err)
require.Contains(t, enterpriseClient.BaseURL.String(), "api.example.com")
}
func TestSplitRepositoryNameWithWorkingExample(t *testing.T) {
var validRepositoryNames = []struct {
fullName string
owner string
repository string
}{
{"influxdata/telegraf", "influxdata", "telegraf"},
{"influxdata/influxdb", "influxdata", "influxdb"},
{"rawkode/saltstack-dotfiles", "rawkode", "saltstack-dotfiles"},
}
for _, tt := range validRepositoryNames {
t.Run(tt.fullName, func(t *testing.T) {
owner, repository, err := splitRepositoryName(tt.fullName)
require.NoError(t, err)
require.Equal(t, tt.owner, owner)
require.Equal(t, tt.repository, repository)
})
}
}
func TestSplitRepositoryNameWithNoSlash(t *testing.T) {
var invalidRepositoryNames = []string{
"influxdata-influxdb",
}
for _, tt := range invalidRepositoryNames {
t.Run(tt, func(t *testing.T) {
_, _, err := splitRepositoryName(tt)
require.Error(t, err)
})
}
}
func TestGetLicenseWhenExists(t *testing.T) {
licenseName := "MIT"
license := gh.License{Name: &licenseName}
repository := gh.Repository{License: &license}
getLicenseReturn := getLicense(&repository)
require.Equal(t, "MIT", getLicenseReturn)
}
func TestGetLicenseWhenMissing(t *testing.T) {
repository := gh.Repository{}
getLicenseReturn := getLicense(&repository)
require.Equal(t, "None", getLicenseReturn)
}
func TestGetTags(t *testing.T) {
licenseName := "MIT"
license := gh.License{Name: &licenseName}
ownerName := "influxdata"
owner := gh.User{Login: &ownerName}
fullName := "influxdata/influxdb"
repositoryName := "influxdb"
language := "Go"
repository := gh.Repository{
FullName: &fullName,
Name: &repositoryName,
License: &license,
Owner: &owner,
Language: &language,
}
getTagsReturn := getTags(&repository)
correctTagsReturn := map[string]string{
"owner": ownerName,
"name": repositoryName,
"language": language,
"license": licenseName,
}
require.Equal(t, getTagsReturn, correctTagsReturn)
}
func TestGetFields(t *testing.T) {
stars := 1
forks := 2
openIssues := 3
size := 4
subscribers := 5
watchers := 6
repository := gh.Repository{
StargazersCount: &stars,
ForksCount: &forks,
OpenIssuesCount: &openIssues,
Size: &size,
NetworkCount: &forks,
SubscribersCount: &subscribers,
WatchersCount: &watchers,
}
getFieldsReturn := getFields(&repository)
correctFieldReturn := make(map[string]interface{})
correctFieldReturn["stars"] = 1
correctFieldReturn["forks"] = 2
correctFieldReturn["networks"] = 2
correctFieldReturn["open_issues"] = 3
correctFieldReturn["size"] = 4
correctFieldReturn["subscribers"] = 5
correctFieldReturn["watchers"] = 6
require.Equal(t, getFieldsReturn, correctFieldReturn)
}

View file

@ -0,0 +1,24 @@
# Gather repository information from GitHub hosted repositories.
[[inputs.github]]
## List of repositories to monitor
repositories = [
"influxdata/telegraf",
"influxdata/influxdb"
]
## Github API access token. Unauthenticated requests are limited to 60 per hour.
# access_token = ""
## Github API enterprise url. Github Enterprise accounts must specify their base url.
# enterprise_base_url = ""
## Timeout for HTTP requests.
# http_timeout = "5s"
## List of additional fields to query.
## NOTE: Getting those fields might involve issuing additional API-calls, so please
## make sure you do not exceed the rate-limit of GitHub.
##
## Available fields are:
## - pull-requests -- number of open and closed pull requests (2 API-calls per repository)
# additional_fields = []