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 permet pool metrics to dynamo db backend #21742

Merged
merged 6 commits into from
Aug 18, 2023
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
3 changes: 3 additions & 0 deletions changelog/21742.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
storage/dynamodb: Added three permit pool metrics for the DynamoDB backend, `pending_permits`, `active_permits`, and `pool_size`.
```
47 changes: 45 additions & 2 deletions physical/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

log "github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -89,7 +90,7 @@ type DynamoDBBackend struct {
client *dynamodb.DynamoDB
logger log.Logger
haEnabled bool
permitPool *physical.PermitPool
permitPool *PermitPoolWithMetrics
}

// DynamoDBRecord is the representation of a vault entry in
Expand Down Expand Up @@ -122,6 +123,12 @@ type DynamoDBLockRecord struct {
Expires int64
}

type PermitPoolWithMetrics struct {
physical.PermitPool
pendingPermits int32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: run git fmt. It should align these types with spaces

poolSize int
}

// NewDynamoDBBackend constructs a DynamoDB backend. If the
// configured DynamoDB table does not exist, it creates it.
func NewDynamoDBBackend(conf map[string]string, logger log.Logger) (physical.Backend, error) {
Expand Down Expand Up @@ -248,7 +255,7 @@ func NewDynamoDBBackend(conf map[string]string, logger log.Logger) (physical.Bac
return &DynamoDBBackend{
table: table,
client: client,
permitPool: physical.NewPermitPool(maxParInt),
permitPool: NewPermitPoolWithMetrics(maxParInt),
haEnabled: haEnabledBool,
logger: logger,
}, nil
Expand Down Expand Up @@ -909,3 +916,39 @@ func isConditionCheckFailed(err error) bool {

return false
}

// NewPermitPoolWithMetrics returns a new permit pool with the provided
// number of permits which emits metrics
func NewPermitPoolWithMetrics(permits int) *PermitPoolWithMetrics {
return &PermitPoolWithMetrics{
PermitPool: *physical.NewPermitPool(permits),
pendingPermits: 0,
poolSize: permits,
}
}

// Acquire returns when a permit has been acquired
func (c *PermitPoolWithMetrics) Acquire() {
atomic.AddInt32(&c.pendingPermits, 1)
c.emitPermitMetrics()
c.PermitPool.Acquire()
atomic.AddInt32(&c.pendingPermits, -1)
c.emitPermitMetrics()
}

// Release returns a permit to the pool
func (c *PermitPoolWithMetrics) Release() {
c.PermitPool.Release()
c.emitPermitMetrics()
}

// Get the number of requests in the permit pool
func (c *PermitPoolWithMetrics) CurrentPermits() int {
return c.PermitPool.CurrentPermits()
}

func (c *PermitPoolWithMetrics) emitPermitMetrics() {
metrics.SetGauge([]string{"dynamodb", "permit_pool", "pending_permits"}, float32(c.pendingPermits))
metrics.SetGauge([]string{"dynamodb", "permit_pool", "active_permits"}, float32(c.PermitPool.CurrentPermits()))
metrics.SetGauge([]string{"dynamodb", "permit_pool", "pool_size"}, float32(c.poolSize))
}