Skip to content

Commit 2319e07

Browse files
committed
Better logs with context extraction
1 parent c2dac8f commit 2319e07

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

go/cmd/gitter/gitter.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func ArchiveRepo(ctx context.Context, url string) ([]byte, error) {
249249
// We update if archive does not exist OR if it is older than the last fetch
250250
stats, err := os.Stat(archivePath)
251251
if os.IsNotExist(err) || (err == nil && stats.ModTime().Before(accessTime)) {
252-
logger.InfoContext(ctx, "Archiving git blob", slog.String("url", ctx.Value(urlKey).(string)))
252+
logger.InfoContext(ctx, "Archiving git blob")
253253
// Archive
254254
// tar --zstd -cf <archivePath> -C "<gitStorePath>/<repoDirName>" .
255255
// using -C to archive the relative path so it unzips nicely
@@ -359,7 +359,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
359359

360360
ctx := context.WithValue(r.Context(), urlKey, url)
361361

362-
logger.InfoContext(ctx, "Received request", slog.Bool("forceUpdate", forceUpdate), slog.String("remoteAddr", r.RemoteAddr))
362+
logger.InfoContext(ctx, "Received request: /git", slog.Bool("forceUpdate", forceUpdate), slog.String("remoteAddr", r.RemoteAddr))
363363
// If request came from a local ip, don't do the check
364364
if !isLocalRequest(r) {
365365
// Check if url starts with protocols: http(s)://, git://, ssh://, (s)ftp://
@@ -402,7 +402,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
402402
return ArchiveRepo(ctx, url)
403403
})
404404
if err != nil {
405-
logger.ErrorContext(ctx, "Error archiving blob", slog.String("url", url), slog.Any("error", err))
405+
logger.ErrorContext(ctx, "Error archiving blob", slog.Any("error", err))
406406
http.Error(w, fmt.Sprintf("Error archiving blob: %v", err), http.StatusInternalServerError)
407407

408408
return
@@ -419,7 +419,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
419419
return
420420
}
421421

422-
logger.InfoContext(ctx, "Request completed successfully", slog.String("url", url))
422+
logger.InfoContext(ctx, "Request completed successfully: /git")
423423
}
424424

