Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate file info in log context #3405

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this method is only called via the Enterprise flow. Source-specific changes would presumably need to be made for each source.

return handlers.HandleFile(fileCtx, readCloser, &chunkSkel, reporter)
}

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