Skip to content

Commit

Permalink
Increment errors (#81)
Browse files Browse the repository at this point in the history
* Increment errors

* increment counters for tokens updated

* fix nit
  • Loading branch information
sudermanjr authored Feb 14, 2023
1 parent 61cf5d4 commit d25af3e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
24 changes: 15 additions & 9 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type App struct {
VaultClient *vault.Client
TFCloudToken string
EnableMetrics bool
Errors *Errors
Metrics *Metrics
}

// Config represents the configuration file
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions pkg/app/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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()
}

0 comments on commit d25af3e

Please sign in to comment.