Skip to content

Commit

Permalink
feat(operator): Introduce OTel tracing for Task controller
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Oct 7, 2022
1 parent a195614 commit 805b62a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions operator/api/v1alpha1/semconv/semconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func AddAttributeFromWorkloadInstance(s trace.Span, w v1alpha1.KeptnWorkloadInst
s.SetAttributes(common.Version.String(w.Spec.Version))
}

func AddAttributeFromTask(s trace.Span, t v1alpha1.KeptnTask) {
s.SetAttributes(common.ApplicationName.String(t.Spec.AppName))
s.SetAttributes(common.Workload.String(t.Spec.Workload))
s.SetAttributes(common.Version.String(t.Spec.WorkloadVersion))
s.SetAttributes(common.TaskName.String(t.Name))
s.SetAttributes(common.TaskType.String(string(t.Spec.Type)))
}

func AddAttributeFromAnnotations(s trace.Span, annotations map[string]string) {
s.SetAttributes(common.ApplicationName.String(annotations[common.AppAnnotation]))
s.SetAttributes(common.Workload.String(annotations[common.WorkloadAnnotation]))
Expand Down
20 changes: 20 additions & 0 deletions operator/controllers/keptntask/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
"github.com/go-logr/logr"
klcv1alpha1 "github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1"
"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common"
"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/semconv"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -41,6 +46,7 @@ type KeptnTaskReconciler struct {
Recorder record.EventRecorder
Log logr.Logger
Meters common.KeptnMeters
Tracer trace.Tracer
}

//+kubebuilder:rbac:groups=lifecycle.keptn.sh,resources=keptntasks,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -53,13 +59,22 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
r.Log.Info("Reconciling KeptnTask")
task := &klcv1alpha1.KeptnTask{}

traceContextCarrier := propagation.MapCarrier(task.Annotations)
ctx = otel.GetTextMapPropagator().Extract(ctx, traceContextCarrier)

ctx, span := r.Tracer.Start(ctx, "reconcile_task", trace.WithSpanKind(trace.SpanKindConsumer))
defer span.End()

semconv.AddAttributeFromTask(span, *task)

if err := r.Client.Get(ctx, req.NamespacedName, task); err != nil {
if errors.IsNotFound(err) {
// taking down all associated K8s resources is handled by K8s
r.Log.Info("KeptnTask resource not found. Ignoring since object must be deleted")
return ctrl.Result{}, nil
}
r.Log.Error(err, "Failed to get the KeptnTask")
span.SetStatus(codes.Error, err.Error())
return ctrl.Result{Requeue: true, RequeueAfter: 30 * time.Second}, nil
}

Expand All @@ -71,18 +86,21 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

err := r.Client.Status().Update(ctx, task)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return ctrl.Result{Requeue: true}, err
}

jobExists, err := r.JobExists(ctx, *task, req.Namespace)
if err != nil {
r.Log.Error(err, "Could not check if job is running")
span.SetStatus(codes.Error, err.Error())
return ctrl.Result{Requeue: true, RequeueAfter: 30 * time.Second}, nil
}

if !jobExists {
err = r.createJob(ctx, req, task)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return ctrl.Result{Requeue: true}, err
}
return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
Expand All @@ -91,6 +109,7 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if !task.Status.Status.IsCompleted() {
err := r.updateJob(ctx, req, task)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, err
}
return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
Expand All @@ -108,6 +127,7 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

err = r.Client.Status().Update(ctx, task)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return ctrl.Result{Requeue: true}, err
}

Expand Down
1 change: 1 addition & 0 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func main() {
Log: ctrl.Log.WithName("KeptnTask Controller"),
Recorder: mgr.GetEventRecorderFor("keptntask-controller"),
Meters: meters,
Tracer: otel.Tracer("keptn/operator/task"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "KeptnTask")
os.Exit(1)
Expand Down

0 comments on commit 805b62a

Please sign in to comment.