Skip to content

Commit

Permalink
Add traceContext to pipelinerun status
Browse files Browse the repository at this point in the history
Signed-off-by: Jayadeep KM <kmjayadeep@gmail.com>
  • Loading branch information
kmjayadeep authored and tekton-robot committed Jan 23, 2023
1 parent 1664f65 commit bcbe2ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ type PipelineRunStatusFields struct {
// Provenance contains some key authenticated metadata about how a software artifact was built (what sources, what inputs/outputs, etc.).
// +optional
Provenance *Provenance `json:"provenance,omitempty"`

// SpanContext contains tracing span context fields
SpanContext map[string]string `json:"spanContext,omitempty"`
}

// SkippedTask is used to describe the Tasks that were skipped due to their When Expressions
Expand Down
54 changes: 24 additions & 30 deletions pkg/reconciler/pipelinerun/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,46 +42,40 @@ const (
// spanContext is propogated through annotations in the CR
func initTracing(ctx context.Context, tracerProvider trace.TracerProvider, pr *v1beta1.PipelineRun) context.Context {
logger := logging.FromContext(ctx)
carrier := make(map[string]string)

pro := otel.GetTextMapPropagator()
prAnnotations := pr.Annotations
if prAnnotations == nil {
prAnnotations = make(map[string]string)
}

// if spanContext is not present in the CR, create new parent trace and
// add the marshalled spancontext to the annotations
if _, e := prAnnotations[SpanContextAnnotation]; !e {
ctxWithTrace, span := tracerProvider.Tracer(TracerName).Start(ctx, "PipelineRun:Reconciler")
defer span.End()
span.SetAttributes(attribute.String("pipeline", pr.Name), attribute.String("namespace", pr.Namespace))
// SpanContext was created already
if pr.Status.SpanContext != nil && len(pr.Status.SpanContext) > 0 {
return pro.Extract(ctx, propagation.MapCarrier(pr.Status.SpanContext))
}

if !span.SpanContext().HasTraceID() {
logger.Debug("tracerProvider doesn't provide a traceId, tracing is disabled")
return ctx
}
pr.Status.SpanContext = make(map[string]string)

marshalled, err := getMarshalledSpanFromContext(ctxWithTrace)
// SpanContext was propogated through annotations
if pr.Annotations != nil && pr.Annotations[SpanContextAnnotation] != "" {
err := json.Unmarshal([]byte(pr.Annotations[SpanContextAnnotation]), &pr.Status.SpanContext)
if err != nil {
logger.Error("Unable to marshal spancontext, err: %s", err)
return ctx
logger.Error("unable to unmarshal spancontext, err: %s", err)
}
logger.Debug("adding spancontext", marshalled)
prAnnotations[SpanContextAnnotation] = marshalled
pr.Annotations = prAnnotations
span.AddEvent("updating TaskRun CR with SpanContext annotations")
return ctxWithTrace
}

err := json.Unmarshal([]byte(pr.Annotations[SpanContextAnnotation]), &carrier)
if err != nil {
logger.Error("unable to unmarshal spancontext, err: %s", err)
return pro.Extract(ctx, propagation.MapCarrier(pr.Status.SpanContext))
}

logger.Debug("got traceContext %s", carrier)
// Create a new root span since there was no parent spanContext provided through annotations
ctxWithTrace, span := tracerProvider.Tracer(TracerName).Start(ctx, "PipelineRun:Reconciler")
defer span.End()
span.SetAttributes(attribute.String("taskrun", pr.Name), attribute.String("namespace", pr.Namespace))

pro.Inject(ctxWithTrace, propagation.MapCarrier(pr.Status.SpanContext))

logger.Debug("got tracing carrier", pr.Status.SpanContext)
if len(pr.Status.SpanContext) == 0 {
logger.Debug("tracerProvider doesn't provide a traceId, tracing is disabled")
return ctx
}

return pro.Extract(ctx, propagation.MapCarrier(carrier))
span.AddEvent("updating PipelineRun status with SpanContext")
return ctxWithTrace
}

// Extract spanContext from the context and return it as json encoded string
Expand Down

0 comments on commit bcbe2ea

Please sign in to comment.