Skip to content

fix: Install Terraform once and only log >=500 #4339

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

Merged
merged 1 commit into from
Oct 3, 2022
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 coderd/httpmw/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
next.ServeHTTP(sw, r)

// Don't log successful health check requests.
if r.URL.Path == "/api/v2" && sw.Status == 200 {
if r.URL.Path == "/api/v2" && sw.Status == http.StatusOK {
return
}

Expand All @@ -37,7 +37,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {

// For status codes 400 and higher we
// want to log the response body.
if sw.Status >= 400 {
if sw.Status >= http.StatusInternalServerError {
httplog = httplog.With(
slog.F("response_body", string(sw.ResponseBody())),
)
Expand All @@ -47,7 +47,7 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
// includes proxy errors etc. It also causes slogtest to fail
// instantly without an error message by default.
logLevelFn := httplog.Debug
if sw.Status >= 400 {
if sw.Status >= http.StatusInternalServerError {
logLevelFn = httplog.Warn
}

Expand Down
30 changes: 19 additions & 11 deletions provisioner/terraform/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ var (
maxTerraformVersion = version.Must(version.NewVersion("1.3.0"))

terraformMinorVersionMismatch = xerrors.New("Terraform binary minor version mismatch.")

installTerraform sync.Once
installTerraformExecPath string
// nolint:errname
installTerraformError error
)

const (
Expand Down Expand Up @@ -93,18 +98,21 @@ func Serve(ctx context.Context, options *ServeOptions) error {
return xerrors.Errorf("absolute binary context canceled: %w", err)
}

installer := &releases.ExactVersion{
InstallDir: options.CachePath,
Product: product.Terraform,
Version: TerraformVersion,
}
installer.SetLogger(slog.Stdlib(ctx, options.Logger, slog.LevelDebug))
options.Logger.Info(ctx, "installing terraform", slog.F("dir", options.CachePath), slog.F("version", TerraformVersion))
execPath, err := installer.Install(ctx)
if err != nil {
return xerrors.Errorf("install terraform: %w", err)
// We don't want to install Terraform multiple times!
installTerraform.Do(func() {
installer := &releases.ExactVersion{
InstallDir: options.CachePath,
Product: product.Terraform,
Version: TerraformVersion,
}
installer.SetLogger(slog.Stdlib(ctx, options.Logger, slog.LevelDebug))
options.Logger.Debug(ctx, "installing terraform", slog.F("dir", options.CachePath), slog.F("version", TerraformVersion))
installTerraformExecPath, installTerraformError = installer.Install(ctx)
})
if installTerraformError != nil {
return xerrors.Errorf("install terraform: %w", installTerraformError)
}
options.BinaryPath = execPath
options.BinaryPath = installTerraformExecPath
Comment on lines +102 to +115
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we calling Serve more than once?

} else {
options.BinaryPath = absoluteBinary
}
Expand Down