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

[SPIRE Agent] add telemetry around LRU cache entry operations #4335

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move lru metrics to agent package and wrap metrics
Signed-off-by: gordonhu7 <hu.gordon@hotmail.com>
  • Loading branch information
gordonhu7 committed Jul 12, 2023
commit 1a7e20c32341f7eb09b16debed159b67aa8caeba
15 changes: 8 additions & 7 deletions pkg/agent/manager/cache/lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/spire/pkg/agent/common/backoff"
"github.com/spiffe/spire/pkg/common/telemetry"
"github.com/spiffe/spire/pkg/common/telemetry/agent"
"github.com/spiffe/spire/proto/spire/common"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ type LRUCache struct {
trustDomain spiffeid.TrustDomain
clk clock.Clock

metrics *telemetry.LRUMetrics
metrics telemetry.Metrics

mu sync.RWMutex

Expand Down Expand Up @@ -118,7 +119,7 @@ func NewLRUCache(log logrus.FieldLogger, trustDomain spiffeid.TrustDomain, bundl
JWTSVIDCache: NewJWTSVIDCache(),

log: log,
metrics: telemetry.NewLRUMetrics(&telemetry.LRUConfig{MetricsImpl: metrics}),
metrics: metrics,
trustDomain: trustDomain,
records: make(map[string]*lruCacheRecord),
selectors: make(map[selector]*selectorsMapIndex),
Expand Down Expand Up @@ -221,7 +222,7 @@ func (c *LRUCache) NewSubscriber(selectors []*common.Selector) Subscriber {
// updated through a call to UpdateSVIDs.
func (c *LRUCache) UpdateEntries(update *UpdateEntries, checkSVID func(*common.RegistrationEntry, *common.RegistrationEntry, *X509SVID) bool) {
c.mu.Lock()
defer func() { c.metrics.SetEntriesMapSize(c.CountRecords()) }()
defer func() { agent.SetEntriesMapSize(c.metrics, c.CountRecords()) }()
defer c.mu.Unlock()

// Remove bundles that no longer exist. The bundle for the agent trust
Expand Down Expand Up @@ -293,7 +294,7 @@ func (c *LRUCache) UpdateEntries(update *UpdateEntries, checkSVID func(*common.R
delete(c.staleEntries, id)
}
}
c.metrics.IncrementEntriesRemoved(entriesRemoved)
agent.IncrementEntriesRemoved(c.metrics, entriesRemoved)
hugordon7 marked this conversation as resolved.
Show resolved Hide resolved

outdatedEntries := make(map[string]struct{})
entriesUpdated := 0
Expand Down Expand Up @@ -386,8 +387,8 @@ func (c *LRUCache) UpdateEntries(update *UpdateEntries, checkSVID func(*common.R
}
}
}
c.metrics.IncrementEntriesAdded(entriesCreated)
c.metrics.IncrementEntriesUpdated(entriesUpdated)
agent.IncrementEntriesAdded(c.metrics, entriesCreated)
agent.IncrementEntriesUpdated(c.metrics, entriesUpdated)

// entries with active subscribers which are not cached will be put in staleEntries map;
// irrespective of what svid cache size as we cannot deny identity to a subscriber
Expand Down Expand Up @@ -439,7 +440,7 @@ func (c *LRUCache) UpdateEntries(update *UpdateEntries, checkSVID func(*common.R

func (c *LRUCache) UpdateSVIDs(update *UpdateSVIDs) {
c.mu.Lock()
defer func() { c.metrics.SetSVIDMapSize(c.CountSVIDs()) }()
defer func() { agent.SetSVIDMapSize(c.metrics, c.CountSVIDs()) }()
defer c.mu.Unlock()

// Allocate a set of selectors that
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/manager/cache/lru_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ func TestSubscribeToLRUCacheChanges(t *testing.T) {
func TestMetrics(t *testing.T) {
cache := newTestLRUCache(t)
fakeMetrics := fakemetrics.New()
cache.metrics = telemetry.NewLRUMetrics(&telemetry.LRUConfig{MetricsImpl: fakeMetrics})
cache.metrics = fakeMetrics

foo := makeRegistrationEntry("FOO", "A")
bar := makeRegistrationEntry("BAR", "B")
Expand Down
23 changes: 23 additions & 0 deletions pkg/common/telemetry/agent/lru.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package agent

import "github.com/spiffe/spire/pkg/common/telemetry"

func IncrementEntriesAdded(m telemetry.Metrics, entriesAdded int) {
m.IncrCounter([]string{telemetry.EntryAdded}, float32(entriesAdded))
}

func IncrementEntriesUpdated(m telemetry.Metrics, entriesUpdated int) {
m.IncrCounter([]string{telemetry.EntryUpdated}, float32(entriesUpdated))
}

func IncrementEntriesRemoved(m telemetry.Metrics, entriesRemoved int) {
m.IncrCounter([]string{telemetry.EntryRemoved}, float32(entriesRemoved))
}

func SetEntriesMapSize(m telemetry.Metrics, recordMapSize int) {
m.SetGauge([]string{telemetry.RecordMapSize}, float32(recordMapSize))
}

func SetSVIDMapSize(m telemetry.Metrics, svidMapSize int) {
m.SetGauge([]string{telemetry.SVIDMapSize}, float32(svidMapSize))
}
46 changes: 0 additions & 46 deletions pkg/common/telemetry/lru.go

This file was deleted.