Skip to content

Commit

Permalink
feat: propagate file info in log context
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz committed Oct 14, 2024
1 parent cf54b71 commit fef37cd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
6 changes: 3 additions & 3 deletions pkg/sources/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (s *Source) scanDir(ctx context.Context, path string, chunksChan chan *sour
var skipSymlinkErr = errors.New("skipping symlink")

func (s *Source) scanFile(ctx context.Context, path string, chunksChan chan *sources.Chunk) error {
logger := ctx.Logger().WithValues("path", path)
fileCtx := context.WithValues(ctx, "path", path)
fileStat, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("unable to stat file: %w", err)
Expand All @@ -168,7 +168,7 @@ func (s *Source) scanFile(ctx context.Context, path string, chunksChan chan *sou
}
defer inputFile.Close()

logger.V(3).Info("scanning file")
fileCtx.Logger().V(3).Info("scanning file")

chunkSkel := &sources.Chunk{
SourceType: s.Type(),
Expand All @@ -185,7 +185,7 @@ func (s *Source) scanFile(ctx context.Context, path string, chunksChan chan *sou
Verify: s.verify,
}

return handlers.HandleFile(ctx, inputFile, chunkSkel, sources.ChanReporter{Ch: chunksChan})
return handlers.HandleFile(fileCtx, inputFile, chunkSkel, sources.ChanReporter{Ch: chunksChan})
}

// Enumerate implements SourceUnitEnumerator interface. This implementation simply
Expand Down
31 changes: 9 additions & 22 deletions pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,8 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
logger.Error(
err,
"error handling binary file",
"filename", fileName,
"commit", commitHash,
"file", diff.PathB,
"path", fileName,
)
}
continue
Expand All @@ -677,21 +676,19 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
if err != nil {
ctx.Logger().Error(
err, "error creating reader for commits",
"filename", fileName,
"commit", fullHash,
"file", diff.PathB,
"path", fileName,
)
return nil
}
defer reader.Close()

data := make([]byte, d.Len())
if _, err := io.ReadFull(reader, data); err != nil {
ctx.Logger().Error(
logger.Error(
err, "error reading diff content for commit",
"filename", fileName,
"commit", fullHash,
"file", diff.PathB,
"path", fileName,
)
return nil
}
Expand Down Expand Up @@ -829,7 +826,7 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
)
for diff := range diffChan {
fullHash := diff.Commit.Hash
logger := ctx.Logger().WithValues("filename", diff.PathB, "commit", fullHash, "file", diff.PathB)
logger := ctx.Logger().WithValues("commit", fullHash, "path", diff.PathB)
logger.V(2).Info("scanning staged changes from git")

if scanOptions.MaxDepth > 0 && depth >= scanOptions.MaxDepth {
Expand Down Expand Up @@ -877,7 +874,7 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,
Verify: s.verify,
}
if err := s.handleBinary(ctx, gitDir, reporter, chunkSkel, commitHash, fileName); err != nil {
logger.Error(err, "error handling binary file", "filename", fileName)
logger.Error(err, "error handling binary file")
}
continue
}
Expand All @@ -887,24 +884,14 @@ func (s *Git) ScanStaged(ctx context.Context, repo *git.Repository, path string,

reader, err := d.ReadCloser()
if err != nil {
ctx.Logger().Error(
err, "error creating reader for staged",
"filename", fileName,
"commit", fullHash,
"file", diff.PathB,
)
logger.Error(err, "error creating reader for staged")
return nil
}
defer reader.Close()

data := make([]byte, d.Len())
if _, err := reader.Read(data); err != nil {
ctx.Logger().Error(
err, "error reading diff content for staged",
"filename", fileName,
"commit", fullHash,
"file", diff.PathB,
)
logger.Error(err, "error reading diff content for staged")
return nil
}
chunk := sources.Chunk{
Expand Down Expand Up @@ -1255,7 +1242,7 @@ func (s *Git) handleBinary(ctx context.Context, gitDir string, reporter sources.

defer func() { _ = cmd.Wait() }()

return handlers.HandleFile(ctx, stdout, chunkSkel, reporter, handlers.WithSkipArchives(s.skipArchives))
return handlers.HandleFile(fileCtx, stdout, chunkSkel, reporter, handlers.WithSkipArchives(s.skipArchives))
}

func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) error {
Expand Down
6 changes: 4 additions & 2 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1492,8 +1492,10 @@ func (s *Source) scanTarget(ctx context.Context, target sources.ChunkingTarget,
SourceMetadata: &source_metadatapb.MetaData{
Data: &source_metadatapb.MetaData_Github{Github: meta},
},
Verify: s.verify}
return handlers.HandleFile(ctx, readCloser, &chunkSkel, reporter)
Verify: s.verify,
}
fileCtx := context.WithValues(ctx, "path", meta.GetFile())
return handlers.HandleFile(fileCtx, readCloser, &chunkSkel, reporter)
}

func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporter sources.ChunkReporter) error {
Expand Down

0 comments on commit fef37cd

Please sign in to comment.