Skip to content

Commit

Permalink
Fix config drop passed from configuration file
Browse files Browse the repository at this point in the history
 - add info logs when cron gets scheduled
 - fix yaml tags for DiskCron
 - update gocd-prometheus-exporter.sample.yaml with more values
  • Loading branch information
nikhilsbhat committed Aug 3, 2022
1 parent 7c58140 commit b612c23
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 8 deletions.
14 changes: 12 additions & 2 deletions gocd-prometheus-exporter.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
---
gocd-username: "admin"
gocd-password: "admin"
log-level: info
port: 9995
api-cron-schedule: "@every 20s"
disk-cron-schedule: "@every 20s"
metric-cron:
agent_down: "@every 60s"
metric-endpoint: "/new-metrics"
skip-metrics:
- "backup_configured"
- "admin_count"
gocd-pipelines-path:
- /path/to/pipeline/directory1
- /path/to/pipeline/directory2
- /path/to/pipeline/directory3
metric-cron:
agent_down: "@every 60s"
16 changes: 16 additions & 0 deletions infrastructure/gocd-prometheus-exporter.fixture.malformed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
gocd-username: "testing"
gocd-password: "password"
gocd-pipelines-path:
- /path/to/pipeline/directory1
- /path/to/pipeline/directory2
- /path/to/pipeline/directory3
log-level: debug
port: 9995
api-cron-schedule: "@every @2m"
disk-cron-schedule: "@every @2m"
metric-cron
agent_down: "@every 60s"
skip-metrics:
- "backup_configured"
- "admin_count"
17 changes: 17 additions & 0 deletions infrastructure/gocd-prometheus-exporter.fixture.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
gocd-username: "testing"
gocd-password: "password"
gocd-pipelines-path:
- /path/to/pipeline/directory1
- /path/to/pipeline/directory2
- /path/to/pipeline/directory3
log-level: debug
port: 9995
api-cron-schedule: "@every @2m"
disk-cron-schedule: "@every @2m"
metric-cron:
agent_down: "@every 60s"
skip-metrics:
- "backup_configured"
- "admin_count"
metric-endpoint: "/new-metrics"
4 changes: 2 additions & 2 deletions pkg/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Config struct {
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"`
DiskCron string `json:"disk-cron-schedule,omitempty" yaml:"disk-cron-schedule,omitempty"`
MetricCron map[string]string `json:"metric-cron,omitempty" yaml:"metric-cron,omitempty"`
AppGraceDuration time.Duration `json:"grace-duration,omitempty" yaml:"grace-duration,omitempty"`
}
Expand All @@ -52,7 +52,7 @@ func GetConfig(conf Config, path string) (*Config, error) {

return &conf, fmt.Errorf("parsing config file errored with: %w", err)
}
if err = mergo.Merge(&newConfig, &conf, mergo.WithOverride); err != nil {
if err = mergo.Merge(&newConfig, &conf); err != nil {
log.Println("failed to merge configurations, dropping configurations from file")

return &conf, fmt.Errorf("merging config errored with: %w", err)
Expand Down
58 changes: 58 additions & 0 deletions pkg/app/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package app_test

import (
"path/filepath"
"testing"

"github.com/nikhilsbhat/gocd-prometheus-exporter/pkg/app"
"github.com/stretchr/testify/assert"
)

func TestGetConfig(t *testing.T) {
config := app.Config{
Port: 8090,
GoCdUserName: "test",
GoCdPassword: "password",
DiskCron: "@every 1m",
APICron: "@every 1m",
Endpoint: "/metrics",
}
t.Run("Should fetch config successfully, by merging both config", func(t *testing.T) {
expected := app.Config{
GoCdUserName: "testing",
GoCdPassword: "password",
InsecureTLS: false,
GoCdPipelinesPath: []string{"/path/to/pipeline/directory1", "/path/to/pipeline/directory2", "/path/to/pipeline/directory3"},
Port: 9995,
LogLevel: "debug",
SkipMetrics: []string{"backup_configured", "admin_count"},
APICron: "@every @2m",
DiskCron: "@every @2m",
MetricCron: map[string]string{"agent_down": "@every 60s"},
Endpoint: "/new-metrics",
}

path, err := filepath.Abs("../../infrastructure/gocd-prometheus-exporter.fixture.yaml")
assert.NoError(t, err)

actual, err := app.GetConfig(config, path)
assert.NoError(t, err)
assert.Equal(t, &expected, actual)
})

t.Run("Should error out while fetching config due to wrong config file path", func(t *testing.T) {
actual, err := app.GetConfig(config, "gocd-prometheus-exporter.fixture.yaml")
assert.EqualError(t, err, "fetching config file information failed with: "+
"stat gocd-prometheus-exporter.fixture.yaml: no such file or directory")
assert.Equal(t, actual, &config)
})

t.Run("Should error out while decoding config from malformed config file", func(t *testing.T) {
path, err := filepath.Abs("../../infrastructure/gocd-prometheus-exporter.fixture.malformed.yaml")
assert.NoError(t, err)

actual, err := app.GetConfig(config, path)
assert.EqualError(t, err, "parsing config file errored with: yaml: line 13: could not find expected ':'")
assert.Equal(t, actual, &config)
})
}
2 changes: 2 additions & 0 deletions pkg/gocd/admins.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ 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.getCron(common.MetricSystemAdminsCount), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricSystemAdminsCount)) //nolint:errcheck

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: 4 additions & 0 deletions pkg/gocd/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ 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.getCron(common.MetricAgentDown), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricAgentDown)) //nolint:errcheck

