Skip to content

Commit

Permalink
Add counter to vault token renewal
Browse files Browse the repository at this point in the history
  • Loading branch information
findkim committed May 5, 2020
1 parent c8b3706 commit 055f3c4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,7 @@ to provide insight on performance of the templates.
| `consul-template.runner_actions` | action=(start\|stop\|run) | A count of runner actions |
| `consul-template.commands_exec` | status=(success\|error) | The number of commands executed after rendering templates |
| `consul-template.commands_exec_time` | id=tmplDestination | The execution time (seconds) of a template command |
| `consul-template.vault.token` | status=(configured\|renewed\|expired\|stopped) | A counter of vault token renewal statuses |
### Metric Samples
Expand Down
9 changes: 9 additions & 0 deletions dependency/vault_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ type renewer interface {
Dependency
stopChan() chan struct{}
secrets() (*Secret, *api.Secret)

// recordCounter is an abstraction from metrics reporting for a renewer
recordCounter(string, string)
}

func renewSecret(clients *ClientSet, d renewer) error {
Expand All @@ -93,14 +96,20 @@ func renewSecret(clients *ClientSet, d renewer) error {
case err := <-renewer.DoneCh():
if err != nil {
log.Printf("[WARN] %s: failed to renew: %s", d, err)
d.recordCounter("status", "stopped")
} else {
d.recordCounter("status", "expired")
}

log.Printf("[WARN] %s: renewer done (maybe the lease expired)", d)
return nil
case renewal := <-renewer.RenewCh():
log.Printf("[TRACE] %s: successfully renewed", d)
printVaultWarnings(d, renewal.Secret.Warnings)
updateSecret(secret, renewal.Secret)
d.recordCounter("status", "renewed")
case <-d.stopChan():
d.recordCounter("status", "stopped")
return ErrStopped
}
}
Expand Down
2 changes: 2 additions & 0 deletions dependency/vault_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func (d *VaultReadQuery) readSecret(clients *ClientSet, opts *QueryOptions) (*ap
return vaultSecret, nil
}

func (d *VaultReadQuery) recordCounter(key, value string) {}

func deletedKVv2(s *api.Secret) bool {
switch md := s.Data["metadata"].(type) {
case map[string]interface{}:
Expand Down
33 changes: 29 additions & 4 deletions dependency/vault_token.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package dependency

import (
"context"

"github.com/hashicorp/consul-template/telemetry"
"github.com/hashicorp/vault/api"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/api/metric"
)

var (
Expand All @@ -15,6 +19,9 @@ type VaultTokenQuery struct {
stopCh chan struct{}
secret *Secret
vaultSecret *api.Secret

// counterRenew is a counter to monitor the renewal status of the vault token.
counterRenew metric.Int64Counter
}

// NewVaultTokenQuery creates a new dependency.
Expand All @@ -26,10 +33,22 @@ func NewVaultTokenQuery(token string) (*VaultTokenQuery, error) {
LeaseDuration: 1,
},
}

meter := telemetry.GlobalMeter()
counter, err := meter.NewInt64Counter("consul-template.vault.token",
metric.WithDescription("A counter of vault token renewal statuses"+
"with label status=(configured|renewed|expired|stopped)"))
if err != nil {
return nil, err
}

counter.Add(context.Background(), 1, telemetry.NewLabel("status", "configured"))

return &VaultTokenQuery{
stopCh: make(chan struct{}, 1),
vaultSecret: vaultSecret,
secret: transformSecret(vaultSecret),
stopCh: make(chan struct{}, 1),
vaultSecret: vaultSecret,
secret: transformSecret(vaultSecret),
counterRenew: counter,
}, nil
}

Expand All @@ -47,7 +66,6 @@ func (d *VaultTokenQuery) Fetch(clients *ClientSet, opts *QueryOptions,
if err != nil {
return nil, nil, errors.Wrap(err, d.String())
}
renewSecret(clients, d)
}

return nil, nil, ErrLeaseExpired
Expand Down Expand Up @@ -80,3 +98,10 @@ func (d *VaultTokenQuery) String() string {
func (d *VaultTokenQuery) Type() Type {
return TypeVault
}

// recordCounter increments a counter for the vault dependency with a
// set of key value label
func (d *VaultTokenQuery) recordCounter(key, value string) {
ctx := context.Background()
d.counterRenew.Add(ctx, 1, telemetry.NewLabel(key, value))
}
2 changes: 2 additions & 0 deletions dependency/vault_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,5 @@ func (d *VaultWriteQuery) writeSecret(clients *ClientSet, opts *QueryOptions) (*

return vaultSecret, nil
}

func (d *VaultWriteQuery) recordCounter(key, value string) {}

0 comments on commit 055f3c4

Please sign in to comment.