Skip to content

Commit 83ca31d

Browse files
committed
feat(tracing): add agent.impvm.reconcile, start, and stop spans
Instrument the agent ImpVM reconciler with OTel tracing: - agent.impvm.reconcile root span with vm.name/namespace/node/phase attrs - agent.impvm.start cross-process child span via SpanFromVM (picks up operator traceparent from imp.dev/trace-context annotation) - agent.impvm.stop child span in handleTerminating
1 parent 062b810 commit 83ca31d

1 file changed

Lines changed: 45 additions & 5 deletions

File tree

internal/agent/reconciler.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"net/http"
99
"time"
1010

11+
"go.opentelemetry.io/otel"
12+
"go.opentelemetry.io/otel/attribute"
13+
"go.opentelemetry.io/otel/trace"
1114
apierrors "k8s.io/apimachinery/pkg/api/errors"
1215
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1316
"k8s.io/apimachinery/pkg/runtime"
@@ -18,6 +21,7 @@ import (
1821

1922
impdevv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1"
2023
"github.com/syscode-labs/imp/internal/agent/network"
24+
"github.com/syscode-labs/imp/internal/tracing"
2125
)
2226

2327
// ImpVMReconciler watches ImpVM objects and drives VM lifecycle on this node.
@@ -46,11 +50,11 @@ type ImpVMReconciler struct {
4650
// +kubebuilder:rbac:groups=imp.dev,resources=impnetworks,verbs=get;list;watch
4751
// +kubebuilder:rbac:groups=imp.dev,resources=impnetworks/status,verbs=get;update;patch
4852

49-
func (r *ImpVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
53+
func (r *ImpVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
5054
log := logf.FromContext(ctx).WithValues("node", r.NodeName)
5155

5256
vm := &impdevv1alpha1.ImpVM{}
53-
if err := r.Get(ctx, req.NamespacedName, vm); err != nil {
57+
if err = r.Get(ctx, req.NamespacedName, vm); err != nil {
5458
return ctrl.Result{}, client.IgnoreNotFound(err)
5559
}
5660

@@ -59,6 +63,19 @@ func (r *ImpVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
5963
return ctrl.Result{}, nil
6064
}
6165

66+
ctx, span := otel.Tracer("imp.agent").Start(ctx, "agent.impvm.reconcile",
67+
trace.WithAttributes(
68+
attribute.String("vm.name", req.Name),
69+
attribute.String("vm.namespace", req.Namespace),
70+
attribute.String("vm.node", r.NodeName),
71+
attribute.String("vm.phase", string(vm.Status.Phase)),
72+
),
73+
)
74+
defer func() {
75+
tracing.RecordError(span, err)
76+
span.End()
77+
}()
78+
6279
log = log.WithValues("vm", req.NamespacedName, "phase", vm.Status.Phase)
6380

6481
switch vm.Status.Phase {
@@ -94,9 +111,21 @@ func (r *ImpVMReconciler) handleStarting(ctx context.Context, vm *impdevv1alpha1
94111
return ctrl.Result{RequeueAfter: 2 * time.Second}, nil
95112
}
96113

97-
func (r *ImpVMReconciler) handleScheduled(ctx context.Context, vm *impdevv1alpha1.ImpVM) (ctrl.Result, error) {
114+
func (r *ImpVMReconciler) handleScheduled(ctx context.Context, vm *impdevv1alpha1.ImpVM) (result ctrl.Result, err error) {
98115
log := logf.FromContext(ctx)
99116

117+
ctx, span := tracing.SpanFromVM(ctx, vm, "agent.impvm.start",
118+
trace.WithAttributes(
119+
attribute.String("vm.name", vm.Name),
120+
attribute.String("vm.namespace", vm.Namespace),
121+
attribute.String("vm.node", r.NodeName),
122+
),
123+
)
124+
defer func() {
125+
tracing.RecordError(span, err)
126+
span.End()
127+
}()
128+
100129
// Set phase=Starting before calling driver to make concurrent reconciles idempotent.
101130
base := vm.DeepCopy()
102131
vm.Status.Phase = impdevv1alpha1.VMPhaseStarting
@@ -200,10 +229,21 @@ func (r *ImpVMReconciler) handleRunning(ctx context.Context, vm *impdevv1alpha1.
200229
return r.finishFailed(ctx, vm)
201230
}
202231

203-
func (r *ImpVMReconciler) handleTerminating(ctx context.Context, vm *impdevv1alpha1.ImpVM) (ctrl.Result, error) {
232+
func (r *ImpVMReconciler) handleTerminating(ctx context.Context, vm *impdevv1alpha1.ImpVM) (result ctrl.Result, err error) {
204233
log := logf.FromContext(ctx)
205234

206-
if err := r.Driver.Stop(ctx, vm); err != nil {
235+
ctx, span := otel.Tracer("imp.agent").Start(ctx, "agent.impvm.stop",
236+
trace.WithAttributes(
237+
attribute.String("vm.name", vm.Name),
238+
attribute.String("vm.namespace", vm.Namespace),
239+
),
240+
)
241+
defer func() {
242+
tracing.RecordError(span, err)
243+
span.End()
244+
}()
245+
246+
if err = r.Driver.Stop(ctx, vm); err != nil {
207247
log.Error(err, "Driver Stop failed — will retry")
208248
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
209249
}

0 commit comments

Comments
 (0)