@@ -19,6 +19,7 @@ package http
1919import (
2020 "context"
2121 "fmt"
22+ "sync"
2223 "time"
2324
2425 kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
@@ -37,6 +38,15 @@ import (
3738 "github.com/kedacore/http-add-on/operator/metrics"
3839)
3940
41+ type httpScaledObjectMetricsData struct {
42+ namespace string
43+ }
44+
45+ var (
46+ httpScaledObjectPromMetricsMap map [string ]httpScaledObjectMetricsData
47+ httpScaledObjectPromMetricsLock * sync.Mutex
48+ )
49+
4050// HTTPScaledObjectReconciler reconciles a HTTPScaledObject object
4151//
4252//revive:disable-next-line:exported
@@ -49,6 +59,11 @@ type HTTPScaledObjectReconciler struct {
4959 BaseConfig config.Base
5060}
5161
62+ func init () {
63+ httpScaledObjectPromMetricsMap = make (map [string ]httpScaledObjectMetricsData )
64+ httpScaledObjectPromMetricsLock = & sync.Mutex {}
65+ }
66+
5267// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects,verbs=get;list;watch;create;update;patch;delete
5368// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/status,verbs=get;update;patch
5469// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/finalizers,verbs=update
@@ -79,6 +94,7 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
7994 }
8095
8196 if httpso .GetDeletionTimestamp () != nil {
97+ r .updatePromMetricsOnDelete (ctx , httpso )
8298 return ctrl.Result {}, finalizeScaledObject (ctx , logger , r .Client , httpso )
8399 }
84100
@@ -99,7 +115,6 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
99115 "DeploymentName" ,
100116 httpso .Name ,
101117 )
102- r .updatePromMetrics (ctx , httpso , req .Namespace )
103118
104119 // check if ScalingMetric is correct
105120 if httpso .Spec .ScalingMetric != nil &&
@@ -141,6 +156,7 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
141156 )
142157
143158 // success reconciling
159+ r .updatePromMetrics (ctx , httpso )
144160 logger .Info ("Reconcile success" )
145161 return ctrl.Result {}, nil
146162}
@@ -165,14 +181,34 @@ func (r *HTTPScaledObjectReconciler) SetupWithManager(mgr ctrl.Manager) error {
165181 Complete (r )
166182}
167183
168- func (r * HTTPScaledObjectReconciler ) updatePromMetrics (ctx context.Context , scaledObject * httpv1alpha1.HTTPScaledObject , namespacedName string ) {
184+ func (r * HTTPScaledObjectReconciler ) updatePromMetrics (ctx context.Context , scaledObject * httpv1alpha1.HTTPScaledObject ) {
185+ httpScaledObjectPromMetricsLock .Lock ()
186+ defer httpScaledObjectPromMetricsLock .Unlock ()
187+
188+ namespacedName := scaledObject .Name + scaledObject .Namespace
189+ metricsData , ok := httpScaledObjectPromMetricsMap [namespacedName ]
190+ if ok {
191+ metrics .RecordDeleteHTTPScaledObjectCount (scaledObject .Namespace )
192+ }
193+ metricsData .namespace = scaledObject .Namespace
194+
169195 logger := log .FromContext (ctx , "updatePromMetrics" , namespacedName )
170196 logger .Info ("updatePromMetrics" )
171- metrics .RecordHTTPScaledObjectCount (namespacedName )
197+ metrics .RecordHTTPScaledObjectCount (scaledObject .Namespace )
198+
199+ httpScaledObjectPromMetricsMap [namespacedName ] = metricsData
172200}
173201
174- func (r * HTTPScaledObjectReconciler ) updatePromMetricsOnDelete (ctx context.Context , scaledObject * httpv1alpha1.HTTPScaledObject , namespacedName string ) {
202+ func (r * HTTPScaledObjectReconciler ) updatePromMetricsOnDelete (ctx context.Context , scaledObject * httpv1alpha1.HTTPScaledObject ) {
203+ httpScaledObjectPromMetricsLock .Lock ()
204+ defer httpScaledObjectPromMetricsLock .Unlock ()
205+
206+ namespacedName := scaledObject .Name + scaledObject .Namespace
175207 logger := log .FromContext (ctx , "updatePromMetricsOnDelete" , namespacedName )
176208 logger .Info ("updatePromMetricsOnDelete" )
177- metrics .RecordDeleteHTTPScaledObjectCount (namespacedName )
209+
210+ if _ , ok := httpScaledObjectPromMetricsMap [namespacedName ]; ok {
211+ metrics .RecordDeleteHTTPScaledObjectCount (scaledObject .Namespace )
212+ }
213+ delete (httpScaledObjectPromMetricsMap , namespacedName )
178214}
0 commit comments