Path: blob/main/pkg/integrations/github_exporter/github_exporter.go
5283 views
package github_exporter //nolint:golint12import (3"fmt"4"net/url"56"github.com/go-kit/log"7"github.com/go-kit/log/level"8"github.com/grafana/agent/pkg/integrations"9integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"10"github.com/grafana/agent/pkg/integrations/v2/metricsutils"11gh_config "github.com/infinityworks/github-exporter/config"12"github.com/infinityworks/github-exporter/exporter"13config_util "github.com/prometheus/common/config"14)1516// DefaultConfig holds the default settings for the github_exporter integration17var DefaultConfig = Config{18APIURL: "https://api.github.com",19}2021// Config controls github_exporter22type Config struct {23// URL for the GitHub API24APIURL string `yaml:"api_url,omitempty"`2526// A list of GitHub repositories for which to collect metrics.27Repositories []string `yaml:"repositories,omitempty"`2829// A list of GitHub organizations for which to collect metrics.30Organizations []string `yaml:"organizations,omitempty"`3132// A list of GitHub users for which to collect metrics.33Users []string `yaml:"users,omitempty"`3435// A GitHub authentication token that allows the API to be queried more often.36APIToken config_util.Secret `yaml:"api_token,omitempty"`3738// 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`39APITokenFile string `yaml:"api_token_file,omitempty"`40}4142// UnmarshalYAML implements yaml.Unmarshaler for Config43func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {44*c = DefaultConfig4546type plain Config47return unmarshal((*plain)(c))48}4950// Name returns the name of the integration that this config represents.51func (c *Config) Name() string {52return "github_exporter"53}5455// InstanceKey returns the hostname:port of the GitHub API server.56func (c *Config) InstanceKey(agentKey string) (string, error) {57u, err := url.Parse(c.APIURL)58if err != nil {59return "", fmt.Errorf("could not parse url: %w", err)60}61return u.Host, nil62}6364// NewIntegration creates a new github_exporter65func (c *Config) NewIntegration(logger log.Logger) (integrations.Integration, error) {66return New(logger, c)67}6869func init() {70integrations.RegisterIntegration(&Config{})71integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("github"))72}7374// New creates a new github_exporter integration.75func New(logger log.Logger, c *Config) (integrations.Integration, error) {76conf := gh_config.Config{}77err := conf.SetAPIURL(c.APIURL)78if err != nil {79level.Error(logger).Log("msg", "api url is invalid", "err", err)80return nil, err81}82conf.SetRepositories(c.Repositories)83conf.SetOrganisations(c.Organizations)84conf.SetUsers(c.Users)85if c.APIToken != "" {86conf.SetAPIToken(string(c.APIToken))87}88if c.APITokenFile != "" {89err = conf.SetAPITokenFromFile(c.APITokenFile)90if err != nil {91level.Error(logger).Log("msg", "unable to load GitHub API token from file", "err", err)92return nil, err93}94}9596ghExporter := exporter.Exporter{97APIMetrics: exporter.AddMetrics(),98Config: conf,99}100101return integrations.NewCollectorIntegration(102c.Name(),103integrations.WithCollectors(&ghExporter),104), nil105}106107108