44 "context"
55 "time"
66
7+ "go.opentelemetry.io/otel"
8+ "go.opentelemetry.io/otel/attribute"
9+ "go.opentelemetry.io/otel/trace"
710 corev1 "k8s.io/api/core/v1"
811 apierrors "k8s.io/apimachinery/pkg/api/errors"
912 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -15,6 +18,7 @@ import (
1518 "sigs.k8s.io/controller-runtime/pkg/reconcile"
1619
1720 impv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1"
21+ "github.com/syscode-labs/imp/internal/tracing"
1822)
1923
2024// ImpVMMigrationReconciler reconciles ImpVMMigration objects.
@@ -28,14 +32,22 @@ type ImpVMMigrationReconciler struct {
2832// +kubebuilder:rbac:groups=imp.dev,resources=impvmmigrations/finalizers,verbs=update
2933// +kubebuilder:rbac:groups=core,resources=nodes,verbs=get;list;watch
3034
31- func (r * ImpVMMigrationReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
35+ func (r * ImpVMMigrationReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (result ctrl.Result , err error ) {
3236 log := logf .FromContext (ctx )
3337
3438 mig := & impv1alpha1.ImpVMMigration {}
3539 if err := r .Get (ctx , req .NamespacedName , mig ); err != nil {
3640 return ctrl.Result {}, client .IgnoreNotFound (err )
3741 }
3842
43+ ctx , span := otel .Tracer ("imp.operator" ).Start (ctx , "operator.impvmmigration.reconcile" ,
44+ trace .WithAttributes (
45+ attribute .String ("mig.name" , req .Name ),
46+ attribute .String ("mig.namespace" , req .Namespace ),
47+ attribute .String ("mig.phase" , string (mig .Status .Phase )),
48+ ))
49+ defer func () { tracing .RecordError (span , err ); span .End () }()
50+
3951 switch mig .Status .Phase {
4052 case "" :
4153 return r .handleEmpty (ctx , mig )
@@ -65,12 +77,19 @@ func (r *ImpVMMigrationReconciler) handleEmpty(ctx context.Context, mig *impv1al
6577
6678// handlePending validates the source VM, selects a target node, creates a child
6779// ImpVMSnapshot, then advances to Phase="Snapshotting".
68- func (r * ImpVMMigrationReconciler ) handlePending (ctx context.Context , mig * impv1alpha1.ImpVMMigration ) (ctrl.Result , error ) {
80+ func (r * ImpVMMigrationReconciler ) handlePending (ctx context.Context , mig * impv1alpha1.ImpVMMigration ) (result ctrl.Result , err error ) {
6981 log := logf .FromContext (ctx )
7082
83+ ctx , span := otel .Tracer ("imp.operator" ).Start (ctx , "operator.impvmmigration.pending" ,
84+ trace .WithAttributes (
85+ attribute .String ("mig.name" , mig .Name ),
86+ attribute .String ("mig.namespace" , mig .Namespace ),
87+ ))
88+ defer func () { tracing .RecordError (span , err ); span .End () }()
89+
7190 // Validate source VM exists.
7291 vm := & impv1alpha1.ImpVM {}
73- err : = r .Get (ctx , client.ObjectKey {
92+ err = r .Get (ctx , client.ObjectKey {
7493 Namespace : mig .Spec .SourceVMNamespace ,
7594 Name : mig .Spec .SourceVMName ,
7695 }, vm )
@@ -139,7 +158,14 @@ func (r *ImpVMMigrationReconciler) handlePending(ctx context.Context, mig *impv1
139158// handleSnapshotting waits for the child snapshot to reach a terminal state.
140159// On success it creates the target VM and advances to "Restoring".
141160// On failure it sets Phase="Failed".
142- func (r * ImpVMMigrationReconciler ) handleSnapshotting (ctx context.Context , mig * impv1alpha1.ImpVMMigration ) (ctrl.Result , error ) {
161+ func (r * ImpVMMigrationReconciler ) handleSnapshotting (ctx context.Context , mig * impv1alpha1.ImpVMMigration ) (result ctrl.Result , err error ) {
162+ ctx , span := otel .Tracer ("imp.operator" ).Start (ctx , "operator.impvmmigration.snapshotting" ,
163+ trace .WithAttributes (
164+ attribute .String ("mig.name" , mig .Name ),
165+ attribute .String ("mig.namespace" , mig .Namespace ),
166+ ))
167+ defer func () { tracing .RecordError (span , err ); span .End () }()
168+
143169 if mig .Status .SnapshotRef == "" {
144170 return r .failMigration (ctx , mig , "internal error: Snapshotting phase without SnapshotRef" )
145171 }
@@ -219,7 +245,14 @@ func (r *ImpVMMigrationReconciler) createTargetVM(ctx context.Context, mig *impv
219245
220246// handleRestoring waits for the target VM to reach Running, then deletes the source
221247// VM and marks the migration Succeeded.
222- func (r * ImpVMMigrationReconciler ) handleRestoring (ctx context.Context , mig * impv1alpha1.ImpVMMigration ) (ctrl.Result , error ) {
248+ func (r * ImpVMMigrationReconciler ) handleRestoring (ctx context.Context , mig * impv1alpha1.ImpVMMigration ) (result ctrl.Result , err error ) {
249+ ctx , span := otel .Tracer ("imp.operator" ).Start (ctx , "operator.impvmmigration.restoring" ,
250+ trace .WithAttributes (
251+ attribute .String ("mig.name" , mig .Name ),
252+ attribute .String ("mig.namespace" , mig .Namespace ),
253+ ))
254+ defer func () { tracing .RecordError (span , err ); span .End () }()
255+
223256 log := logf .FromContext (ctx )
224257
225258 targetVM := & impv1alpha1.ImpVM {}
0 commit comments