Skip to content

Commit

Permalink
Add support for having metric specific cron
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jul 21, 2022
1 parent 50d70a0 commit f61a2ce
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 43 deletions.
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func goCdExport(context *cli.Context) error {
log.Println(err)
}

signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP, syscall.SIGQUIT)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP, syscall.SIGQUIT) //nolint:govet

promLogConfig := &promlog.Config{Level: &promlog.AllowedLevel{}, Format: &promlog.AllowedFormat{}}
if err := promLogConfig.Level.Set(finalConfig.LogLevel); err != nil {
Expand Down Expand Up @@ -228,6 +228,7 @@ func goCdExport(context *cli.Context) error {
finalConfig.LogLevel,
finalConfig.APICron,
finalConfig.DiskCron,
finalConfig.MetricCron,
caContent,
pipelinePaths,
logger,
Expand Down
4 changes: 3 additions & 1 deletion gocd-prometheus-exporter.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
gocd-pipelines-path:
- /path/to/pipeline/directory1
- /path/to/pipeline/directory2
- /path/to/pipeline/directory3
- /path/to/pipeline/directory3
metric-cron:
agent_down: "@every 60s"
29 changes: 15 additions & 14 deletions pkg/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ import (

// Config holds the information of the app gocd-prometheus-exporter.
type Config struct {
GoCdBaseURL string `json:"gocd-base-url,omitempty" yaml:"gocd-base-url,omitempty"`
GoCdUserName string `json:"gocd-username,omitempty" yaml:"gocd-username,omitempty"`
GoCdPassword string `json:"gocd-password,omitempty" yaml:"gocd-password,omitempty"`
InsecureTLS bool `json:"insecure-tls,omitempty" yaml:"insecure-tls,omitempty"`
GoCdPipelinesPath []string `json:"gocd-pipelines-path,omitempty" yaml:"gocd-pipelines-path,omitempty"`
GoCdPipelinesRootPath string `json:"gocd-pipelines-root-path,omitempty" yaml:"gocd-pipelines-root-path,omitempty"`
CaPath string `json:"ca-path,omitempty" yaml:"ca-path,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"`
Endpoint string `json:"metric-endpoint,omitempty" yaml:"metric-endpoint,omitempty"`
LogLevel string `json:"log-level,omitempty" yaml:"log-level,omitempty"`
SkipMetrics []string `json:"skip-metrics,omitempty" yaml:"skip-metrics,omitempty"`
APICron string `json:"api-cron-schedule,omitempty" yaml:"api-cron-schedule,omitempty"`
DiskCron string `json:"disk-cron,omitempty" yaml:"disk-cron,omitempty"`
AppGraceDuration time.Duration `json:"grace-duration,omitempty" yaml:"grace-duration,omitempty"`
GoCdBaseURL string `json:"gocd-base-url,omitempty" yaml:"gocd-base-url,omitempty"`
GoCdUserName string `json:"gocd-username,omitempty" yaml:"gocd-username,omitempty"`
GoCdPassword string `json:"gocd-password,omitempty" yaml:"gocd-password,omitempty"`
InsecureTLS bool `json:"insecure-tls,omitempty" yaml:"insecure-tls,omitempty"`
GoCdPipelinesPath []string `json:"gocd-pipelines-path,omitempty" yaml:"gocd-pipelines-path,omitempty"`
GoCdPipelinesRootPath string `json:"gocd-pipelines-root-path,omitempty" yaml:"gocd-pipelines-root-path,omitempty"`
CaPath string `json:"ca-path,omitempty" yaml:"ca-path,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"`
Endpoint string `json:"metric-endpoint,omitempty" yaml:"metric-endpoint,omitempty"`
LogLevel string `json:"log-level,omitempty" yaml:"log-level,omitempty"`
SkipMetrics []string `json:"skip-metrics,omitempty" yaml:"skip-metrics,omitempty"`
APICron string `json:"api-cron-schedule,omitempty" yaml:"api-cron-schedule,omitempty"`
DiskCron string `json:"disk-cron,omitempty" yaml:"disk-cron,omitempty"`
MetricCron map[string]string `json:"metric-cron,omitempty" yaml:"metric-cron,omitempty"`
AppGraceDuration time.Duration `json:"grace-duration,omitempty" yaml:"grace-duration,omitempty"`
}

// GetConfig returns the new instance of Config.
Expand Down
2 changes: 1 addition & 1 deletion pkg/gocd/admins.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (conf *client) GetAdminsInfo() (SystemAdmins, error) {

func (conf *client) configureAdminsInfo() {
scheduleGetAdmins := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetAdmins.AddFunc(conf.apiCron, func() {
_, err := scheduleGetAdmins.AddFunc(conf.getCron(common.MetricSystemAdminsCount), func() {
admins, err := conf.GetAdminsInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("system admin", err.Error())) //nolint:errcheck
Expand Down
4 changes: 2 additions & 2 deletions pkg/gocd/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (conf *client) GetAgentJobRunHistory() ([]AgentJobHistory, error) {

func (conf *client) configureGetAgentsInfo() {
scheduleGetAgentsInfo := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetAgentsInfo.AddFunc(conf.apiCron, func() {
_, err := scheduleGetAgentsInfo.AddFunc(conf.getCron(common.MetricAgentDown), func() {
agentsInfo, err := conf.GetAgentsInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("agents", err.Error())) //nolint:errcheck
Expand All @@ -85,7 +85,7 @@ func (conf *client) configureGetAgentsInfo() {

func (conf *client) configureGetAgentJobRunHistory() {
scheduleGetAgentJobRunHistory := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetAgentJobRunHistory.AddFunc(conf.apiCron, func() {
_, err := scheduleGetAgentJobRunHistory.AddFunc(conf.getCron(common.MetricJobStatus), func() {
agentsJobRunHistory, err := conf.GetAgentJobRunHistory()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("agents", err.Error())) //nolint:errcheck
Expand Down
1 change: 1 addition & 0 deletions pkg/gocd/agents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Test_client_GetAgentJobRunHistory(t *testing.T) {
"",
nil,
nil,
nil,
logger,
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/gocd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (conf *client) GetBackupInfo() (BackupConfig, error) {

func (conf *client) configureGetBackupInfo() {
scheduleGetBackupInfo := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetBackupInfo.AddFunc(conf.apiCron, func() {
_, err := scheduleGetBackupInfo.AddFunc(conf.getCron(common.MetricConfiguredBackup), func() {
backupInfo, err := conf.GetBackupInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("gocd backup", err.Error())) //nolint:errcheck
Expand Down
1 change: 1 addition & 0 deletions pkg/gocd/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestConfig_GetBackupInfo(t *testing.T) {
"",
nil,
nil,
nil,
logger,
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/gocd/configrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (conf *client) GetConfigRepoInfo() ([]ConfigRepo, error) {

func (conf *client) configureGetConfigRepo() {
scheduleGetConfigRepo := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetConfigRepo.AddFunc(conf.apiCron, func() {
_, err := scheduleGetConfigRepo.AddFunc(conf.getCron(common.MetricConfigRepoCount), func() {
repos, err := conf.GetConfigRepoInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("config repo", err.Error())) //nolint:errcheck
Expand Down
1 change: 1 addition & 0 deletions pkg/gocd/configrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestConfig_GetConfigRepoInfo(t *testing.T) {
"",
nil,
nil,
nil,
logger,
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/gocd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (conf *client) GetEnvironmentInfo() ([]Environment, error) {

func (conf *client) configureGetEnvironmentInfo() {
scheduleGetEnvironmentInfo := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetEnvironmentInfo.AddFunc(conf.apiCron, func() {
_, err := scheduleGetEnvironmentInfo.AddFunc(conf.getCron(common.MetricEnvironmentCountAll), func() {
environmentInfo, err := conf.GetEnvironmentInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("environment", err.Error())) //nolint:errcheck
Expand Down
48 changes: 35 additions & 13 deletions pkg/gocd/gocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ package gocd
import (
"crypto/tls"
"crypto/x509"
"fmt"
"sync"
"time"

"github.com/go-kit/log/level"
"github.com/nikhilsbhat/gocd-prometheus-exporter/pkg/common"

"github.com/go-kit/log"
"github.com/go-resty/resty/v2"
)

// client holds resty.Client which could be used for interacting with GoCD and other information.
type client struct {
client *resty.Client
logger log.Logger
apiCron string
diskCron string
lock sync.RWMutex
paths []string
client *resty.Client
logger log.Logger
defaultAPICron string
diskCron string
metricSpecificCron map[string]string
lock sync.RWMutex
paths []string
}

// GoCd implements methods to get various information regarding GoCD.
Expand All @@ -36,7 +41,12 @@ type GoCd interface {
}

// NewClient returns new instance of client when invoked.
func NewClient(baseURL, userName, passWord, loglevel, cron, diskCron string, caContent []byte, path []string, logger log.Logger) GoCd {
func NewClient(baseURL, userName, passWord, loglevel, defaultAPICron, diskCron string,
metricSpecificCron map[string]string,
caContent []byte,
path []string,
logger log.Logger,
) GoCd {
newClient := resty.New()
newClient.SetRetryCount(defaultRetryCount)
newClient.SetRetryWaitTime(defaultRetryWaitTime * time.Second)
Expand All @@ -54,11 +64,23 @@ func NewClient(baseURL, userName, passWord, loglevel, cron, diskCron string, caC
}

return &client{
client: newClient,
logger: logger,
lock: sync.RWMutex{},
apiCron: cron,
diskCron: diskCron,
paths: path,
client: newClient,
logger: logger,
lock: sync.RWMutex{},
defaultAPICron: defaultAPICron,
diskCron: diskCron,
metricSpecificCron: metricSpecificCron,
paths: path,
}
}

func (conf *client) getCron(metric string) string {
if val, ok := conf.metricSpecificCron[metric]; ok {
level.Debug(conf.logger).Log(common.LogCategoryMsg, fmt.Sprintf("the cron for metric %s would be %s", metric, val)) //nolint:errcheck

return val
}
level.Debug(conf.logger).Log(common.LogCategoryMsg, fmt.Sprintf("metric %s would be using default cron", metric)) //nolint:errcheck

return conf.defaultAPICron
}
2 changes: 1 addition & 1 deletion pkg/gocd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (conf *client) GetHealthInfo() ([]ServerHealth, error) {

func (conf *client) configureGetHealthInfo() {
scheduleGetHealthInfo := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetHealthInfo.AddFunc(conf.apiCron, func() {
_, err := scheduleGetHealthInfo.AddFunc(conf.getCron(common.MetricServerHealth), func() {
healthInfo, err := conf.GetHealthInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("server health", err.Error())) //nolint:errcheck
Expand Down
1 change: 1 addition & 0 deletions pkg/gocd/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestConfig_GetHealthInfo(t *testing.T) {
"",
nil,
nil,
nil,
logger,
)

Expand Down
5 changes: 1 addition & 4 deletions pkg/gocd/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
"github.com/go-kit/log/level"
)

func (conf *client) GetPipelineInfo() {
}

// GetPipelineGroupInfo fetches information of backup configured in GoCD server.
func (conf *client) GetPipelineGroupInfo() ([]PipelineGroup, error) {
conf.lock.Lock()
Expand Down Expand Up @@ -56,7 +53,7 @@ func (conf *client) getPipelineCount(groups []PipelineGroup) int {

func (conf *client) configureGetPipelineGroupInfo() {
scheduleGetPipelineGroupInfo := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetPipelineGroupInfo.AddFunc(conf.apiCron, func() {
_, err := scheduleGetPipelineGroupInfo.AddFunc(conf.getCron(common.MetricPipelineGroupCount), func() {
pipelineInfo, err := conf.GetPipelineGroupInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("pipeline group", err.Error())) //nolint:errcheck
Expand Down
4 changes: 2 additions & 2 deletions pkg/gocd/schedulers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

// CronSchedulers schedules all the jobs so that data will be available for the exporter to serve.
func (conf *client) CronSchedulers() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronMessages("api", conf.apiCron)) //nolint:errcheck
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronMessages("disk", conf.diskCron)) //nolint:errcheck
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronMessages("api", conf.defaultAPICron)) //nolint:errcheck
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronMessages("disk", conf.diskCron)) //nolint:errcheck
conf.configureDiskUsage()
conf.configureAdminsInfo()
conf.configureGetConfigRepo()
Expand Down
2 changes: 1 addition & 1 deletion pkg/gocd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (conf *client) GetVersionInfo() (VersionInfo, error) {

func (conf *client) configureGetVersionInfo() {
scheduleGetVersionInfo := cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))
_, err := scheduleGetVersionInfo.AddFunc(conf.apiCron, func() {
_, err := scheduleGetVersionInfo.AddFunc(conf.getCron(common.MetricVersion), func() {
version, err := conf.GetVersionInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("version", err.Error())) //nolint:errcheck
Expand Down
1 change: 1 addition & 0 deletions pkg/gocd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Test_config_GetVersionInfo(t *testing.T) {
"",
nil,
nil,
nil,
logger,
)
actual, err := client.GetVersionInfo()
Expand Down

0 comments on commit f61a2ce

Please sign in to comment.