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

Fixes #4302.
This commit is contained in:
Kyle Carberry
2022-10-03 15:19:02 -05:00
committed by GitHub
parent aa3812ff4e
commit 9bc0d06aa0
2 changed files with 22 additions and 14 deletions

View File

@ -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
}
@ -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())),
)
@ -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
}

View File

@ -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 (
@ -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,
// 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)
}
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)
}
options.BinaryPath = execPath
options.BinaryPath = installTerraformExecPath
} else {
options.BinaryPath = absoluteBinary
}