From a155945a5737c80426576e7c5e82f4684aa87e10 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Tue, 22 Nov 2022 08:48:33 +0100 Subject: [PATCH] fixed propagation of span contexts Signed-off-by: Florian Bacher --- .../v1alpha1/keptnworkloadinstance_types.go | 3 +++ .../tests/keptnworkloadinstance_test.go | 2 ++ operator/controllers/common/spanhandler.go | 20 +++++++++++++------ .../keptnworkloadinstance/controller.go | 3 ++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/operator/api/v1alpha1/keptnworkloadinstance_types.go b/operator/api/v1alpha1/keptnworkloadinstance_types.go index 9ae58c1c6fb..ee9e223dc5a 100644 --- a/operator/api/v1alpha1/keptnworkloadinstance_types.go +++ b/operator/api/v1alpha1/keptnworkloadinstance_types.go @@ -389,6 +389,9 @@ func (w KeptnWorkloadInstance) GetSpanKey(phase string) string { } func (w KeptnWorkloadInstance) GetSpanName(phase string) string { + if phase == "" { + return w.Name + } return fmt.Sprintf("%s/%s", w.Spec.WorkloadName, phase) } diff --git a/operator/api/v1alpha1/tests/keptnworkloadinstance_test.go b/operator/api/v1alpha1/tests/keptnworkloadinstance_test.go index b7bab8f7b1a..855dd12ba0c 100644 --- a/operator/api/v1alpha1/tests/keptnworkloadinstance_test.go +++ b/operator/api/v1alpha1/tests/keptnworkloadinstance_test.go @@ -205,6 +205,8 @@ func TestKeptnWorkloadInstance(t *testing.T) { }, }, evaluation.Spec) + require.Equal(t, "workload", workload.GetSpanName("")) + require.Equal(t, "workloadname/phase", workload.GetSpanName("phase")) require.Equal(t, []attribute.KeyValue{ diff --git a/operator/controllers/common/spanhandler.go b/operator/controllers/common/spanhandler.go index 5ab64cbd935..d6ea540dcc6 100644 --- a/operator/controllers/common/spanhandler.go +++ b/operator/controllers/common/spanhandler.go @@ -16,8 +16,13 @@ type ISpanHandler interface { UnbindSpan(reconcileObject client.Object, phase string) error } +type spanContext struct { + Span trace.Span + Ctx context.Context +} + type SpanHandler struct { - bindCRDSpan map[string]trace.Span + bindCRDSpan map[string]spanContext mtx sync.Mutex } @@ -26,14 +31,14 @@ func (r *SpanHandler) GetSpan(ctx context.Context, tracer trace.Tracer, reconcil if err != nil { return nil, nil, err } - appvName := piWrapper.GetSpanKey(phase) + spanKey := piWrapper.GetSpanKey(phase) r.mtx.Lock() defer r.mtx.Unlock() if r.bindCRDSpan == nil { - r.bindCRDSpan = make(map[string]trace.Span) + r.bindCRDSpan = make(map[string]spanContext) } - if span, ok := r.bindCRDSpan[appvName]; ok { - return ctx, span, nil + if span, ok := r.bindCRDSpan[spanKey]; ok { + return span.Ctx, span.Span, nil } spanName := piWrapper.GetSpanName(phase) childCtx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindConsumer)) @@ -43,7 +48,10 @@ func (r *SpanHandler) GetSpan(ctx context.Context, tracer trace.Tracer, reconcil otel.GetTextMapPropagator().Inject(childCtx, traceContextCarrier) piWrapper.SetPhaseTraceID(phase, traceContextCarrier) - r.bindCRDSpan[appvName] = span + r.bindCRDSpan[spanKey] = spanContext{ + Span: span, + Ctx: childCtx, + } return childCtx, span, nil } diff --git a/operator/controllers/keptnworkloadinstance/controller.go b/operator/controllers/keptnworkloadinstance/controller.go index 7fc35f5bf93..af27e91471d 100644 --- a/operator/controllers/keptnworkloadinstance/controller.go +++ b/operator/controllers/keptnworkloadinstance/controller.go @@ -159,7 +159,8 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr appTraceContextCarrier := propagation.MapCarrier(workloadInstance.Spec.TraceId) ctxAppTrace := otel.GetTextMapPropagator().Extract(context.TODO(), appTraceContextCarrier) - ctxWorkloadTrace, spanTrace, err := r.SpanHandler.GetSpan(ctxAppTrace, r.Tracer, workloadInstance, workloadInstance.Name) + // this will be the parent span for all phases of the WorkloadInstance + ctxWorkloadTrace, spanTrace, err := r.SpanHandler.GetSpan(ctxAppTrace, r.Tracer, workloadInstance, "") if err != nil { r.Log.Error(err, "could not get span") }