Record debug evidence in one run-scoped capture session
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Package debugbundle owns explicitly requested per-run debug bundles.
|
||||
// Package debugbundle allocates explicitly requested per-run debug directories.
|
||||
package debugbundle
|
||||
|
||||
import (
|
||||
@@ -10,38 +10,54 @@ import (
|
||||
)
|
||||
|
||||
type Bundle struct {
|
||||
path, summaryRoot, traceRoot string
|
||||
createdAt time.Time
|
||||
path string
|
||||
createdAt time.Time
|
||||
}
|
||||
|
||||
// Allocate starts a capture session. A link failure returns the already-created
|
||||
// bundle as well as the error so callers can identify and retain available data.
|
||||
func Allocate(parent, runID string, startedAt time.Time) (*Bundle, error) {
|
||||
parent = strings.TrimSpace(parent)
|
||||
if parent == "" {
|
||||
return nil, fmt.Errorf("debug parent must not be empty")
|
||||
}
|
||||
if err := validateRunID(runID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parent, err := filepath.Abs(parent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve debug parent: %w", err)
|
||||
}
|
||||
if err := os.MkdirAll(parent, 0o700); err != nil {
|
||||
return nil, fmt.Errorf("create debug parent %q: %w", parent, err)
|
||||
}
|
||||
path := filepath.Join(parent, runID)
|
||||
if err := os.Mkdir(path, 0o700); err != nil {
|
||||
if os.IsExist(err) {
|
||||
return nil, fmt.Errorf("debug bundle %q already exists", path)
|
||||
}
|
||||
return nil, fmt.Errorf("create debug bundle %q: %w", path, err)
|
||||
directory := filepath.Join(parent, runID)
|
||||
if err := os.Mkdir(directory, 0o700); err != nil {
|
||||
return nil, fmt.Errorf("create debug bundle %q: %w", directory, err)
|
||||
}
|
||||
summary, trace := filepath.Join(path, "summary"), filepath.Join(path, "trace")
|
||||
if err := os.Mkdir(summary, 0o700); err != nil {
|
||||
_ = os.Remove(path)
|
||||
return nil, fmt.Errorf("create debug summary %q: %w", summary, err)
|
||||
bundle := &Bundle{path: directory, createdAt: startedAt}
|
||||
if err := updateLatest(parent, runID); err != nil {
|
||||
return bundle, fmt.Errorf("update debug latest for %q: %w", directory, err)
|
||||
}
|
||||
if err := os.Mkdir(trace, 0o700); err != nil {
|
||||
_ = os.RemoveAll(path)
|
||||
return nil, fmt.Errorf("create debug trace %q: %w", trace, err)
|
||||
return bundle, nil
|
||||
}
|
||||
|
||||
func updateLatest(parent, runID string) error {
|
||||
temporary, err := os.CreateTemp(parent, ".latest-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return &Bundle{path: path, summaryRoot: summary, traceRoot: trace, createdAt: startedAt}, nil
|
||||
name := temporary.Name()
|
||||
defer os.Remove(name)
|
||||
if err := temporary.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Remove(name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Symlink(runID, name); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(name, filepath.Join(parent, "latest"))
|
||||
}
|
||||
|
||||
func validateRunID(runID string) error {
|
||||
@@ -59,18 +75,6 @@ func (b *Bundle) Path() string {
|
||||
}
|
||||
return b.path
|
||||
}
|
||||
func (b *Bundle) SummaryRoot() string {
|
||||
if b == nil {
|
||||
return ""
|
||||
}
|
||||
return b.summaryRoot
|
||||
}
|
||||
func (b *Bundle) TraceRoot() string {
|
||||
if b == nil {
|
||||
return ""
|
||||
}
|
||||
return b.traceRoot
|
||||
}
|
||||
func (b *Bundle) RunID() string {
|
||||
if b == nil {
|
||||
return ""
|
||||
@@ -83,9 +87,3 @@ func (b *Bundle) CreatedAt() time.Time {
|
||||
}
|
||||
return b.createdAt
|
||||
}
|
||||
func (b *Bundle) Summary() *SummaryWriter {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
return &SummaryWriter{root: b.summaryRoot, runID: b.RunID(), createdAt: b.createdAt}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user