From b99434f7133c8f96dc27b333a61a40438f0559c7 Mon Sep 17 00:00:00 2001 From: Minyi Zhu Date: Fri, 27 Dec 2024 15:19:02 -0500 Subject: [PATCH] move store to static --- cmd/cluster-agent/api/v2/series/job.go | 5 ++++- .../autoscaling/workload/loadstore/store.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/cluster-agent/api/v2/series/job.go b/cmd/cluster-agent/api/v2/series/job.go index 8dca57e93814fb..c77c095005ca9c 100644 --- a/cmd/cluster-agent/api/v2/series/job.go +++ b/cmd/cluster-agent/api/v2/series/job.go @@ -62,7 +62,7 @@ func newJobQueue(ctx context.Context) *jobQueue { Limiter: rate.NewLimiter(rate.Limit(payloadProcessQPS), payloadProcessRateBurst), }, )), - store: loadstore.NewEntityStore(ctx), + store: loadstore.GetWorkloadMetricStore(ctx), isStarted: false, } go q.start(ctx) @@ -113,6 +113,9 @@ func (jq *jobQueue) reportTelemetry(ctx context.Context) { case <-ctx.Done(): return case <-infoTicker.C: + if jq.store == nil { + continue + } info := jq.store.GetStoreInfo() statsResults := info.StatsResults for _, statsResult := range statsResults { diff --git a/pkg/clusteragent/autoscaling/workload/loadstore/store.go b/pkg/clusteragent/autoscaling/workload/loadstore/store.go index 604ad6f954c46b..59dc056c9cd6ad 100644 --- a/pkg/clusteragent/autoscaling/workload/loadstore/store.go +++ b/pkg/clusteragent/autoscaling/workload/loadstore/store.go @@ -8,12 +8,27 @@ package loadstore import ( + "context" "strings" + "sync" "github.com/DataDog/agent-payload/v5/gogen" "github.com/DataDog/datadog-agent/pkg/util/kubernetes" ) +var ( + WorkloadMetricStore Store + WorkloadMetricStoreOnce sync.Once +) + +// GetWorkloadMetricStore returns the workload metric store, init once +func GetWorkloadMetricStore(ctx context.Context) Store { + WorkloadMetricStoreOnce.Do(func() { + WorkloadMetricStore = NewEntityStore(ctx) + }) + return WorkloadMetricStore +} + // StoreInfo represents the store information which aggregates the entities to lowest level, i.e., container level type StoreInfo struct { currentTime Timestamp