Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry for LRU cache #10079

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions sdk/physical/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"sync/atomic"

metrics "github.com/armon/go-metrics"
mgritter marked this conversation as resolved.
Show resolved Hide resolved
log "github.com/hashicorp/go-hclog"
lru "github.com/hashicorp/golang-lru"
"github.com/hashicorp/vault/sdk/helper/locksutil"
Expand Down Expand Up @@ -57,6 +58,7 @@ type Cache struct {
logger log.Logger
enabled *uint32
cacheExceptions *pathmanager.PathManager
metricSink metrics.MetricSink
}

// TransactionalCache is a Cache that wraps the physical that is transactional
Expand All @@ -73,7 +75,7 @@ var _ Transactional = (*TransactionalCache)(nil)

// NewCache returns a physical cache of the given size.
// If no size is provided, the default size is used.
func NewCache(b Backend, size int, logger log.Logger) *Cache {
func NewCache(b Backend, size int, logger log.Logger, metricSink metrics.MetricSink) *Cache {
if logger.IsDebug() {
logger.Debug("creating LRU cache", "size", size)
}
Expand All @@ -93,13 +95,14 @@ func NewCache(b Backend, size int, logger log.Logger) *Cache {
// This fails safe.
enabled: new(uint32),
cacheExceptions: pm,
metricSink: metricSink,
}
return c
}

func NewTransactionalCache(b Backend, size int, logger log.Logger) *TransactionalCache {
func NewTransactionalCache(b Backend, size int, logger log.Logger, metricSink metrics.MetricSink) *TransactionalCache {
c := &TransactionalCache{
Cache: NewCache(b, size, logger),
Cache: NewCache(b, size, logger, metricSink),
Transactional: b.(Transactional),
}
return c
Expand Down Expand Up @@ -146,6 +149,7 @@ func (c *Cache) Put(ctx context.Context, entry *Entry) error {
err := c.backend.Put(ctx, entry)
if err == nil {
c.lru.Add(entry.Key, entry)
c.metricSink.IncrCounter([]string{"cache", "write"}, 1)
}
return err
}
Expand All @@ -165,10 +169,12 @@ func (c *Cache) Get(ctx context.Context, key string) (*Entry, error) {
if raw == nil {
return nil, nil
}
c.metricSink.IncrCounter([]string{"cache", "hit"}, 1)
return raw.(*Entry), nil
}
}

c.metricSink.IncrCounter([]string{"cache", "miss"}, 1)
// Read from the underlying backend
ent, err := c.backend.Get(ctx, key)
if err != nil {
Expand Down Expand Up @@ -241,8 +247,10 @@ func (c *TransactionalCache) Transaction(ctx context.Context, txns []*TxnEntry)
switch txn.Operation {
case PutOperation:
c.lru.Add(txn.Entry.Key, txn.Entry)
c.metricSink.IncrCounter([]string{"cache", "write"}, 1)
case DeleteOperation:
c.lru.Remove(txn.Entry.Key)
c.metricSink.IncrCounter([]string{"cache", "delete"}, 1)
}
}

Expand Down
10 changes: 6 additions & 4 deletions sdk/physical/inmem/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/physical"
Expand All @@ -16,7 +17,8 @@ func TestCache(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cache := physical.NewCache(inm, 0, logger)

cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})
cache.SetEnabled(true)
physical.ExerciseBackend(t, cache)
physical.ExerciseBackend_ListPrefix(t, cache)
Expand All @@ -29,7 +31,7 @@ func TestCache_Purge(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cache := physical.NewCache(inm, 0, logger)
cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})
cache.SetEnabled(true)

ent := &physical.Entry{
Expand Down Expand Up @@ -76,7 +78,7 @@ func TestCache_Disable(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cache := physical.NewCache(inm, 0, logger)
cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})

disabledTests := func() {
ent := &physical.Entry{
Expand Down Expand Up @@ -276,7 +278,7 @@ func TestCache_Refresh(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cache := physical.NewCache(inm, 0, logger)
cache := physical.NewCache(inm, 0, logger, &metrics.BlackholeSink{})
cache.SetEnabled(true)

ent := &physical.Entry{
Expand Down
4 changes: 2 additions & 2 deletions vault/core_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func coreInit(c *Core, conf *CoreConfig) error {
cacheLogger := c.baseLogger.Named("storage.cache")
c.allLoggers = append(c.allLoggers, cacheLogger)
if txnOK {
c.physical = physical.NewTransactionalCache(c.sealUnwrapper, conf.CacheSize, cacheLogger)
c.physical = physical.NewTransactionalCache(c.sealUnwrapper, conf.CacheSize, cacheLogger, c.MetricSink().Sink)
} else {
c.physical = physical.NewCache(c.sealUnwrapper, conf.CacheSize, cacheLogger)
c.physical = physical.NewCache(c.sealUnwrapper, conf.CacheSize, cacheLogger, c.MetricSink().Sink)
}
c.physicalCache = c.physical.(physical.ToggleablePurgemonster)

Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/hashicorp/vault/api/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions vendor/github.com/hashicorp/vault/sdk/physical/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions website/pages/docs/internals/telemetry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ These metrics represent operational aspects of the running Vault instance.
| `vault.metrics.collection.error` (cluster,gauge) | Errors while collection usage guages, labeled by gauge type. | counter |
| `vault.rollback.attempt.<mountpoint>` | Time taken to perform a rollback operation on the given mount point. The mount point name has its forward slashes `/` replaced by `-`. For example, a rollback operation on the `auth/token` backend would be reportes as `vault.rollback.attempt.auth-token-`. | ms | summary |
| `vault.route.create.<mountpoint>` | Time taken to dispatch a create operation to a backend, and for that backend to process it. The mount point name has its forward slashes `/` replaced by `-`. For example, a create operation to `ns1/secret/` would have corresponding metric `vault.route.create.ns1-secret-`. The number of samples of this metric, and the corresponding ones for other operations below, indicates how many operations were performed per mount point. | ms | summary |
| `vault.route.delete.<mountpoint>` | Time taken to dispatch a delete operation to a backend, and for that backend to process it. | ms | summary |
| `vault.route.list.<mountpoint>` | Time taken to dispatch a list operation to a backend, and for that backend to process it. | ms | summary |
| `vault.route.read.<mountpoint>` | Time taken to dispatch a read operation to a backend, and for that backend to process it. | ms | summary |
| `vault.route.rollback.<mountpoint>` | Time taken to dispatch a rollback operation to a backend, and for that backend to process it. Rollback operations are automatically scheduled to clean up partial errors. | ms | summary |
| `vault.route.delete.<mountpoint>` | Time taken to dispatch a delete operation to a backend, and for that backend to process it. | ms | summary |
| `vault.route.list.<mountpoint>` | Time taken to dispatch a list operation to a backend, and for that backend to process it. | ms | summary |
| `vault.route.read.<mountpoint>` | Time taken to dispatch a read operation to a backend, and for that backend to process it. | ms | summary |
| `vault.route.rollback.<mountpoint>` | Time taken to dispatch a rollback operation to a backend, and for that backend to process it. Rollback operations are automatically scheduled to clean up partial errors. | ms | summary |
| `vault.cache.hit` | Number of times a value was retrieved from the LRU cache. | cache hit | counter |
| `vault.cache.miss` | Number of times a value was not in the LRU cache. The results in a read from the configured storage. | cache miss | counter |
| `vault.cache.write` | Number of times a value was written to the LRU cache. | cache write | counter |
| `vault.cache.delete` | Number of times a value was deleted from the LRU cache. This does not count cache expirations. | cache delete | counter |

## Runtime Metrics

Expand Down