@@ -140,6 +140,8 @@ func New(
140140 logger .InfoContext (ctx , "Startup fetch completed for existing repo" , "upstream" , repo .UpstreamURL (),
141141 "duration" , time .Since (start ))
142142
143+ recordCloneMetrics (ctx , "local" , time .Since (start ), 0 )
144+
143145 postRefs , err := repo .GetLocalRefs (ctx )
144146 if err != nil {
145147 logger .WarnContext (ctx , "Failed to get post-fetch refs for existing repo" , "upstream" , repo .UpstreamURL (),
@@ -469,8 +471,10 @@ func (s *Strategy) startClone(ctx context.Context, repo *gitclone.Repository) {
469471
470472 logger .InfoContext (ctx , "Attempting mirror snapshot restore" , "upstream" , upstream )
471473
472- if err := s .tryRestoreSnapshot (ctx , repo ); err != nil {
473- logger .InfoContext (ctx , "Mirror snapshot restore failed, falling back to clone" , "upstream" , upstream , "error" , err )
474+ cloneStart := time .Now ()
475+ restoreResult , restoreErr := s .tryRestoreSnapshot (ctx , repo )
476+ if restoreErr != nil {
477+ logger .InfoContext (ctx , "Mirror snapshot restore failed, falling back to clone" , "upstream" , upstream , "error" , restoreErr )
474478 } else {
475479 logger .InfoContext (ctx , "Mirror snapshot restored, fetching to freshen" , "upstream" , upstream )
476480
@@ -500,6 +504,9 @@ func (s *Strategy) startClone(ctx context.Context, repo *gitclone.Repository) {
500504
501505 logger .InfoContext (ctx , "Post-restore fetch completed, serving" , "upstream" , upstream )
502506
507+ recordCloneSuccess (ctx , "mirror" )
508+ recordCloneMetrics (ctx , "mirror" , time .Since (cloneStart ), restoreResult .BytesRead )
509+
503510 if s .config .SnapshotInterval > 0 {
504511 s .scheduleSnapshotJobs (repo )
505512 }
@@ -522,11 +529,15 @@ func (s *Strategy) startClone(ctx context.Context, repo *gitclone.Repository) {
522529
523530 if err != nil {
524531 logger .ErrorContext (ctx , "Clone failed" , "upstream" , upstream , "error" , err )
532+ recordCloneFailure (ctx , "upstream" , err )
525533 return
526534 }
527535
528536 logger .InfoContext (ctx , "Clone completed" , "upstream" , upstream , "path" , repo .Path ())
529537
538+ recordCloneSuccess (ctx , "upstream" )
539+ recordCloneMetrics (ctx , "upstream" , time .Since (cloneStart ), 0 )
540+
530541 if s .config .SnapshotInterval > 0 {
531542 s .scheduleSnapshotJobs (repo )
532543 }
@@ -539,28 +550,30 @@ func (s *Strategy) startClone(ctx context.Context, repo *gitclone.Repository) {
539550// Mirror snapshots are bare repositories that can be extracted and used directly
540551// without any conversion. On failure the repo path is cleaned up so the caller
541552// can fall back to clone.
542- func (s * Strategy ) tryRestoreSnapshot (ctx context.Context , repo * gitclone.Repository ) error {
553+ func (s * Strategy ) tryRestoreSnapshot (ctx context.Context , repo * gitclone.Repository ) ( * snapshot. RestoreResult , error ) {
543554 cacheKey := mirrorSnapshotCacheKey (repo .UpstreamURL ())
544555
545556 if err := os .MkdirAll (filepath .Dir (repo .Path ()), 0o750 ); err != nil {
546- return errors .Wrap (err , "create parent directory for restore" )
557+ return nil , errors .Wrap (err , "create parent directory for restore" )
547558 }
548559
549560 logger := logging .FromContext (ctx )
550561
551- if err := snapshot .Restore (ctx , s .cache , cacheKey , repo .Path (), s .config .ZstdThreads ); err != nil {
562+ result , err := snapshot .Restore (ctx , s .cache , cacheKey , repo .Path (), s .config .ZstdThreads )
563+ if err != nil {
552564 _ = os .RemoveAll (repo .Path ())
553- return errors .Wrap (err , "restore mirror snapshot" )
565+ return nil , errors .Wrap (err , "restore mirror snapshot" )
554566 }
555- logger .InfoContext (ctx , "Mirror snapshot extracted" , "upstream" , repo .UpstreamURL (), "path" , repo .Path ())
567+ logger .InfoContext (ctx , "Mirror snapshot extracted" , "upstream" , repo .UpstreamURL (), "path" , repo .Path (),
568+ "bytes_read" , result .BytesRead , "duration_ms" , result .Duration .Milliseconds ())
556569
557570 if err := repo .MarkRestored (ctx ); err != nil {
558571 _ = os .RemoveAll (repo .Path ())
559- return errors .Wrap (err , "mark restored" )
572+ return nil , errors .Wrap (err , "mark restored" )
560573 }
561574 logger .InfoContext (ctx , "Repository marked as restored" , "upstream" , repo .UpstreamURL (), "state" , repo .State ())
562575
563- return nil
576+ return result , nil
564577}
565578
566579func (s * Strategy ) maybeBackgroundFetch (repo * gitclone.Repository ) {
0 commit comments