@@ -39,6 +39,7 @@ import (
3939 "sigs.k8s.io/controller-runtime/pkg/manager"
4040 "sigs.k8s.io/controller-runtime/pkg/reconcile"
4141
42+ sentrygo "github.com/getsentry/sentry-go"
4243 "github.com/percona/percona-postgresql-operator/internal/config"
4344 "github.com/percona/percona-postgresql-operator/internal/controller/runtime"
4445 "github.com/percona/percona-postgresql-operator/internal/initialize"
@@ -84,19 +85,35 @@ func (r *Reconciler) Reconcile(
8485 ctx context.Context , request reconcile.Request ,
8586) (reconcile.Result , error ) {
8687 ctx , span := r .Tracer .Start (ctx , "Reconcile" )
87- log := logging .FromContext (ctx )
8888 defer span .End ()
8989
90+ // Add Sentry context
91+ hub := sentrygo .GetHubFromContext (ctx )
92+ if hub == nil {
93+ hub = sentrygo .CurrentHub ().Clone ()
94+ }
95+ hub .ConfigureScope (func (scope * sentrygo.Scope ) {
96+ scope .SetTag ("namespace" , request .Namespace )
97+ scope .SetTag ("name" , request .Name )
98+ })
99+ ctx = sentrygo .SetHubOnContext (ctx , hub )
100+ defer func () {
101+ if err := recover (); err != nil {
102+ hub .Recover (err )
103+ panic (err )
104+ }
105+ }()
106+
107+ log := logging .FromContext (ctx )
108+
90109 // get the postgrescluster from the cache
91110 cluster := & v1beta1.PostgresCluster {}
92- if err := r .Client .Get (ctx , request .NamespacedName , cluster ); err != nil {
93- // NotFound cannot be fixed by requeuing so ignore it. During background
94- // deletion, we receive delete events from cluster's dependents after
95- // cluster is deleted.
96- if err = client .IgnoreNotFound (err ); err != nil {
97- log .Error (err , "unable to fetch PostgresCluster" )
98- span .RecordError (err )
99- }
111+ err := r .Client .Get (ctx , request .NamespacedName , cluster )
112+ if err = client .IgnoreNotFound (err ); err != nil {
113+ log .Error (err , "unable to fetch PostgresCluster" )
114+ span .RecordError (err )
115+ // Record unexpected errors
116+ hub .CaptureException (err )
100117 return runtime .ErrorWithBackoff (err )
101118 }
102119
@@ -116,8 +133,7 @@ func (r *Reconciler) Reconcile(
116133
117134 // Keep a copy of cluster prior to any manipulations.
118135 before := cluster .DeepCopy ()
119- var err error
120- result := reconcile.Result {}
136+ var result reconcile.Result
121137 defer func () {
122138 if ! equality .Semantic .DeepEqual (before .Status , cluster .Status ) {
123139 statusErr := r .Client .Status ().Patch (ctx , cluster , client .MergeFrom (before ), r .Owner )
@@ -141,7 +157,7 @@ func (r *Reconciler) Reconcile(
141157 if deleteResult , deleteErr := r .handleDelete (ctx , cluster ); deleteErr != nil {
142158 span .RecordError (deleteErr )
143159 log .Error (deleteErr , "deleting" )
144- return runtime . ErrorWithBackoff ( deleteErr )
160+ return reconcile. Result {}, errors . Join ( err , deleteErr )
145161
146162 } else if deleteResult != nil {
147163 if log := log .V (1 ); log .Enabled () {
@@ -162,7 +178,7 @@ func (r *Reconciler) Reconcile(
162178 // specifically allow reconciliation if the cluster is shutdown to
163179 // facilitate upgrades, otherwise return
164180 if ! initialize .FromPointer (cluster .Spec .Shutdown ) {
165- return runtime . ErrorWithBackoff ( err )
181+ return reconcile. Result {}, err
166182 }
167183 }
168184
@@ -176,7 +192,7 @@ func (r *Reconciler) Reconcile(
176192 path := field .NewPath ("spec" , "standby" )
177193 err := field .Invalid (path , cluster .Name , "Standby requires a host or repoName to be enabled" )
178194 r .Recorder .Event (cluster , corev1 .EventTypeWarning , "InvalidStandbyConfiguration" , err .Error ())
179- return runtime . ErrorWithBackoff ( err )
195+ return reconcile. Result {}, err
180196 }
181197
182198 var (
@@ -226,7 +242,7 @@ func (r *Reconciler) Reconcile(
226242
227243 ObservedGeneration : cluster .GetGeneration (),
228244 })
229- return runtime . ErrorWithBackoff ( patchClusterStatus ())
245+ return reconcile. Result {}, errors . Join ( err , patchClusterStatus ())
230246 } else {
231247 meta .RemoveStatusCondition (& cluster .Status .Conditions , v1beta1 .PostgresClusterProgressing )
232248 }
@@ -269,7 +285,7 @@ func (r *Reconciler) Reconcile(
269285 returnEarly , err := r .reconcileDirMoveJobs (ctx , cluster )
270286 if err != nil || returnEarly {
271287 log .V (1 ).Info ("waiting for directory move jobs" , "cluster" , cluster .Name , "error" , err )
272- return runtime . ErrorWithBackoff ( errors .Join (err , patchClusterStatus () ))
288+ return reconcile. Result {}, errors .Join (err , patchClusterStatus ())
273289 }
274290 }
275291 if err == nil {
@@ -325,7 +341,7 @@ func (r *Reconciler) Reconcile(
325341 returnEarly , err := r .reconcileDataSource (ctx , cluster , instances , clusterVolumes , rootCA )
326342 if err != nil || returnEarly {
327343 log .V (1 ).Info ("waiting for data source initialization" , "cluster" , cluster .Name , "error" , err )
328- return runtime . ErrorWithBackoff ( errors .Join (err , patchClusterStatus () ))
344+ return reconcile. Result {}, errors .Join (err , patchClusterStatus ())
329345 }
330346 }
331347 if err == nil {
@@ -431,6 +447,12 @@ func (r *Reconciler) Reconcile(
431447 "requeue" , result .Requeue ,
432448 "requeueAfter" , result .RequeueAfter )
433449
450+ // If we get here, reconciliation succeeded.
451+ // Record any errors that occurred during reconciliation.
452+ if err != nil {
453+ hub .CaptureException (err )
454+ }
455+
434456 return result , errors .Join (err , patchClusterStatus ())
435457}
436458
0 commit comments