@@ -40,6 +40,9 @@ import (
4040
4141 "github.com/go-logr/logr"
4242 monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
43+ "go.opentelemetry.io/otel/attribute"
44+ "go.opentelemetry.io/otel/codes"
45+ "go.opentelemetry.io/otel/trace"
4346 apimeta "k8s.io/apimachinery/pkg/api/meta"
4447
4548 agentraxv1alpha1 "github.com/gitcommitankit/agentrax/api/v1alpha1"
@@ -125,7 +128,23 @@ type AgentDeploymentReconciler struct {
125128// It creates and self-heals a Deployment, Service, and (when Prometheus Operator is present)
126129// a ServiceMonitor as owned child resources, then updates status conditions.
127130func (r * AgentDeploymentReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
131+ // Start a root OTel span for the entire reconcile loop. The span is ended via
132+ // defer so all exit paths (early return, error, requeue) are covered.
133+ ctx , span := observability .Tracer .Start (ctx , "reconcile" ,
134+ trace .WithAttributes (
135+ attribute .String ("tenant" , req .Namespace ),
136+ attribute .String ("name" , req .Name ),
137+ ),
138+ )
139+ defer span .End ()
140+
141+ // Enrich the controller-runtime logger with trace_id/span_id so every log
142+ // line emitted inside this reconcile cycle is correlated to the active OTel trace.
128143 logger := log .FromContext (ctx )
144+ if sc := span .SpanContext (); sc .IsValid () {
145+ logger = logger .WithValues ("trace_id" , sc .TraceID ().String (), "span_id" , sc .SpanID ().String ())
146+ }
147+ ctx = log .IntoContext (ctx , logger )
129148
130149 // Observe reconcile wall-clock duration on every exit path, including early
131150 // returns, errors, and requeues. The tenant label uses the request namespace
@@ -138,13 +157,19 @@ func (r *AgentDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Requ
138157 }()
139158
140159 // 1. Fetch the AgentDeployment; return immediately if it has been deleted.
160+ fetchCtx , fetchSpan := observability .Tracer .Start (ctx , "fetch_crd" )
141161 ad := & agentraxv1alpha1.AgentDeployment {}
142- if err := r .Get (ctx , req .NamespacedName , ad ); err != nil {
162+ if err := r .Get (fetchCtx , req .NamespacedName , ad ); err != nil {
143163 if apierrors .IsNotFound (err ) {
164+ fetchSpan .End ()
144165 return ctrl.Result {}, nil
145166 }
167+ fetchSpan .RecordError (err )
168+ fetchSpan .SetStatus (codes .Error , "fetch_crd failed" )
169+ fetchSpan .End ()
146170 return ctrl.Result {}, fmt .Errorf ("fetching AgentDeployment: %w" , err )
147171 }
172+ fetchSpan .End ()
148173
149174 // 2. Handle finalizer lifecycle.
150175 if ad .DeletionTimestamp .IsZero () {
@@ -202,37 +227,61 @@ func (r *AgentDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Requ
202227 }
203228 }
204229
230+ // 4–7. Reconcile child resources (Deployment, Service, ServiceMonitor, HPA)
231+ // under a single span. Each helper propagates ctx so sub-operations can be
232+ // correlated if they are instrumented in future phases.
233+ childrenCtx , childrenSpan := observability .Tracer .Start (ctx , "reconcile_children" )
234+
205235 // 4. Reconcile child Deployment.
206- if err := r .reconcileDeployment (ctx , ad ); err != nil {
236+ if err := r .reconcileDeployment (childrenCtx , ad ); err != nil {
237+ childrenSpan .RecordError (err )
238+ childrenSpan .SetStatus (codes .Error , "reconcile_children failed" )
239+ childrenSpan .End ()
207240 return ctrl.Result {}, fmt .Errorf ("reconciling deployment: %w" , err )
208241 }
209242
210243 // 5. Reconcile child Service.
211- if err := r .reconcileService (ctx , ad ); err != nil {
244+ if err := r .reconcileService (childrenCtx , ad ); err != nil {
245+ childrenSpan .RecordError (err )
246+ childrenSpan .SetStatus (codes .Error , "reconcile_children failed" )
247+ childrenSpan .End ()
212248 return ctrl.Result {}, fmt .Errorf ("reconciling service: %w" , err )
213249 }
214250
215251 // 6. Reconcile ServiceMonitor when Prometheus Operator is present.
216- if err := r .reconcileServiceMonitor (ctx , ad ); err != nil {
252+ if err := r .reconcileServiceMonitor (childrenCtx , ad ); err != nil {
253+ childrenSpan .RecordError (err )
254+ childrenSpan .SetStatus (codes .Error , "reconcile_children failed" )
255+ childrenSpan .End ()
217256 return ctrl.Result {}, fmt .Errorf ("reconciling servicemonitor: %w" , err )
218257 }
219258
220259 // 7. Reconcile the managed HPA (skip during active canary — Phase 4 owns it).
221260 // reconcileHPA also returns the quota evaluation state so updateStatus can
222261 // write the correct QuotaLimited condition onto the freshly re-fetched object.
223- hpaResult , qs , err := r .reconcileHPA (ctx , ad )
262+ hpaResult , qs , err := r .reconcileHPA (childrenCtx , ad )
224263 if err != nil {
264+ childrenSpan .RecordError (err )
265+ childrenSpan .SetStatus (codes .Error , "reconcile_children failed" )
266+ childrenSpan .End ()
225267 return ctrl.Result {}, fmt .Errorf ("reconciling hpa: %w" , err )
226268 }
269+ childrenSpan .End ()
227270
228271 // 8. Derive status from the live Deployment and update it — always last.
229272 // We continue into updateStatus even when hpaResult requests a requeue so
230273 // that the QuotaLimited condition is written in the same reconcile cycle.
231274 // Return the shorter of the two requeue intervals.
232- statusResult , err := r .updateStatus (ctx , ad , logger , qs )
275+ statusCtx , statusSpan := observability .Tracer .Start (ctx , "update_status" )
276+ statusResult , err := r .updateStatus (statusCtx , ad , logger , qs )
233277 if err != nil {
278+ statusSpan .RecordError (err )
279+ statusSpan .SetStatus (codes .Error , "update_status failed" )
280+ statusSpan .End ()
234281 return statusResult , fmt .Errorf ("updating status: %w" , err )
235282 }
283+ statusSpan .End ()
284+
236285 if hpaResult .RequeueAfter > 0 {
237286 if statusResult .RequeueAfter == 0 || hpaResult .RequeueAfter < statusResult .RequeueAfter {
238287 return hpaResult , nil
0 commit comments