Skip to content
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

Improve logging. Add warning when command times out #22

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions pkg/healthcheck/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func HealthCheck(ctx context.Context, data *HttpHealthArgs, diag *diag.Diagnosti

result := window.Do(func(attempt int, successes int) bool {
if successes != 0 {
tflog.Trace(ctx, fmt.Sprintf("SUCCESS [%d/%d] http %s %s", successes, data.ConsecutiveSuccesses, data.Method, endpoint))
tflog.Debug(ctx, fmt.Sprintf("SUCCESS [%d/%d] http %s %s", successes, data.ConsecutiveSuccesses, data.Method, endpoint))
} else {
tflog.Trace(ctx, fmt.Sprintf("ATTEMPT #%d http %s %s", attempt, data.Method, endpoint))
tflog.Debug(ctx, fmt.Sprintf("ATTEMPT #%d http %s %s", attempt, data.Method, endpoint))
}

httpResponse, err := client.Do(&http.Request{
Expand All @@ -144,7 +144,7 @@ func HealthCheck(ctx context.Context, data *HttpHealthArgs, diag *diag.Diagnosti
multierror.Append(err, fmt.Errorf("check status code: %w", err))
}
if success {
tflog.Trace(ctx, fmt.Sprintf("SUCCESS CODE %d", httpResponse.StatusCode))
tflog.Debug(ctx, fmt.Sprintf("SUCCESS CODE %d", httpResponse.StatusCode))
body, err := io.ReadAll(httpResponse.Body)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR READING BODY %v", err))
Expand All @@ -154,7 +154,7 @@ func HealthCheck(ctx context.Context, data *HttpHealthArgs, diag *diag.Diagnosti
data.ResultBody = string(body)
}
} else {
tflog.Trace(ctx, fmt.Sprintf("FAILURE CODE %d", httpResponse.StatusCode))
tflog.Debug(ctx, fmt.Sprintf("FAILURE CODE %d", httpResponse.StatusCode))
}

// Check JSONPath
Expand Down
19 changes: 14 additions & 5 deletions pkg/provider/resource_local_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package provider
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -244,27 +245,35 @@ func (r *LocalCommandResource) RunCommand(ctx context.Context, data *LocalComman
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = append(os.Environ(), env...)
cmd.Cancel = func() error {
tflog.Warn(ctx, fmt.Sprintf("The command timed out after %d milliseconds", data.CommandTimeout.ValueInt64()))
diag.AddWarning("Command timeout", fmt.Sprintf("The command timed out after %d milliseconds", data.CommandTimeout.ValueInt64()))
return fmt.Errorf("command timed out after %d milliseconds", data.CommandTimeout.ValueInt64())
}

err := cmd.Start()
if err != nil {
tflog.Trace(ctx, fmt.Sprintf("ATTEMPT #%d error starting command", attempt))
tflog.Debug(ctx, fmt.Sprintf("ATTEMPT #%d error starting command", attempt))
tflog.Error(ctx, fmt.Sprintf("Error starting command %v", err))
return false
}
err = cmd.Wait()
if err != nil {
tflog.Trace(ctx, fmt.Sprintf("ATTEMPT #%d exit_code=%d", attempt, err.(*exec.ExitError).ExitCode()))
if errors.Is(err, &exec.ExitError{}) {
e := err.(*exec.ExitError)
tflog.Debug(ctx, fmt.Sprintf("ATTEMPT #%d exit_code=%d stderr=%q", attempt, e.ExitCode(), string(e.Stderr)))
}
data.Stdout = types.StringValue(stdout.String())
data.Stderr = types.StringValue(stderr.String())
tflog.Debug(ctx, fmt.Sprintf("Command stdout: %s", stdout.String()))
tflog.Debug(ctx, fmt.Sprintf("Command stdout: %s", stderr.String()))
tflog.Debug(ctx, fmt.Sprintf("Command stderr: %s", stderr.String()))
return false
}
tflog.Trace(ctx, fmt.Sprintf("SUCCESS [%d/%d]", successes, data.ConsecutiveSuccesses.ValueInt64()))
tflog.Debug(ctx, fmt.Sprintf("SUCCESS [%d/%d]", successes, data.ConsecutiveSuccesses.ValueInt64()))
data.Stdout = types.StringValue(stdout.String())
data.Stderr = types.StringValue(stderr.String())
tflog.Debug(ctx, fmt.Sprintf("Command stdout: %s", stdout.String()))
tflog.Debug(ctx, fmt.Sprintf("Command stdout: %s", stderr.String()))
tflog.Debug(ctx, fmt.Sprintf("Command stderr: %s", stderr.String()))
return true
})

Expand Down
Loading