Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e531c09

Browse files
authored
fix: Write agent logs to "/tmp/coder-agent.log" for debugging (#1239)
It was difficult to obtain logs for the agent if it failed to start for some reason. Now they'll go to a consistent spot!
1 parent c2b5009 commit e531c09

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

agent/agent.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"os/exec"
1414
"os/user"
15+
"path/filepath"
1516
"runtime"
1617
"strconv"
1718
"strings"
@@ -21,7 +22,6 @@ import (
2122
"github.com/armon/circbuf"
2223
"github.com/google/uuid"
2324

24-
gsyslog "github.com/hashicorp/go-syslog"
2525
"go.uber.org/atomic"
2626

2727
"cdr.dev/slog"
@@ -166,15 +166,9 @@ func (*agent) runStartupScript(ctx context.Context, script string) error {
166166
return xerrors.Errorf("get user shell: %w", err)
167167
}
168168

169-
var writer io.WriteCloser
170-
// Attempt to use the syslog to write startup information.
171-
writer, err = gsyslog.NewLogger(gsyslog.LOG_INFO, "USER", "coder-startup-script")
169+
writer, err := os.OpenFile(filepath.Join(os.TempDir(), "coder-startup-script.log"), os.O_CREATE|os.O_RDWR, 0600)
172170
if err != nil {
173-
// If the syslog isn't supported or cannot be created, use a text file in temp.
174-
writer, err = os.CreateTemp("", "coder-startup-script-*.txt")
175-
if err != nil {
176-
return xerrors.Errorf("open startup script log file: %w", err)
177-
}
171+
return xerrors.Errorf("open startup script log file: %w", err)
178172
}
179173
defer func() {
180174
_ = writer.Close()

cli/agent.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"net/http"
66
"net/url"
7+
"os"
8+
"path/filepath"
79
"time"
810

911
"cloud.google.com/go/compute/metadata"
@@ -17,6 +19,8 @@ import (
1719
"github.com/coder/coder/cli/cliflag"
1820
"github.com/coder/coder/codersdk"
1921
"github.com/coder/retry"
22+
23+
"gopkg.in/natefinch/lumberjack.v2"
2024
)
2125

2226
func workspaceAgent() *cobra.Command {
@@ -36,7 +40,13 @@ func workspaceAgent() *cobra.Command {
3640
if err != nil {
3741
return xerrors.Errorf("parse %q: %w", rawURL, err)
3842
}
39-
logger := slog.Make(sloghuman.Sink(cmd.OutOrStdout())).Leveled(slog.LevelDebug)
43+
44+
logWriter := &lumberjack.Logger{
45+
Filename: filepath.Join(os.TempDir(), "coder-agent.log"),
46+
MaxSize: 5, // MB
47+
}
48+
defer logWriter.Close()
49+
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
4050
client := codersdk.New(coderURL)
4151

4252
// exchangeToken returns a session token.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ require (
6767
github.com/golang-migrate/migrate/v4 v4.15.2
6868
github.com/google/go-github/v43 v43.0.1-0.20220414155304-00e42332e405
6969
github.com/google/uuid v1.3.0
70-
github.com/hashicorp/go-syslog v1.0.0
7170
github.com/hashicorp/go-version v1.4.0
7271
github.com/hashicorp/hc-install v0.3.1
7372
github.com/hashicorp/hcl/v2 v2.12.0
@@ -115,6 +114,7 @@ require (
115114
google.golang.org/api v0.75.0
116115
google.golang.org/protobuf v1.28.0
117116
gopkg.in/DataDog/dd-trace-go.v1 v1.38.0
117+
gopkg.in/natefinch/lumberjack.v2 v2.0.0
118118
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
119119
nhooyr.io/websocket v1.8.7
120120
storj.io/drpc v0.0.30

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbt
9797
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
9898
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28=
9999
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
100+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
100101
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
101102
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
102103
github.com/ClickHouse/clickhouse-go v1.4.3/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
@@ -927,7 +928,6 @@ github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa
927928
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
928929
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
929930
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
930-
github.com/hashicorp/go-syslog v1.0.0 h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE=
931931
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
932932
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
933933
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
@@ -2516,6 +2516,7 @@ gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
25162516
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
25172517
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
25182518
gopkg.in/jinzhu/gorm.v1 v1.9.1/go.mod h1:56JJPUzbikvTVnoyP1nppSkbJ2L8sunqTBDY2fDrmFg=
2519+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
25192520
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
25202521
gopkg.in/olivere/elastic.v3 v3.0.75/go.mod h1:yDEuSnrM51Pc8dM5ov7U8aI/ToR3PG0llA8aRv2qmw0=
25212522
gopkg.in/olivere/elastic.v5 v5.0.84/go.mod h1:LXF6q9XNBxpMqrcgax95C6xyARXWbbCXUrtTxrNrxJI=

0 commit comments

Comments
 (0)