Skip to content

Commit 96eb969

Browse files
committed
feat(tracing): add operator.impvm.reconcile and operator.impvm.schedule spans
1 parent 3d1a08e commit 96eb969

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

internal/controller/impvm_controller.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"context"
2121
"time"
2222

23+
"go.opentelemetry.io/otel"
24+
"go.opentelemetry.io/otel/attribute"
25+
"go.opentelemetry.io/otel/trace"
2326
corev1 "k8s.io/api/core/v1"
2427
apierrors "k8s.io/apimachinery/pkg/api/errors"
2528
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -34,6 +37,7 @@ import (
3437
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3538

3639
impdevv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1"
40+
"github.com/syscode-labs/imp/internal/tracing"
3741
)
3842

3943
const (
@@ -58,14 +62,22 @@ type ImpVMReconciler struct {
5862
// +kubebuilder:rbac:groups=imp.dev,resources=clusterimpconfigs,verbs=get;list;watch
5963
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
6064

61-
func (r *ImpVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
65+
func (r *ImpVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
6266
log := logf.FromContext(ctx)
6367

6468
vm := &impdevv1alpha1.ImpVM{}
6569
if err := r.Get(ctx, req.NamespacedName, vm); err != nil {
6670
return ctrl.Result{}, client.IgnoreNotFound(err)
6771
}
6872

73+
ctx, span := otel.Tracer("imp.operator").Start(ctx, "operator.impvm.reconcile",
74+
trace.WithAttributes(
75+
attribute.String("vm.name", req.Name),
76+
attribute.String("vm.namespace", req.Namespace),
77+
attribute.String("vm.phase", string(vm.Status.Phase)),
78+
))
79+
defer func() { tracing.RecordError(span, err); span.End() }()
80+
6981
// 1. Handle deletion
7082
if !vm.DeletionTimestamp.IsZero() {
7183
return r.handleDeletion(ctx, vm)
@@ -103,6 +115,7 @@ func (r *ImpVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
103115
// Assign to node
104116
specPatch := client.MergeFrom(vm.DeepCopy())
105117
vm.Spec.NodeName = nodeName
118+
tracing.InjectToVM(ctx, vm, span)
106119
if err := r.Patch(ctx, vm, specPatch); err != nil {
107120
return ctrl.Result{}, err
108121
}

internal/controller/impvm_scheduler.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ import (
2121
"sort"
2222
"strings"
2323

24+
"go.opentelemetry.io/otel"
25+
"go.opentelemetry.io/otel/attribute"
26+
"go.opentelemetry.io/otel/trace"
2427
corev1 "k8s.io/api/core/v1"
2528
apierrors "k8s.io/apimachinery/pkg/api/errors"
2629
"sigs.k8s.io/controller-runtime/pkg/client"
2730
logf "sigs.k8s.io/controller-runtime/pkg/log"
2831

2932
impdevv1alpha1 "github.com/syscode-labs/imp/api/v1alpha1"
33+
"github.com/syscode-labs/imp/internal/tracing"
3034
)
3135

3236
const labelImpEnabled = "imp/enabled"
@@ -60,7 +64,14 @@ func sumUsedResources(ctx context.Context, c client.Client, vms []impdevv1alpha1
6064

6165
// schedule selects a node for vm using a capacity-aware least-loaded strategy.
6266
// Returns "" and no error when no suitable node is available.
63-
func (r *ImpVMReconciler) schedule(ctx context.Context, vm *impdevv1alpha1.ImpVM) (string, error) {
67+
func (r *ImpVMReconciler) schedule(ctx context.Context, vm *impdevv1alpha1.ImpVM) (nodeName string, err error) {
68+
ctx, span := otel.Tracer("imp.operator").Start(ctx, "operator.impvm.schedule",
69+
trace.WithAttributes(
70+
attribute.String("vm.name", vm.Name),
71+
attribute.String("vm.namespace", vm.Namespace),
72+
))
73+
defer func() { tracing.RecordError(span, err); span.End() }()
74+
6475
log := logf.FromContext(ctx)
6576

6677
// 1. List nodes with imp/enabled=true

0 commit comments

Comments
 (0)