Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/github_exporter/github_exporter.go
5283 views
1
package github_exporter //nolint:golint
2
3
import (
4
"fmt"
5
"net/url"
6
7
"github.com/go-kit/log"
8
"github.com/go-kit/log/level"
9
"github.com/grafana/agent/pkg/integrations"
10
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
11
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
12
gh_config "github.com/infinityworks/github-exporter/config"
13
"github.com/infinityworks/github-exporter/exporter"
14
config_util "github.com/prometheus/common/config"
15
)
16
17
// DefaultConfig holds the default settings for the github_exporter integration
18
var DefaultConfig = Config{
19
APIURL: "https://api.github.com",
20
}
21
22
// Config controls github_exporter
23
type Config struct {
24
// URL for the GitHub API
25
APIURL string `yaml:"api_url,omitempty"`
26
27
// A list of GitHub repositories for which to collect metrics.
28
Repositories []string `yaml:"repositories,omitempty"`
29
30
// A list of GitHub organizations for which to collect metrics.
31
Organizations []string `yaml:"organizations,omitempty"`
32
33
// A list of GitHub users for which to collect metrics.
34
Users []string `yaml:"users,omitempty"`
35
36
// A GitHub authentication token that allows the API to be queried more often.
37
APIToken config_util.Secret `yaml:"api_token,omitempty"`
38
39
// A path to a file containing a GitHub authentication token that allows the API to be queried more often. If supplied, this supersedes `api_token`
40
APITokenFile string `yaml:"api_token_file,omitempty"`
41
}
42
43
// UnmarshalYAML implements yaml.Unmarshaler for Config
44
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
45
*c = DefaultConfig
46
47
type plain Config
48
return unmarshal((*plain)(c))
49
}
50
51
// Name returns the name of the integration that this config represents.
52
func (c *Config) Name() string {
53
return "github_exporter"
54
}
55
56
// InstanceKey returns the hostname:port of the GitHub API server.
57
func (c *Config) InstanceKey(agentKey string) (string, error) {
58
u, err := url.Parse(c.APIURL)
59
if err != nil {
60
return "", fmt.Errorf("could not parse url: %w", err)
61
}
62
return u.Host, nil
63
}
64
65
// NewIntegration creates a new github_exporter
66
func (c *Config) NewIntegration(logger log.Logger) (integrations.Integration, error) {
67
return New(logger, c)
68
}
69
70
func init() {
71
integrations.RegisterIntegration(&Config{})
72
integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("github"))
73
}
74
75
// New creates a new github_exporter integration.
76
func New(logger log.Logger, c *Config) (integrations.Integration, error) {
77
conf := gh_config.Config{}
78
err := conf.SetAPIURL(c.APIURL)
79
if err != nil {
80
level.Error(logger).Log("msg", "api url is invalid", "err", err)
81
return nil, err
82
}
83
conf.SetRepositories(c.Repositories)
84
conf.SetOrganisations(c.Organizations)
85
conf.SetUsers(c.Users)
86
if c.APIToken != "" {
87
conf.SetAPIToken(string(c.APIToken))
88
}
89
if c.APITokenFile != "" {
90
err = conf.SetAPITokenFromFile(c.APITokenFile)
91
if err != nil {
92
level.Error(logger).Log("msg", "unable to load GitHub API token from file", "err", err)
93
return nil, err
94
}
95
}
96
97
ghExporter := exporter.Exporter{
98
APIMetrics: exporter.AddMetrics(),
99
Config: conf,
100
}
101
102
return integrations.NewCollectorIntegration(
103
c.Name(),
104
integrations.WithCollectors(&ghExporter),
105
), nil
106
}
107
108