Skip to content

Commit

Permalink
fixed propagation of span contexts
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <[email protected]>
  • Loading branch information
bacherfl authored and odubajDT committed Nov 22, 2022
1 parent cd2f8cb commit a155945
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions operator/api/v1alpha1/keptnworkloadinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions operator/api/v1alpha1/tests/keptnworkloadinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
20 changes: 14 additions & 6 deletions operator/controllers/common/spanhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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))
Expand All @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit a155945

Please sign in to comment.