feat: propagate file info in log context (#3405)

This commit is contained in:
Richard Gomez
2024-10-14 17:13:39 -07:00
committed by GitHub
parent e6281ca641
commit 34e443adcf
3 changed files with 16 additions and 27 deletions
+3 -3
View File
@@ -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)
@@ -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(),
@@ -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
+9 -22
View File
@@ -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
@@ -677,9 +676,8 @@ 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
}
@@ -687,11 +685,10 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path string
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
}
@@ -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 {
@@ -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
}
@@ -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{
@@ -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 {
+4 -2
View File
@@ -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 {