Skip to content

Commit 25c320f

Browse files
authored
Improve logging statements’ accuracy and reporting of data points. (#44)
This changeset improves several logs by: - reporting their data points as structured data rather than phrase components - including the task ID (“PrimaryKey” in code) to facilitate correlation among logs and metadata - adding a few other data points and adding new log events such as task-finished
1 parent ab0ed50 commit 25c320f

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

internal/verifier/check.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,16 +370,30 @@ func FetchFailedAndIncompleteTasks(ctx context.Context, coll *mongo.Collection,
370370

371371
func (verifier *Verifier) Work(ctx context.Context, workerNum int, wg *sync.WaitGroup) {
372372
defer wg.Done()
373-
verifier.logger.Debug().Msgf("[Worker %d] Started", workerNum)
373+
374+
verifier.logger.Debug().
375+
Int("workerNum", workerNum).
376+
Msg("Worker started.")
377+
378+
defer verifier.logger.Debug().
379+
Int("workerNum", workerNum).
380+
Msg("Worker finished.")
381+
374382
for {
375383
select {
376384
case <-ctx.Done():
377385
return
378386
default:
379387
task, err := verifier.FindNextVerifyTaskAndUpdate()
380388
if errors.Is(err, mongo.ErrNoDocuments) {
381-
verifier.logger.Debug().Msgf("[Worker %d] No tasks found, sleeping...", workerNum)
382-
time.Sleep(verifier.workerSleepDelayMillis * time.Millisecond)
389+
duration := verifier.workerSleepDelayMillis * time.Millisecond
390+
391+
verifier.logger.Debug().
392+
Int("workerNum", workerNum).
393+
Stringer("duration", duration).
394+
Msg("No tasks found. Sleeping.")
395+
396+
time.Sleep(duration)
383397
continue
384398
} else if err != nil {
385399
panic(err)

internal/verifier/migration_verifier.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,11 @@ func (verifier *Verifier) getDocumentsCursor(ctx context.Context, collection *mo
459459
}
460460
}
461461
findCmd := append(bson.D{{"find", collection.Name()}}, findOptions...)
462-
verifier.logger.Debug().Msgf("getDocuments findCmd: %s opts: %v", findCmd, *runCommandOptions)
462+
verifier.logger.Debug().
463+
Interface("task", task.PrimaryKey).
464+
Str("findCmd", fmt.Sprintf("%s", findCmd)).
465+
Str("options", fmt.Sprintf("%v", *runCommandOptions)).
466+
Msg("getDocuments findCmd.")
463467

464468
return collection.Database().RunCommandCursor(ctx, findCmd, runCommandOptions)
465469
}
@@ -540,7 +544,22 @@ func (verifier *Verifier) compareOneDocument(srcClientDoc, dstClientDoc bson.Raw
540544
}
541545

542546
func (verifier *Verifier) ProcessVerifyTask(workerNum int, task *VerificationTask) {
543-
verifier.logger.Debug().Msgf("[Worker %d] Processing verify task", workerNum)
547+
start := time.Now()
548+
549+
verifier.logger.Debug().
550+
Int("workerNum", workerNum).
551+
Interface("task", task.PrimaryKey).
552+
Msg("Processing document comparison task.")
553+
554+
defer func() {
555+
elapsed := time.Since(start)
556+
557+
verifier.logger.Debug().
558+
Int("workerNum", workerNum).
559+
Interface("task", task.PrimaryKey).
560+
Stringer("timeElapsed", elapsed).
561+
Msg("Finished document comparison task.")
562+
}()
544563

545564
problems, docsCount, bytesCount, err := verifier.FetchAndCompareDocuments(
546565
context.Background(),
@@ -549,7 +568,11 @@ func (verifier *Verifier) ProcessVerifyTask(workerNum int, task *VerificationTas
549568

550569
if err != nil {
551570
task.Status = verificationTaskFailed
552-
verifier.logger.Error().Msgf("[Worker %d] Error comparing docs: %+v", workerNum, err)
571+
verifier.logger.Error().
572+
Err(err).
573+
Int("workerNum", workerNum).
574+
Interface("task", task.PrimaryKey).
575+
Msg("Failed to fetch and compare documents for document comparison task.")
553576
} else {
554577
task.SourceDocumentCount = docsCount
555578
task.SourceByteCount = bytesCount
@@ -560,10 +583,15 @@ func (verifier *Verifier) ProcessVerifyTask(workerNum int, task *VerificationTas
560583
task.Status = verificationTaskFailed
561584
// We know we won't change lastGeneration while verification tasks are running, so no mutex needed here.
562585
if verifier.lastGeneration {
563-
verifier.logger.Error().Msgf("[Worker %d] Verification Task %+v failed critical section and is a true error",
564-
workerNum, task.PrimaryKey)
586+
verifier.logger.Error().
587+
Int("workerNum", workerNum).
588+
Interface("task", task.PrimaryKey).
589+
Msg("Document comparison task failed critical section and is a true error.")
565590
} else {
566-
verifier.logger.Debug().Msgf("[Worker %d] Verification Task %+v failed, may pass next generation", workerNum, task.PrimaryKey)
591+
verifier.logger.Debug().
592+
Int("workerNum", workerNum).
593+
Interface("task", task.PrimaryKey).
594+
Msg("Document comparison task failed, but it may pass in the next generation.")
567595

568596
var mismatches []VerificationResult
569597
var missingIds []interface{}
@@ -594,15 +622,24 @@ func (verifier *Verifier) ProcessVerifyTask(workerNum int, task *VerificationTas
594622
// mismatched & missing docs.
595623
err := verifier.InsertFailedCompareRecheckDocs(task.QueryFilter.Namespace, idsToRecheck, dataSizes)
596624
if err != nil {
597-
verifier.logger.Error().Msgf("[Worker %d] Error inserting document mismatch into Recheck queue: %+v", workerNum, err)
625+
verifier.logger.Error().
626+
Err(err).
627+
Int("workerNum", workerNum).
628+
Interface("task", task.PrimaryKey).
629+
Int("rechecksCount", len(idsToRecheck)).
630+
Msg("Failed to enqueue rechecks after document mismatches.")
598631
}
599632
}
600633
}
601634
}
602635

603636
err = verifier.UpdateVerificationTask(task)
604637
if err != nil {
605-
verifier.logger.Error().Msgf("Failed updating verification status: %v", err)
638+
verifier.logger.Error().
639+
Err(err).
640+
Int("workerNum", workerNum).
641+
Interface("task", task.PrimaryKey).
642+
Msg("Failed to update task status.")
606643
}
607644
}
608645

0 commit comments

Comments
 (0)