@@ -32,6 +32,7 @@ import (
3232 "github.com/argoproj/gitops-engine/pkg/sync/hook"
3333 resourceutil "github.com/argoproj/gitops-engine/pkg/sync/resource"
3434 kubeutil "github.com/argoproj/gitops-engine/pkg/utils/kube"
35+ "github.com/argoproj/gitops-engine/pkg/utils/tracing"
3536)
3637
3738type reconciledResource struct {
@@ -209,6 +210,8 @@ func NewSyncContext(
209210 kubectl kubeutil.Kubectl ,
210211 namespace string ,
211212 openAPISchema openapi.Resources ,
213+ syncTracer tracing.Tracer ,
214+ syncTraceID , syncTraceRootSpanID string ,
212215 opts ... SyncOpt ,
213216) (SyncContext , func (), error ) {
214217 dynamicIf , err := dynamic .NewForConfig (restConfig )
@@ -246,6 +249,9 @@ func NewSyncContext(
246249 permissionValidator : func (_ * unstructured.Unstructured , _ * metav1.APIResource ) error {
247250 return nil
248251 },
252+ syncTracer : syncTracer ,
253+ syncTraceID : syncTraceID ,
254+ syncTraceRootSpanID : syncTraceRootSpanID ,
249255 }
250256 for _ , opt := range opts {
251257 opt (ctx )
@@ -357,6 +363,11 @@ type syncContext struct {
357363 // lock to protect concurrent updates of the result list
358364 lock sync.Mutex
359365
366+ // tracer for tracing the sync operation
367+ syncTraceID string
368+ syncTraceRootSpanID string
369+ syncTracer tracing.Tracer
370+
360371 // syncNamespace is a function that will determine if the managed
361372 // namespace should be synced
362373 syncNamespace func (* unstructured.Unstructured , * unstructured.Unstructured ) (bool , error )
@@ -1262,6 +1273,8 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
12621273 ss .Go (func (state runState ) runState {
12631274 logCtx := sc .log .WithValues ("dryRun" , dryRun , "task" , t )
12641275 logCtx .V (1 ).Info ("Pruning" )
1276+ span := sc .syncTracer .StartSpanFromTraceParent ("pruneObject" , sc .syncTraceID , sc .syncTraceRootSpanID )
1277+ defer span .Finish ()
12651278 result , message := sc .pruneObject (t .liveObj , sc .prune , dryRun )
12661279 if result == common .ResultCodeSyncFailed {
12671280 state = failed
@@ -1270,6 +1283,7 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
12701283 if ! dryRun || sc .dryRun || result == common .ResultCodeSyncFailed {
12711284 sc .setResourceResult (t , result , operationPhases [result ], message )
12721285 }
1286+ sc .setBaggageItemForTasks (& span , t , message , result , operationPhases [result ])
12731287 return state
12741288 })
12751289 }
@@ -1289,19 +1303,27 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
12891303 ss .Go (func (state runState ) runState {
12901304 sc .log .WithValues ("dryRun" , dryRun , "task" , t ).V (1 ).Info ("Deleting" )
12911305 if ! dryRun {
1306+ span := sc .syncTracer .StartSpanFromTraceParent ("hooksDeletion" , sc .syncTraceID , sc .syncTraceRootSpanID )
1307+ defer span .Finish ()
12921308 err := sc .deleteResource (t )
1309+ message := "deleted"
1310+ operationPhase := common .OperationRunning
12931311 if err != nil {
12941312 // it is possible to get a race condition here, such that the resource does not exist when
12951313 // delete is requested, we treat this as a nop
12961314 if ! apierrors .IsNotFound (err ) {
12971315 state = failed
1298- sc .setResourceResult (t , "" , common .OperationError , fmt .Sprintf ("failed to delete resource: %v" , err ))
1316+ message = fmt .Sprintf ("failed to delete resource: %v" , err )
1317+ operationPhase = common .OperationError
1318+ sc .setResourceResult (t , "" , operationPhase , message )
12991319 }
13001320 } else {
13011321 // if there is anything that needs deleting, we are at best now in pending and
13021322 // want to return and wait for sync to be invoked again
13031323 state = pending
1324+ operationPhase = common .OperationSucceeded
13041325 }
1326+ sc .setBaggageItemForTasks (& span , t , message , "" , operationPhase )
13051327 }
13061328 return state
13071329 })
@@ -1330,6 +1352,24 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
13301352 return state
13311353}
13321354
1355+ func (sc * syncContext ) createSpan (operation string , dryrun bool ) tracing.Span {
1356+ // skip tracing if dryrun
1357+ if dryrun {
1358+ return tracing.NopTracer {}.StartSpan (operation )
1359+ }
1360+ return sc .syncTracer .StartSpanFromTraceParent (operation , sc .syncTraceID , sc .syncTraceRootSpanID )
1361+ }
1362+
1363+ func (sc * syncContext ) setBaggageItemForTasks (span * tracing.Span , t * syncTask , message string , result common.ResultCode , operationPhase common.OperationPhase ) {
1364+ resourceKey := t .resourceKey ()
1365+ (* span ).SetBaggageItem ("resource" , resourceKey .String ())
1366+ (* span ).SetBaggageItem ("result" , string (result ))
1367+ (* span ).SetBaggageItem ("operationPhase" , string (operationPhase ))
1368+ (* span ).SetBaggageItem ("message" , message )
1369+ (* span ).SetBaggageItem ("phase" , string (t .phase ))
1370+ (* span ).SetBaggageItem ("wave" , fmt .Sprint (t .wave ()))
1371+ }
1372+
13331373func (sc * syncContext ) processCreateTasks (state runState , tasks syncTasks , dryRun bool ) runState {
13341374 ss := newStateSync (state )
13351375 for _ , task := range tasks {
@@ -1341,11 +1381,14 @@ func (sc *syncContext) processCreateTasks(state runState, tasks syncTasks, dryRu
13411381 logCtx := sc .log .WithValues ("dryRun" , dryRun , "task" , t )
13421382 logCtx .V (1 ).Info ("Applying" )
13431383 validate := sc .validate && ! resourceutil .HasAnnotationOption (t .targetObj , common .AnnotationSyncOptions , common .SyncOptionsDisableValidation )
1384+ span := sc .syncTracer .StartSpanFromTraceParent ("applyObject" , sc .syncTraceID , sc .syncTraceRootSpanID )
1385+ defer span .Finish ()
13441386 result , message := sc .applyObject (t , dryRun , validate )
13451387 if result == common .ResultCodeSyncFailed {
13461388 logCtx .WithValues ("message" , message ).Info ("Apply failed" )
13471389 state = failed
13481390 }
1391+ var phase common.OperationPhase
13491392 if ! dryRun || sc .dryRun || result == common .ResultCodeSyncFailed {
13501393 phase := operationPhases [result ]
13511394 // no resources are created in dry-run, so running phase means validation was
@@ -1355,6 +1398,7 @@ func (sc *syncContext) processCreateTasks(state runState, tasks syncTasks, dryRu
13551398 }
13561399 sc .setResourceResult (t , result , phase , message )
13571400 }
1401+ sc .setBaggageItemForTasks (& span , t , message , result , phase )
13581402 return state
13591403 })
13601404 }
0 commit comments