agentsInfo, err := conf.GetAgentsInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("agents", err.Error())) //nolint:errcheck
Expand All @@ -86,6 +88,8 @@ 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.getCron(common.MetricJobStatus), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricJobStatus)) //nolint:errcheck

agentsJobRunHistory, err := conf.GetAgentJobRunHistory()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("agents", err.Error())) //nolint:errcheck
Expand Down
2 changes: 2 additions & 0 deletions pkg/gocd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ 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.getCron(common.MetricConfiguredBackup), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricConfiguredBackup)) //nolint:errcheck

backupInfo, err := conf.GetBackupInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("gocd backup", err.Error())) //nolint:errcheck
Expand Down
2 changes: 2 additions & 0 deletions pkg/gocd/configrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ 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.getCron(common.MetricConfigRepoCount), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricConfigRepoCount)) //nolint:errcheck

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/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (conf *client) configureDiskUsage() {

func (conf *client) getAndUpdateDiskSize() {
conf.lock.Lock()
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage("pipeline size")) //nolint:errcheck
for _, path := range conf.paths {
level.Debug(conf.logger).Log(common.LogCategoryMsg, fmt.Sprintf("pipeline present at %s would be scanned", path)) //nolint:errcheck
size, pathType, err := conf.GetDiskSize(path)
Expand Down
2 changes: 2 additions & 0 deletions pkg/gocd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ 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.getCron(common.MetricEnvironmentCountAll), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricEnvironmentCountAll)) //nolint:errcheck

environmentInfo, err := conf.GetEnvironmentInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("environment", err.Error())) //nolint:errcheck
Expand Down
2 changes: 2 additions & 0 deletions pkg/gocd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ 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.getCron(common.MetricServerHealth), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricServerHealth)) //nolint:errcheck

healthInfo, err := conf.GetHealthInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("server health", err.Error())) //nolint:errcheck
Expand Down
2 changes: 2 additions & 0 deletions pkg/gocd/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ 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.getCron(common.MetricPipelineGroupCount), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricPipelineGroupCount)) //nolint:errcheck

pipelineInfo, err := conf.GetPipelineGroupInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("pipeline group", err.Error())) //nolint:errcheck
Expand Down
13 changes: 9 additions & 4 deletions pkg/gocd/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
)

var (
goCdTryMessage = `trying to retrieve %s information present in GoCD`
goCdSuccessMessage = `successfully retrieved information of %s from GoCD`
cronMessage = `%s cron will be scheduled for %s as specified`
symlinkMessage = `path %s is link to %s so fetching size of destination`
goCdTryMessage = `trying to retrieve %s information present in GoCD`
goCdSuccessMessage = `successfully retrieved information of %s from GoCD`
cronMessage = `%s cron will be scheduled for %s as specified`
cronScheduledMessage = `%s cron got scheduled`
symlinkMessage = `path %s is link to %s so fetching size of destination`
)

func apiError(component, errMsg string) error {
Expand All @@ -35,6 +36,10 @@ func getCronMessages(component, schedule string) string {
return fmt.Sprintf(cronMessage, component, schedule)
}

func getCronScheduledMessage(component string) string {
return fmt.Sprintf(cronScheduledMessage, component)
}

func getLinkMessage(link, path string) string {
return fmt.Sprintf(symlinkMessage, link, path)
}
2 changes: 2 additions & 0 deletions pkg/gocd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ 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.getCron(common.MetricVersion), func() {
level.Info(conf.logger).Log(common.LogCategoryMsg, getCronScheduledMessage(common.MetricVersion)) //nolint:errcheck

version, err := conf.GetVersionInfo()
if err != nil {
level.Error(conf.logger).Log(common.LogCategoryErr, apiError("version", err.Error())) //nolint:errcheck
Expand Down

0 comments on commit b612c23

Please sign in to comment.