Skip to content

Commit

Permalink
[cmd/builder] Log output from go subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
mx-psi committed Oct 20, 2023
1 parent 287b98f commit 16fe19c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_logging-builder.yaml
Original file line number Diff line number Diff line change
@@ -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: []
34 changes: 19 additions & 15 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ import (
"time"

"go.uber.org/zap"
"go.uber.org/zap/zapio"
)

var (
// ErrGoNotFound is returned when a Go binary hasn't been found
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 {
Expand Down Expand Up @@ -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)))

Expand All @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 16fe19c

Please sign in to comment.