@@ -32,6 +32,7 @@ import (
32
32
"github.com/argoproj/gitops-engine/pkg/sync/hook"
33
33
resourceutil "github.com/argoproj/gitops-engine/pkg/sync/resource"
34
34
kubeutil "github.com/argoproj/gitops-engine/pkg/utils/kube"
35
+ "github.com/argoproj/gitops-engine/pkg/utils/tracing"
35
36
)
36
37
37
38
type reconciledResource struct {
@@ -209,6 +210,8 @@ func NewSyncContext(
209
210
kubectl kubeutil.Kubectl ,
210
211
namespace string ,
211
212
openAPISchema openapi.Resources ,
213
+ syncTracer tracing.Tracer ,
214
+ syncTraceID , syncTraceRootSpanID string ,
212
215
opts ... SyncOpt ,
213
216
) (SyncContext , func (), error ) {
214
217
dynamicIf , err := dynamic .NewForConfig (restConfig )
@@ -246,6 +249,9 @@ func NewSyncContext(
246
249
permissionValidator : func (_ * unstructured.Unstructured , _ * metav1.APIResource ) error {
247
250
return nil
248
251
},
252
+ syncTracer : syncTracer ,
253
+ syncTraceID : syncTraceID ,
254
+ syncTraceRootSpanID : syncTraceRootSpanID ,
249
255
}
250
256
for _ , opt := range opts {
251
257
opt (ctx )
@@ -357,6 +363,11 @@ type syncContext struct {
357
363
// lock to protect concurrent updates of the result list
358
364
lock sync.Mutex
359
365
366
+ // tracer for tracing the sync operation
367
+ syncTraceID string
368
+ syncTraceRootSpanID string
369
+ syncTracer tracing.Tracer
370
+
360
371
// syncNamespace is a function that will determine if the managed
361
372
// namespace should be synced
362
373
syncNamespace func (* unstructured.Unstructured , * unstructured.Unstructured ) (bool , error )
@@ -1262,6 +1273,8 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
1262
1273
ss .Go (func (state runState ) runState {
1263
1274
logCtx := sc .log .WithValues ("dryRun" , dryRun , "task" , t )
1264
1275
logCtx .V (1 ).Info ("Pruning" )
1276
+ span := sc .syncTracer .StartSpanFromTraceParent ("pruneObject" , sc .syncTraceID , sc .syncTraceRootSpanID )
1277
+ defer span .Finish ()
1265
1278
result , message := sc .pruneObject (t .liveObj , sc .prune , dryRun )
1266
1279
if result == common .ResultCodeSyncFailed {
1267
1280
state = failed
@@ -1270,6 +1283,7 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
1270
1283
if ! dryRun || sc .dryRun || result == common .ResultCodeSyncFailed {
1271
1284
sc .setResourceResult (t , result , operationPhases [result ], message )
1272
1285
}
1286
+ sc .setBaggageItemForTasks (& span , t , message , result , operationPhases [result ])
1273
1287
return state
1274
1288
})
1275
1289
}
@@ -1289,19 +1303,27 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
1289
1303
ss .Go (func (state runState ) runState {
1290
1304
sc .log .WithValues ("dryRun" , dryRun , "task" , t ).V (1 ).Info ("Deleting" )
1291
1305
if ! dryRun {
1306
+ span := sc .syncTracer .StartSpanFromTraceParent ("hooksDeletion" , sc .syncTraceID , sc .syncTraceRootSpanID )
1307
+ defer span .Finish ()
1292
1308
err := sc .deleteResource (t )
1309
+ message := "deleted"
1310
+ operationPhase := common .OperationRunning
1293
1311
if err != nil {
1294
1312
// it is possible to get a race condition here, such that the resource does not exist when
1295
1313
// delete is requested, we treat this as a nop
1296
1314
if ! apierrors .IsNotFound (err ) {
1297
1315
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 )
1299
1319
}
1300
1320
} else {
1301
1321
// if there is anything that needs deleting, we are at best now in pending and
1302
1322
// want to return and wait for sync to be invoked again
1303
1323
state = pending
1324
+ operationPhase = common .OperationSucceeded
1304
1325
}
1326
+ sc .setBaggageItemForTasks (& span , t , message , "" , operationPhase )
1305
1327
}
1306
1328
return state
1307
1329
})
@@ -1330,6 +1352,24 @@ func (sc *syncContext) runTasks(tasks syncTasks, dryRun bool) runState {
1330
1352
return state
1331
1353
}
1332
1354
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
+
1333
1373
func (sc * syncContext ) processCreateTasks (state runState , tasks syncTasks , dryRun bool ) runState {
1334
1374
ss := newStateSync (state )
1335
1375
for _ , task := range tasks {
@@ -1341,11 +1381,14 @@ func (sc *syncContext) processCreateTasks(state runState, tasks syncTasks, dryRu
1341
1381
logCtx := sc .log .WithValues ("dryRun" , dryRun , "task" , t )
1342
1382
logCtx .V (1 ).Info ("Applying" )
1343
1383
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 ()
1344
1386
result , message := sc .applyObject (t , dryRun , validate )
1345
1387
if result == common .ResultCodeSyncFailed {
1346
1388
logCtx .WithValues ("message" , message ).Info ("Apply failed" )
1347
1389
state = failed
1348
1390
}
1391
+ var phase common.OperationPhase
1349
1392
if ! dryRun || sc .dryRun || result == common .ResultCodeSyncFailed {
1350
1393
phase := operationPhases [result ]
1351
1394
// 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
1355
1398
}
1356
1399
sc .setResourceResult (t , result , phase , message )
1357
1400
}
1401
+ sc .setBaggageItemForTasks (& span , t , message , result , phase )
1358
1402
return state
1359
1403
})
1360
1404
}
0 commit comments