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

feat(kuma-cp): multitenant counter metrics #6707

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
2 changes: 1 addition & 1 deletion pkg/metrics/components/cp_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Setup(rt runtime.Runtime) error {
// We don't want to use cached ResourceManager because the cache is just for a couple of seconds
// and we will be retrieving resources every minute. There is no other place in the system for now that needs all resources from all meshes
// therefore it makes no sense to cache all content of the Database in the cache.
counter, err := metrics.NewStoreCounter(rt.ResourceManager(), rt.Metrics())
counter, err := metrics.NewStoreCounter(rt.ResourceManager(), rt.Metrics(), rt.Tenants())
if err != nil {
return err
}
Expand Down
29 changes: 22 additions & 7 deletions pkg/metrics/store/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ import (
"github.com/kumahq/kuma/pkg/core/runtime/component"
"github.com/kumahq/kuma/pkg/core/user"
"github.com/kumahq/kuma/pkg/metrics"
"github.com/kumahq/kuma/pkg/multitenant"
)

var log = core.Log.WithName("metrics").WithName("store-counter")

type storeCounter struct {
resManager manager.ReadOnlyResourceManager
counts *prometheus.GaugeVec
tenants multitenant.Tenants
}

var _ component.Component = &storeCounter{}

func NewStoreCounter(resManager manager.ReadOnlyResourceManager, metrics metrics.Metrics) (*storeCounter, error) {
func NewStoreCounter(resManager manager.ReadOnlyResourceManager, metrics metrics.Metrics, tenants multitenant.Tenants) (*storeCounter, error) {
counts := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "resources_count",
}, []string{"resource_type"})
}, []string{"resource_type", "tenant"})

if err := metrics.Register(counts); err != nil {
return nil, err
Expand All @@ -37,6 +39,7 @@ func NewStoreCounter(resManager manager.ReadOnlyResourceManager, metrics metrics
return &storeCounter{
resManager: resManager,
counts: counts,
tenants: tenants,
}, nil
}

Expand All @@ -50,14 +53,13 @@ func (s *storeCounter) StartWithTicker(stop <-chan struct{}, ticker *time.Ticker
ctx := user.Ctx(context.Background(), user.ControlPlane)

log.Info("starting the resource counter")
if err := s.count(ctx); err != nil {
if err := s.countForAllTenants(ctx); err != nil {
log.Error(err, "unable to count resources")
}
for {
select {
case <-ticker.C:
// TODO: make metrics tenant aware https://github.com/kumahq/kuma/issues/6622
if err := s.count(ctx); err != nil {
if err := s.countForAllTenants(ctx); err != nil {
log.Error(err, "unable to count resources")
}
case <-stop:
Expand All @@ -71,7 +73,20 @@ func (s *storeCounter) NeedLeaderElection() bool {
return true
}

func (s *storeCounter) count(ctx context.Context) error {
func (s *storeCounter) countForAllTenants(ctx context.Context) error {
tenantIDs, err := s.tenants.GetIDs(ctx)
if err != nil {
return err
}
for _, tenantID := range tenantIDs {
if err := s.count(multitenant.WithTenant(ctx, tenantID), tenantID); err != nil {
return err
}
}
return nil
}

func (s *storeCounter) count(ctx context.Context, tenantID string) error {
resourceCount := map[string]uint32{}
if err := s.countGlobalScopedResources(ctx, resourceCount); err != nil {
return err
Expand All @@ -80,7 +95,7 @@ func (s *storeCounter) count(ctx context.Context) error {
return err
}
for resType, counter := range resourceCount {
s.counts.WithLabelValues(resType).Set(float64(counter))
s.counts.WithLabelValues(resType, tenantID).Set(float64(counter))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/store/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ = Describe("Counter", func() {
resManager = manager.NewResourceManager(store)

counterTicker := time.NewTicker(500 * time.Millisecond)
counter, err := metrics_store.NewStoreCounter(resManager, metrics)
counter, err := metrics_store.NewStoreCounter(resManager, metrics, multitenant.SingleTenant)
Expect(err).ToNot(HaveOccurred())

resyncer := insights.NewResyncer(&insights.Config{
Expand Down