Skip to content

feat: update slog to use logfmt #7477

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 9 commits into from
May 14, 2023
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
13 changes: 12 additions & 1 deletion cli/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"testing"

"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -33,8 +35,17 @@ var updateGoldenFiles = flag.Bool("update", false, "update .golden files")

var timestampRegex = regexp.MustCompile(`(?i)\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d+)?Z`)

// We need to override the global color profile.
//
//nolint:tparallel,paralleltest
func TestCommandHelp(t *testing.T) {
t.Parallel()
ogColorProfile := lipgloss.ColorProfile()
// ANSI256 escape codes are far easier for humans to parse in a diff,
// but TrueColor is probably more popular with modern terminals.
lipgloss.SetColorProfile(termenv.ANSI)
t.Cleanup(func() {
lipgloss.SetColorProfile(ogColorProfile)
})
rootClient, replacements := prepareTestData(t)

type testCase struct {
Expand Down
4 changes: 2 additions & 2 deletions cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Usage: coder [global-flags] <subcommand>
Coder v0.0.0-devel — A tool for provisioning self-hosted development environments with Terraform.
- Start a Coder server:

$ coder server
 $ coder server 

- Get started by creating a template from an example:

$ coder templates init
 $ coder templates init 

Subcommands
config-ssh Add an SSH Host entry for your workspaces "ssh
Expand Down
4 changes: 2 additions & 2 deletions cli/testdata/coder_config-ssh_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Add an SSH Host entry for your workspaces "ssh coder.workspace"
- You can use -o (or --ssh-option) so set SSH options to be used for all your
workspaces:

$ coder config-ssh -o ForwardAgent=yes
 $ coder config-ssh -o ForwardAgent=yes 

- You can use --dry-run (or -n) to see the changes that would be made:

$ coder config-ssh --dry-run
 $ coder config-ssh --dry-run 

Options
-n, --dry-run bool, $CODER_SSH_DRY_RUN
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_dotfiles_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Personalize your workspace by applying a canonical dotfiles repository

- Check out and install a dotfiles repository without prompts:

$ coder dotfiles --yes git@github.com:example/dotfiles.git
 $ coder dotfiles --yes git@github.com:example/dotfiles.git 

Options
--symlink-dir string, $CODER_SYMLINK_DIR
Expand Down
8 changes: 4 additions & 4 deletions cli/testdata/coder_port-forward_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ Aliases: tunnel
- Port forward a single TCP port from 1234 in the workspace to port 5678 on
your local machine:

$ coder port-forward <workspace> --tcp 5678:1234
 $ coder port-forward <workspace> --tcp 5678:1234 

- Port forward a single UDP port from port 9000 to port 9000 on your local
machine:

$ coder port-forward <workspace> --udp 9000
 $ coder port-forward <workspace> --udp 9000 

- Port forward multiple TCP ports and a UDP port:

$ coder port-forward <workspace> --tcp 8080:8080 --tcp 9000:3000 --udp 5353:53
 $ coder port-forward <workspace> --tcp 8080:8080 --tcp 9000:3000 --udp 5353:53 

- Port forward multiple ports (TCP or UDP) in condensed syntax:

$ coder port-forward <workspace> --tcp 8080,9000:3000,9090-9092,10000-10002:10010-10012
 $ coder port-forward <workspace> --tcp 8080,9000:3000,9090-9092,10000-10002:10010-10012 

Options
-p, --tcp string-array, $CODER_PORT_FORWARD_TCP
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_schedule_override-stop_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Override the stop time of a currently running workspace instance.
* The new stop time must be at least 30 minutes in the future.
* The workspace template may restrict the maximum workspace runtime.

$ coder schedule override-stop my-workspace 90m
 $ coder schedule override-stop my-workspace 90m 

---
Run `coder --help` for a list of global options.
2 changes: 1 addition & 1 deletion cli/testdata/coder_schedule_start_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Schedule format: <start-time> [day-of-week] [location].

- Set the workspace to start at 9:30am (in Dublin) from Monday to Friday:

$ coder schedule start my-workspace 9:30AM Mon-Fri Europe/Dublin
 $ coder schedule start my-workspace 9:30AM Mon-Fri Europe/Dublin 

---
Run `coder --help` for a list of global options.
2 changes: 1 addition & 1 deletion cli/testdata/coder_schedule_stop_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ When enabling scheduled stop, enter a duration in one of the following formats:
* 2m (2 minutes)
* 2 (2 minutes)

$ coder schedule stop my-workspace 2h30m
 $ coder schedule stop my-workspace 2h30m 

---
Run `coder --help` for a list of global options.
6 changes: 3 additions & 3 deletions cli/testdata/coder_templates_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Aliases: template
Templates are written in standard Terraform and describe the infrastructure for workspaces
- Create a template for developers to create workspaces:

$ coder templates create
 $ coder templates create 

- Make changes to your template, and plan the changes:

$ coder templates plan my-template
 $ coder templates plan my-template 

- Push an update to the template. Your developers can update their workspaces:

$ coder templates push my-template
 $ coder templates push my-template 

Subcommands
create Create a template from the current directory or as specified by
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_templates_versions_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Aliases: version

- List versions of a specific template:

$ coder templates versions list my-template
 $ coder templates versions list my-template 

Subcommands
list List all the versions of the specified template
Expand Down
6 changes: 3 additions & 3 deletions cli/testdata/coder_tokens_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Aliases: token
Tokens are used to authenticate automated clients to Coder.
- Create a token for automation:

$ coder tokens create
 $ coder tokens create 

- List your tokens:

$ coder tokens ls
 $ coder tokens ls 

- Remove a token by ID:

$ coder tokens rm WuoWs4ZsMX
 $ coder tokens rm WuoWs4ZsMX 

Subcommands
create Create a token
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_users_activate_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ platform

Aliases: active

$ coder users activate example_user
 $ coder users activate example_user 

Options
-c, --column string-array (default: username,email,created_at,status)
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_users_show_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage: coder users show [flags] <username|user_id|'me'>

Show a single user. Use 'me' to indicate the currently authenticated user.

$ coder users show me
 $ coder users show me 

Options
-o, --output string (default: table)
Expand Down
2 changes: 1 addition & 1 deletion cli/testdata/coder_users_suspend_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ platform

Aliases: rm, delete

$ coder users suspend example_user
 $ coder users suspend example_user 

Options
-c, --column string-array (default: username,email,created_at,status)
Expand Down
14 changes: 12 additions & 2 deletions cli/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@ import (
"strings"
"testing"

"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/testutil"
)

// We need to override the global color profile to test escape codes.
//
//nolint:tparallel,paralleltest
func TestVersion(t *testing.T) {
t.Parallel()
ogColorProfile := lipgloss.ColorProfile()
lipgloss.SetColorProfile(termenv.ANSI)
t.Cleanup(func() {
lipgloss.SetColorProfile(ogColorProfile)
})

expectedText := `Coder v0.0.0-devel
https://github.com/coder/coder

Full build of Coder, supports the server subcommand.
Full build of Coder, supports the  server  subcommand.
`
expectedJSON := `{
"version": "v0.0.0-devel",
Expand Down
7 changes: 6 additions & 1 deletion coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,12 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
}
api.publishWorkspaceUpdate(ctx, build.WorkspaceID)

api.Logger.Info(ctx, "accepting agent", slog.F("agent", workspaceAgent))
api.Logger.Info(ctx, "accepting agent",
slog.F("owner", owner.Username),
slog.F("workspace", workspace.Name),
slog.F("name", workspaceAgent.Name),
)
api.Logger.Debug(ctx, "accepting agent details", slog.F("agent", workspaceAgent))

defer conn.Close(websocket.StatusNormalClosure, "")

Expand Down
12 changes: 8 additions & 4 deletions enterprise/audit/backends/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ func (slogBackend) Decision() audit.FilterDecision {
}

func (b slogBackend) Export(ctx context.Context, alog database.AuditLog) error {
m := structs.Map(alog)
fields := make([]slog.Field, 0, len(m))
for k, v := range m {
fields = append(fields, slog.F(k, v))
// We don't use structs.Map because we don't want to recursively convert
// fields into maps. When we keep the type information, slog can more
// pleasantly format the output. For example, the clean result of
// (*NullString).Value() may be printed instead of {String: "foo", Valid: true}.
sfs := structs.Fields(alog)
var fields []slog.Field
for _, sf := range sfs {
fields = append(fields, slog.F(sf.Name(), sf.Value()))
}

b.log.Info(ctx, "audit_log", fields...)
Expand Down
23 changes: 13 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ replace github.com/gliderlabs/ssh => github.com/coder/ssh v0.0.0-20230421140225-
replace github.com/imulab/go-scim/pkg/v2 => github.com/coder/go-scim/pkg/v2 v2.0.0-20230221055123-1d63c1222136

require (
cdr.dev/slog v1.4.2
cdr.dev/slog v1.5.3
cloud.google.com/go/compute/metadata v0.2.3
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
Expand All @@ -67,7 +67,9 @@ require (
github.com/cenkalti/backoff/v4 v4.2.0
github.com/charmbracelet/charm v0.12.4
github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/lipgloss v0.6.0
// In later at least v0.7.1, lipgloss changes its terminal detection
// which breaks most of our CLI golden files tests.
github.com/charmbracelet/lipgloss v0.7.1
github.com/cli/safeexec v1.0.0
github.com/codeclysm/extract v2.2.0+incompatible
github.com/coder/flog v1.1.0
Expand All @@ -86,7 +88,7 @@ require (
github.com/gen2brain/beeep v0.0.0-20220402123239-6a3042f4b71a
github.com/gliderlabs/ssh v0.3.4
github.com/go-chi/chi v1.5.4
github.com/go-chi/chi/v5 v5.0.7
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/httprate v0.7.1
github.com/go-chi/render v1.0.1
github.com/go-jose/go-jose/v3 v3.0.0
Expand Down Expand Up @@ -116,7 +118,7 @@ require (
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/klauspost/compress v1.16.3
github.com/lib/pq v1.10.6
github.com/mattn/go-isatty v0.0.17
github.com/mattn/go-isatty v0.0.18
github.com/mitchellh/go-wordwrap v1.0.1
github.com/mitchellh/mapstructure v1.5.0
github.com/moby/moby v20.10.23+incompatible
Expand Down Expand Up @@ -158,11 +160,11 @@ require (
golang.org/x/mod v0.10.0
golang.org/x/oauth2 v0.5.0
golang.org/x/sync v0.1.0
golang.org/x/sys v0.7.0
golang.org/x/sys v0.8.0
golang.org/x/term v0.7.0
golang.org/x/tools v0.6.0
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
golang.zx2c4.com/wireguard v0.0.0-20230223181233-21636207a675
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b
google.golang.org/api v0.108.0
google.golang.org/grpc v1.54.0
google.golang.org/protobuf v1.28.2-0.20230118093459-a9481185b34d
Expand All @@ -174,9 +176,11 @@ require (
tailscale.com v1.32.2
)

require github.com/go-chi/cors v1.2.1

require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/go-chi/cors v1.2.1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
)

require (
Expand All @@ -197,7 +201,6 @@ require (
github.com/ammario/tlru v0.3.0
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bep/godartsass v0.16.0 // indirect
Expand Down Expand Up @@ -293,7 +296,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/muesli/ansi v0.0.0-20221106050444-61f0cd9a192a // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.14.0 // indirect
github.com/muesli/termenv v0.15.1
github.com/niklasfasching/go-org v1.6.5 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
Expand All @@ -306,7 +309,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand Down
Loading