Skip to content

feat: Enable workspace debug logging #6838

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 18 commits into from
Mar 30, 2023
Merged
19 changes: 19 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func (server *Server) AcquireJob(ctx context.Context, _ *proto.Empty) (*proto.Ac
TemplateName: template.Name,
TemplateVersion: templateVersion.Name,
},
LogLevel: input.LogLevel,
},
}
case database.ProvisionerJobTypeTemplateVersionDryRun:
Expand Down Expand Up @@ -1550,6 +1551,7 @@ type TemplateVersionImportJob struct {
type WorkspaceProvisionJob struct {
WorkspaceBuildID uuid.UUID `json:"workspace_build_id"`
DryRun bool `json:"dry_run"`
LogLevel string `json:"log_level,omitempty"`
}

// TemplateVersionDryRunJob is the payload for the "template_version_dry_run" job type.
Expand Down
1 change: 1 addition & 0 deletions coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
workspaceBuildID := uuid.New()
input, err := json.Marshal(provisionerdserver.WorkspaceProvisionJob{
WorkspaceBuildID: workspaceBuildID,
LogLevel: string(createBuild.LogLevel),
})
if err != nil {
return xerrors.Errorf("marshal provision job: %w", err)
Expand Down
8 changes: 8 additions & 0 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ type WorkspacesResponse struct {
Count int `json:"count"`
}

type ProvisionerLogLevel string

const (
ProvisionerLogLevelDebug ProvisionerLogLevel = "debug"
)

// CreateWorkspaceBuildRequest provides options to update the latest workspace build.
type CreateWorkspaceBuildRequest struct {
TemplateVersionID uuid.UUID `json:"template_version_id,omitempty" format:"uuid"`
Expand All @@ -59,6 +65,8 @@ type CreateWorkspaceBuildRequest struct {
// This will not delete old params not included in this list.
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
RichParameterValues []WorkspaceBuildParameter `json:"rich_parameter_values,omitempty"`

LogLevel ProvisionerLogLevel `json:"log_level,omitempty" validate:"omitempty,oneof=debug"`
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Add a comment so this gets documented in the API doc

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

}

type WorkspaceOptions struct {
Expand Down
1 change: 1 addition & 0 deletions docs/api/builds.md
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ curl -X POST http://coder-server:8080/api/v2/workspaces/{workspace}/builds \
```json
{
"dry_run": true,
"log_level": "debug",
"orphan": true,
"parameter_values": [
{
Expand Down
17 changes: 17 additions & 0 deletions docs/api/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
```json
{
"dry_run": true,
"log_level": "debug",
"orphan": true,
"parameter_values": [
{
Expand Down Expand Up @@ -1490,6 +1491,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
| Name | Type | Required | Restrictions | Description |
| ----------------------- | ----------------------------------------------------------------------------- | -------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dry_run` | boolean | false | | |
| `log_level` | [codersdk.ProvisionerLogLevel](#codersdkprovisionerloglevel) | false | | |
| `orphan` | boolean | false | | Orphan may be set for the Destroy transition. |
| `parameter_values` | array of [codersdk.CreateParameterRequest](#codersdkcreateparameterrequest) | false | | Parameter values are optional. It will write params to the 'workspace' scope. This will overwrite any existing parameters with the same name. This will not delete old params not included in this list. |
| `rich_parameter_values` | array of [codersdk.WorkspaceBuildParameter](#codersdkworkspacebuildparameter) | false | | |
Expand All @@ -1501,6 +1503,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a

| Property | Value |
| ------------ | -------- |
| `log_level` | `debug` |
| `transition` | `create` |
| `transition` | `start` |
| `transition` | `stop` |
Expand Down Expand Up @@ -3251,6 +3254,20 @@ Parameter represents a set value for the scope.
| `canceled` |
| `failed` |

## codersdk.ProvisionerLogLevel

```json
"debug"
```

### Properties

#### Enumerated Values

| Value |
| ------- |
| `debug` |

## codersdk.ProvisionerStorageMethod

```json
Expand Down
13 changes: 10 additions & 3 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *server) Provision(stream proto.DRPCProvisioner_ProvisionStream) error {
return xerrors.Errorf("initialize terraform: %w", err)
}
s.logger.Debug(ctx, "ran initialization")
env, err := provisionEnv(config, request.GetPlan().GetParameterValues(), request.GetPlan().GetRichParameterValues(), request.GetPlan().GetGitAuthProviders())
env, err := provisionEnv(config, request.GetPlan().GetParameterValues(), request.GetPlan().GetRichParameterValues(), request.GetPlan().GetGitAuthProviders(), config.ProvisionerLogLevel)
if err != nil {
return err
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func planVars(plan *proto.Provision_Plan) ([]string, error) {
return vars, nil
}

func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue, richParams []*proto.RichParameterValue, gitAuth []*proto.GitAuthProvider) ([]string, error) {
func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue, richParams []*proto.RichParameterValue, gitAuth []*proto.GitAuthProvider, logLevel string) ([]string, error) {
env := safeEnviron()
env = append(env,
"CODER_AGENT_URL="+config.Metadata.CoderUrl,
Expand Down Expand Up @@ -236,7 +236,14 @@ func provisionEnv(config *proto.Provision_Config, params []*proto.ParameterValue
for _, gitAuth := range gitAuth {
env = append(env, provider.GitAuthAccessTokenEnvironmentVariable(gitAuth.Id)+"="+gitAuth.AccessToken)
}
// FIXME env = append(env, "TF_LOG=JSON")

if logLevel == "debug" {
// TF_LOG=JSON enables all kind of logging: trace-debug-info-warn-error.
// Coder shows info-warn-error by default, so we need to filter "trace" log entries,
// on the upper layer.
// The idea behind using TF_LOG=JSON instead of TF_LOG=debug is ensuring the proper log format.
env = append(env, "TF_LOG=JSON")
}
return env, nil
}

Expand Down
Loading