diff --git a/pkg/app/app.go b/pkg/app/app.go index e125960..51894a6 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -26,7 +26,7 @@ type App struct { VaultClient *vault.Client TFCloudToken string EnableMetrics bool - Errors *Errors + Metrics *Metrics } // Config represents the configuration file @@ -120,7 +120,7 @@ func (a *App) Run() error { }() if a.EnableMetrics { - a.registerErrors() + a.registerMetrics() http.Handle("/metrics", promhttp.Handler()) go http.ListenAndServe(":4329", nil) } @@ -155,19 +155,23 @@ func (a *App) updateCircleCIInstance(project CircleCIConfig, wg *sync.WaitGroup) projVariableName := a.Config.TokenVariable token, err := a.VaultClient.CreateToken(project.VaultRole, project.VaultPolicies, a.Config.TokenTTL, a.Config.OrphanTokens) if err != nil { - klog.Errorf("error making token for CircleCI project %s: %s", projName, err.Error()) + a.incrementVaultError() + klog.Errorf("error making token for CircleCI project %s: %w", projName, err) return } klog.V(10).Infof("got token %s for CircleCI project %s", token.Auth.ClientToken, projName) klog.Infof("setting env var %s to vault token value in CircleCI project %s", projVariableName, projName) if err := circleci.UpdateEnvVar(projName, projVariableName, token.Auth.ClientToken, a.CircleToken); err != nil { - klog.Errorf("error updating CircleCI project %s with token value: %s", projName, err.Error) + a.incrementCircleCIError() + klog.Errorf("error updating CircleCI project %s with token value: %w", projName, err) return } if err := circleci.UpdateEnvVar(projName, "VAULT_ADDR", a.Config.VaultAddress, a.CircleToken); err != nil { - klog.Errorf("error updating VAULT_ADDR in CircleCI project %s: %s", projName, err) + a.incrementCircleCIError() + klog.Errorf("error updating VAULT_ADDR in CircleCI project %s: %w", projName, err) return } + a.Metrics.circleTokensUpdated.Inc() } func (a *App) updateTFCloudInstance(instance TFCloudConfig, wg *sync.WaitGroup) { @@ -180,8 +184,8 @@ func (a *App) updateTFCloudInstance(instance TFCloudConfig, wg *sync.WaitGroup) } token, err := a.VaultClient.CreateToken(instance.VaultRole, instance.VaultPolicies, a.Config.TokenTTL, a.Config.OrphanTokens) if err != nil { - klog.Errorf("error getting vault token for TFCloud workspace %s: %s", workspaceLogIdentifier, err.Error()) a.incrementVaultError() + klog.Errorf("error getting vault token for TFCloud workspace %s: %w", workspaceLogIdentifier, err) return } klog.V(10).Infof("got token %v for tfcloud workspace %s", token.Auth.ClientToken, workspaceLogIdentifier) @@ -194,8 +198,8 @@ func (a *App) updateTFCloudInstance(instance TFCloudConfig, wg *sync.WaitGroup) Workspace: instance.Workspace, } if err := tokenVar.Update(); err != nil { - klog.Errorf("error updating token for TFCloud workspace %s: %s", workspaceLogIdentifier, err.Error()) - + a.incrementTfCloudError() + klog.Errorf("error updating token for TFCloud workspace %s: %w", workspaceLogIdentifier, err) return } addressVar := tfcloud.Variable{ @@ -207,9 +211,11 @@ func (a *App) updateTFCloudInstance(instance TFCloudConfig, wg *sync.WaitGroup) WorkspaceIdentifier: workspaceLogIdentifier, } if err := addressVar.Update(); err != nil { - klog.Errorf("error updating VAULT_ADDR for ws %s: %s", workspaceLogIdentifier, err.Error()) + a.incrementTfCloudError() + klog.Errorf("error updating VAULT_ADDR for ws %s: %w", workspaceLogIdentifier, err) return } + a.Metrics.tfcloudTokensUpdated.Inc() } func (a *App) refreshVaultToken() error { diff --git a/pkg/app/errors.go b/pkg/app/errors.go index e8291ab..f327a4d 100644 --- a/pkg/app/errors.go +++ b/pkg/app/errors.go @@ -5,7 +5,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promauto" ) -type Errors struct { +type Metrics struct { totalErrorCount prometheus.Counter vaultErrorCount prometheus.Counter circleCIErrorCount prometheus.Counter @@ -14,8 +14,8 @@ type Errors struct { tfcloudTokensUpdated prometheus.Counter } -func (a *App) registerErrors() { - a.Errors = &Errors{ +func (a *App) registerMetrics() { + a.Metrics = &Metrics{ totalErrorCount: promauto.NewCounter(prometheus.CounterOpts{ Name: "vault_token_injector_errors_total", Help: "The number of errors encountered", @@ -44,16 +44,16 @@ func (a *App) registerErrors() { } func (a App) incrementVaultError() { - a.Errors.vaultErrorCount.Inc() - a.Errors.totalErrorCount.Inc() + a.Metrics.vaultErrorCount.Inc() + a.Metrics.totalErrorCount.Inc() } func (a App) incrementTfCloudError() { - a.Errors.tfCloudErrorCount.Inc() - a.Errors.totalErrorCount.Inc() + a.Metrics.tfCloudErrorCount.Inc() + a.Metrics.totalErrorCount.Inc() } -func (a App) incremenCircleCIError() { - a.Errors.circleCIErrorCount.Inc() - a.Errors.totalErrorCount.Inc() +func (a App) incrementCircleCIError() { + a.Metrics.circleCIErrorCount.Inc() + a.Metrics.totalErrorCount.Inc() }