Skip to content

Commit 6fa3669

Browse files
🔊 Tracing stage of the handling
1 parent 2a9d588 commit 6fa3669

1 file changed

Lines changed: 48 additions & 15 deletions

File tree

‎internal/handler/routes.go‎

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,7 @@ func purgeMyRecycleBin(c *gin.Context, files *service.FileService, bus *eventbus
15821582
// @Success 200 {object} map[string]any
15831583
// @Router /api/files/upload/create [post]
15841584
func createUploadTask(c *gin.Context, cfg *config.Config, files *service.FileService, tasks *service.TaskService, quota *service.QuotaService) {
1585+
startedAt := time.Now()
15851586
result, _, ok := auth.GetAuth(c)
15861587
if !ok {
15871588
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
@@ -1607,6 +1608,7 @@ func createUploadTask(c *gin.Context, cfg *config.Config, files *service.FileSer
16071608
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
16081609
return
16091610
}
1611+
parseDuration := time.Since(startedAt)
16101612
if req.FileSize <= 0 {
16111613
c.JSON(http.StatusBadRequest, gin.H{"error": "file_size must be greater than zero"})
16121614
return
@@ -1664,6 +1666,7 @@ func createUploadTask(c *gin.Context, cfg *config.Config, files *service.FileSer
16641666
poolMultiplier = *pool.BillingConfig.CostMultiplier
16651667
}
16661668
}
1669+
quotaStart := time.Now()
16671670
account, err := quota.EnrichedAccount(c.Request.Context(), result.Account)
16681671
if err != nil {
16691672
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
@@ -1683,22 +1686,29 @@ func createUploadTask(c *gin.Context, cfg *config.Config, files *service.FileSer
16831686
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
16841687
return
16851688
}
1689+
quotaDuration := time.Since(quotaStart)
16861690
chunks := int((req.FileSize + req.ChunkSize - 1) / req.ChunkSize)
1687-
logging.Log.Info().
1688-
Str("accountId", result.Account.GetId()).
1689-
Str("name", name).
1690-
Int64("fileSize", req.FileSize).
1691-
Int64("chunkSize", req.ChunkSize).
1692-
Int("chunks", chunks).
1693-
Str("contentType", req.ContentType).
1694-
Msg("creating upload task")
1691+
dbStart := time.Now()
16951692
payload := &database.PersistentTask{Description: req.Description, Hash: req.Hash, ExpiredAt: expiredAt, Usage: req.Usage, ParentID: req.ParentID, OverwriteID: req.OverwriteID, FastMode: req.FastMode, ApplicationType: req.ApplicationType, Indexed: req.Index}
16961693
task, err := tasks.CreateUploadTask(uuid.MustParse(result.Account.GetId()), name, payload, req.FileSize, resolvedPoolID, name, req.ContentType, req.ChunkSize, chunks)
16971694
if err != nil {
16981695
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
16991696
return
17001697
}
1698+
dbDuration := time.Since(dbStart)
17011699
_ = cfg
1700+
logging.Log.Info().
1701+
Str("accountId", result.Account.GetId()).
1702+
Str("name", name).
1703+
Int64("fileSize", req.FileSize).
1704+
Int64("chunkSize", req.ChunkSize).
1705+
Int("chunks", chunks).
1706+
Str("contentType", req.ContentType).
1707+
Dur("parseDuration", parseDuration).
1708+
Dur("quotaDuration", quotaDuration).
1709+
Dur("dbDuration", dbDuration).
1710+
Dur("totalDuration", time.Since(startedAt)).
1711+
Msg("upload task created")
17021712
c.JSON(http.StatusOK, gin.H{"task_id": task.TaskID, "chunk_size": task.ChunkSize, "chunks_count": task.ChunksCount})
17031713
}
17041714

@@ -1887,15 +1897,23 @@ func directUpload(c *gin.Context, cfg *config.Config, files *service.FileService
18871897
} else if createdFile.StorageKey != nil && strings.TrimSpace(*createdFile.StorageKey) != "" {
18881898
storageKey = strings.TrimSpace(*createdFile.StorageKey)
18891899
}
1900+
eventStart := time.Now()
18901901
if err := publishFileUploaded(c.Request.Context(), bus, dispatcher, eventbus.FileUploadedEvent{FileID: createdFile.ID, ContentType: contentType, StorageKey: storageKey, ProcessingFilePath: tempPath, IsTempFile: true}); err != nil {
18911902
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
18921903
return
18931904
}
1905+
eventDuration := time.Since(eventStart)
18941906
cleanupTempPath = false
1907+
logging.Log.Info().
1908+
Str("fileId", createdFile.ID).
1909+
Dur("eventDuration", eventDuration).
1910+
Dur("totalDuration", time.Since(startedAt)).
1911+
Msg("direct upload complete")
18951912
c.JSON(http.StatusOK, createdFile)
18961913
}
18971914

18981915
func uploadChunk(c *gin.Context, cfg *config.Config, tasks *service.TaskService) {
1916+
startedAt := time.Now()
18991917
result, _, ok := auth.GetAuth(c)
19001918
if !ok {
19011919
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
@@ -1923,19 +1941,26 @@ func uploadChunk(c *gin.Context, cfg *config.Config, tasks *service.TaskService)
19231941
return
19241942
}
19251943
defer reader.Close()
1944+
copyStart := time.Now()
19261945
if _, err := service.CopyStreamToChunk(cfg.Storage.TempDir, taskID, idx, reader); err != nil {
19271946
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
19281947
return
19291948
}
1949+
copyDuration := time.Since(copyStart)
1950+
dbStart := time.Now()
1951+
if err := tasks.UpdateUploadedChunk(taskID, idx); err != nil {
1952+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1953+
return
1954+
}
1955+
dbDuration := time.Since(dbStart)
19301956
logging.Log.Debug().
19311957
Str("taskId", taskID).
19321958
Int("chunkIndex", idx).
19331959
Int64("chunkSize", fileHeader.Size).
1960+
Dur("copyDuration", copyDuration).
1961+
Dur("dbDuration", dbDuration).
1962+
Dur("totalDuration", time.Since(startedAt)).
19341963
Msg("upload chunk staged")
1935-
if err := tasks.UpdateUploadedChunk(taskID, idx); err != nil {
1936-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
1937-
return
1938-
}
19391964
c.JSON(http.StatusOK, gin.H{"ok": true})
19401965
}
19411966

@@ -1966,6 +1991,7 @@ func completeUpload(c *gin.Context, cfg *config.Config, files *service.FileServi
19661991
c.JSON(http.StatusBadRequest, gin.H{"error": "task has no chunks"})
19671992
return
19681993
}
1994+
lookupDuration := time.Since(startedAt)
19691995
chunkDir := filepath.Join(cfg.Storage.TempDir, taskID)
19701996
mergedPath := filepath.Join(cfg.Storage.TempDir, taskID+".merged")
19711997
cleanupChunkDir := true
@@ -1980,15 +2006,18 @@ func completeUpload(c *gin.Context, cfg *config.Config, files *service.FileServi
19802006
_ = os.Remove(mergedPath)
19812007
}
19822008
}()
2009+
mergeStart := time.Now()
19832010
if err := files.MergeChunks(taskID, chunkDir, mergedPath, task.ChunksCount, nil); err != nil {
19842011
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
19852012
return
19862013
}
2014+
mergeDuration := time.Since(mergeStart)
19872015
logging.Log.Info().
19882016
Str("taskId", taskID).
19892017
Int("chunks", task.ChunksCount).
19902018
Str("mergedPath", mergedPath).
1991-
Dur("mergeDuration", time.Since(startedAt)).
2019+
Dur("lookupDuration", lookupDuration).
2020+
Dur("mergeDuration", mergeDuration).
19922021
Msg("upload chunks merged")
19932022
ctx := service.AccessContext{Account: result.Account, Session: result.Session}
19942023
_ = ctx
@@ -2067,16 +2096,20 @@ func completeUpload(c *gin.Context, cfg *config.Config, files *service.FileServi
20672096
if object != nil && strings.TrimSpace(object.MimeType) != "" {
20682097
contentType = object.MimeType
20692098
}
2099+
eventStart := time.Now()
20702100
if err := publishFileUploaded(c.Request.Context(), bus, dispatcher, eventbus.FileUploadedEvent{FileID: created.ID, TaskID: task.TaskID, ContentType: contentType, StorageKey: storageKey, ProcessingFilePath: mergedPath, IsTempFile: true}); err != nil {
20712101
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
20722102
return
20732103
}
2104+
eventDuration := time.Since(eventStart)
20742105
cleanupMergedPath = false
2106+
_ = tasks.MarkCompleted(task.TaskID)
20752107
logging.Log.Info().
20762108
Str("taskId", taskID).
20772109
Str("fileId", created.ID).
2078-
Msg("upload event published")
2079-
_ = tasks.MarkCompleted(task.TaskID)
2110+
Dur("eventDuration", eventDuration).
2111+
Dur("totalDuration", time.Since(startedAt)).
2112+
Msg("chunked upload complete")
20802113
c.JSON(http.StatusOK, created)
20812114
}
20822115

0 commit comments

Comments
 (0)