diff --git a/.chloggen/mx-psi_logging-builder.yaml b/.chloggen/mx-psi_logging-builder.yaml new file mode 100755 index 00000000000..e4ac2fa2747 --- /dev/null +++ b/.chloggen/mx-psi_logging-builder.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: cmd/builder + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Add logging for `go` subcommands that are ran as part of a build" + +# One or more tracking issues or pull requests related to the change +issues: [8715] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/cmd/builder/internal/builder/main.go b/cmd/builder/internal/builder/main.go index 0267b071c52..94b918983c2 100644 --- a/cmd/builder/internal/builder/main.go +++ b/cmd/builder/internal/builder/main.go @@ -13,6 +13,7 @@ import ( "time" "go.uber.org/zap" + "go.uber.org/zap/zapio" ) var ( @@ -20,6 +21,18 @@ var ( ErrGoNotFound = errors.New("go binary not found") ) +func runGoCommand(cfg Config, args ...string) error { + // #nosec G204 -- cfg.Distribution.Go is trusted to be a safe path and the caller is assumed to have carried out necessary input validation + cfg.Logger.Info("Running go subcommand.", zap.Any("arguments", args)) + cmd := exec.Command(cfg.Distribution.Go, args...) + cmd.Dir = cfg.Distribution.OutputPath + writer := &zapio.Writer{Log: cfg.Logger} + defer func() { _ = writer.Close() }() + cmd.Stdout = writer + cmd.Stderr = writer + return cmd.Run() +} + // GenerateAndCompile will generate the source files based on the given configuration, update go mod, and will compile into a binary func GenerateAndCompile(cfg Config) error { if err := Generate(cfg); err != nil { @@ -93,11 +106,8 @@ func Compile(cfg Config) error { if cfg.Distribution.BuildTags != "" { args = append(args, "-tags", cfg.Distribution.BuildTags) } - // #nosec G204 -- cfg.Distribution.Go is trusted to be a safe path and the caller is assumed to have carried out necessary input validation - cmd := exec.Command(cfg.Distribution.Go, args...) - cmd.Dir = cfg.Distribution.OutputPath - if out, err := cmd.CombinedOutput(); err != nil { - return fmt.Errorf("failed to compile the OpenTelemetry Collector distribution: %w. Output:\n%s", err, out) + if err := runGoCommand(cfg, args...); err != nil { + return fmt.Errorf("failed to compile the OpenTelemetry Collector distribution: %w", err) } cfg.Logger.Info("Compiled", zap.String("binary", fmt.Sprintf("%s/%s", cfg.Distribution.OutputPath, cfg.Distribution.Name))) @@ -111,11 +121,8 @@ func GetModules(cfg Config) error { return nil } - // #nosec G204 -- cfg.Distribution.Go is trusted to be a safe path - cmd := exec.Command(cfg.Distribution.Go, "mod", "tidy", "-compat=1.20") - cmd.Dir = cfg.Distribution.OutputPath - if out, err := cmd.CombinedOutput(); err != nil { - return fmt.Errorf("failed to update go.mod: %w. Output:\n%s", err, out) + if err := runGoCommand(cfg, "mod", "tidy", "-compat=1.20"); err != nil { + return fmt.Errorf("failed to update go.mod: %w", err) } cfg.Logger.Info("Getting go modules") @@ -124,11 +131,8 @@ func GetModules(cfg Config) error { retries := 3 failReason := "unknown" for i := 1; i <= retries; i++ { - // #nosec G204 - cmd := exec.Command(cfg.Distribution.Go, "mod", "download") - cmd.Dir = cfg.Distribution.OutputPath - if out, err := cmd.CombinedOutput(); err != nil { - failReason = fmt.Sprintf("%s. Output:\n%s", err, out) + if err := runGoCommand(cfg, "mod", "download"); err != nil { + failReason = err.Error() cfg.Logger.Info("Failed modules download", zap.String("retry", fmt.Sprintf("%d/%d", i, retries))) time.Sleep(5 * time.Second) continue