425425
func cacheHandler(w http.ResponseWriter, r *http.Request) {
@@ -439,7 +439,7 @@ func cacheHandler(w http.ResponseWriter, r *http.Request) {
439439

440440
url := body.URL
441441
ctx := context.WithValue(r.Context(), urlKey, url)
442-
logger.InfoContext(ctx, "Received request: /cache", slog.String("url", url))
442+
logger.InfoContext(ctx, "Received request: /cache")
443443

444444
semaphore <- struct{}{}
445445
defer func() { <-semaphore }()
@@ -450,7 +450,7 @@ func cacheHandler(w http.ResponseWriter, r *http.Request) {
450450
if _, err, _ := gFetch.Do(url, func() (any, error) {
451451
return nil, FetchRepo(ctx, url, body.ForceUpdate)
452452
}); err != nil {
453-
logger.ErrorContext(ctx, "Error fetching blob", slog.String("url", url), slog.Any("error", err))
453+
logger.ErrorContext(ctx, "Error fetching blob", slog.Any("error", err))
454454
if isAuthError(err) {
455455
http.Error(w, fmt.Sprintf("Error fetching blob: %v", err), http.StatusForbidden)
456456

@@ -473,12 +473,12 @@ func cacheHandler(w http.ResponseWriter, r *http.Request) {
473473
return LoadRepository(ctx, repoPath)
474474
})
475475
if err != nil {
476-
logger.ErrorContext(ctx, "Failed to load repository", slog.String("url", url), slog.Any("error", err))
476+
logger.ErrorContext(ctx, "Failed to load repository", slog.Any("error", err))
477477
http.Error(w, fmt.Sprintf("Failed to load repository: %v", err), http.StatusInternalServerError)
478478

479479
return
480480
}
481481

482482
w.WriteHeader(http.StatusOK)
483-
logger.InfoContext(ctx, "Request completed successfully: /cache", slog.String("url", url), slog.Duration("duration", time.Since(start)))
483+
logger.InfoContext(ctx, "Request completed successfully: /cache", slog.Duration("duration", time.Since(start)))
484484
}

go/cmd/gitter/repository.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ func LoadRepository(ctx context.Context, repoPath string) (*Repository, error) {
7070
// Load cache pb file of the repo if exist
7171
if c, err := loadRepositoryCache(cachePath); err == nil {
7272
cache = c
73-
logger.InfoContext(ctx, "Loaded repository cache", slog.String("url", ctx.Value(urlKey).(string)), slog.Int("commits", len(cache.GetCommits())))
73+
logger.InfoContext(ctx, "Loaded repository cache", slog.Int("commits", len(cache.GetCommits())))
7474
} else {
7575
if errors.Is(err, os.ErrNotExist) {
7676
// It's fine if cache doesn't exist, log it just in case
77-
logger.InfoContext(ctx, "No repository cache found", slog.String("url", ctx.Value(urlKey).(string)))
77+
logger.InfoContext(ctx, "No repository cache found")
7878
} else {
7979
return nil, fmt.Errorf("failed to load repository cache: %w", err)
8080
}
@@ -94,7 +94,7 @@ func LoadRepository(ctx context.Context, repoPath string) (*Repository, error) {
9494

9595
// Save cache
9696
if err := saveRepositoryCache(cachePath, repo); err != nil {
97-
logger.ErrorContext(ctx, "Failed to save repository cache", slog.String("url", ctx.Value(urlKey).(string)), slog.Any("err", err))
97+
logger.ErrorContext(ctx, "Failed to save repository cache", slog.Any("err", err))
9898
}
9999

100100
return repo, nil
@@ -104,7 +104,7 @@ func LoadRepository(ctx context.Context, repoPath string) (*Repository, error) {
104104
// Returns a list of new commit hashes that don't have cached Patch IDs.
105105
// The new commit list is in reverse chronological order based on commit date (the default for git log).
106106
func (r *Repository) buildCommitGraph(ctx context.Context, cache *pb.RepositoryCache) ([]SHA1, error) {
107-
logger.InfoContext(ctx, "Starting graph construction", slog.String("url", ctx.Value(urlKey).(string)))
107+
logger.InfoContext(ctx, "Starting graph construction")
108108
start := time.Now()
109109

110110
// Build cache map
@@ -174,7 +174,7 @@ func (r *Repository) buildCommitGraph(ctx context.Context, cache *pb.RepositoryC
174174
for _, parent := range parents {
175175
hash, err := hex.DecodeString(parent)
176176
if err != nil {
177-
logger.ErrorContext(ctx, "Failed to decode hash", slog.String("url", ctx.Value(urlKey).(string)), slog.String("parent", parent), slog.Any("err", err))
177+
logger.ErrorContext(ctx, "Failed to decode hash", slog.String("parent", parent), slog.Any("err", err))
178178
continue
179179
}
180180
parentHashes = append(parentHashes, SHA1(hash))
@@ -184,13 +184,13 @@ func (r *Repository) buildCommitGraph(ctx context.Context, cache *pb.RepositoryC
184184
case 1:
185185
hash, err := hex.DecodeString(commitInfo[0])
186186
if err != nil {
187-
logger.ErrorContext(ctx, "Failed to decode hash", slog.String("url", ctx.Value(urlKey).(string)), slog.String("child", commitInfo[0]), slog.Any("err", err))
187+
logger.ErrorContext(ctx, "Failed to decode hash", slog.String("child", commitInfo[0]), slog.Any("err", err))
188188
continue
189189
}
190190
childHash = SHA1(hash)
191191
default:
192192
// No line should be completely empty (doesn't even have a commit hash) so error
193-
logger.ErrorContext(ctx, "Invalid commit info", slog.String("url", ctx.Value(urlKey).(string)), slog.String("line", line))
193+
logger.ErrorContext(ctx, "Invalid commit info", slog.String("line", line))
194194
continue
195195
}
196196

@@ -223,15 +223,15 @@ func (r *Repository) buildCommitGraph(ctx context.Context, cache *pb.RepositoryC
223223
}
224224
}
225225

226-
logger.InfoContext(ctx, "Commit graph completed", slog.String("url", ctx.Value(urlKey).(string)), slog.Int("new_commits", len(newCommits)), slog.Duration("duration", time.Since(start)))
226+
logger.InfoContext(ctx, "Commit graph completed", slog.Int("new_commits", len(newCommits)), slog.Duration("duration", time.Since(start)))
227227

228228
return newCommits, nil
229229
}
230230

231231
// calculatePatchIDs calculates patch IDs only for the specific commits provided.
232232
// Commits should be passed in order if possible. Processing linear commits sequentially improves performance slightly (in the 'git show' commands).
233233
func (r *Repository) calculatePatchIDs(ctx context.Context, commits []SHA1) error {
234-
logger.InfoContext(ctx, "Starting patch ID calculation", slog.String("url", ctx.Value(urlKey).(string)))
234+
logger.InfoContext(ctx, "Starting patch ID calculation")
235235
start := time.Now()
236236

237237
// Number of workers
@@ -261,7 +261,7 @@ func (r *Repository) calculatePatchIDs(ctx context.Context, commits []SHA1) erro
261261
return fmt.Errorf("failed to calculate patch IDs: %w", err)
262262
}
263263

264-
logger.InfoContext(ctx, "Patch ID calculation completed", slog.String("url", ctx.Value(urlKey).(string)), slog.Int("commits", len(commits)), slog.Duration("duration", time.Since(start)))
264+
logger.InfoContext(ctx, "Patch ID calculation completed", slog.Int("commits", len(commits)), slog.Duration("duration", time.Since(start)))
265265

266266
return nil
267267
}

0 commit comments

Comments
 (0)