Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

feat: write agent output to a temporary file #422

Merged
merged 3 commits into from
Sep 2, 2021

Conversation

jawnsy
Copy link
Contributor

@jawnsy jawnsy commented Sep 2, 2021

How I tested this

Running the agent, the log data is written to a file as well (and truncated/re-created on subsequent starts):

$ go run ./cmd/coder/main.go agent start
2021-09-02 01:36:32.847 [INFO]  <agent.go:90>   starting wsnet listener {"coder_access_url": "https://master.cdr.dev/"}
2021-09-02 01:36:32.848 [INFO]  <listen.go:114> connecting to broker    {"broker_url": "wss://master.cdr.dev/api/private/envagent/listen?service_token=xxx"}
2021-09-02 01:36:32.928 [INFO]  <listen.go:136> broker connection established
^C2021-09-02 01:36:53.420 [INFO]        <agent.go:96>   closing wsnet listener
2021-09-02 01:36:53.421 [INFO]  <listen.go:451> listener closed

$ cat /tmp/coder-agent.log 
2021-09-02 15:22:25.006 [INFO]  <agent.go:92>   starting wsnet listener {"coder_access_url": "https://master.cdr.dev/"}
2021-09-02 15:22:25.007 [INFO]  <listen.go:114> connecting to broker    {"broker_url": "wss://master.cdr.dev/api/private/envagent/listen?service_token=xxx"}
2021-09-02 15:22:25.098 [INFO]  <listen.go:136> broker connection established
2021-09-02 15:22:26.690 [INFO]  <agent.go:98>   closing wsnet listener
2021-09-02 15:22:26.690 [INFO]  <listen.go:451> listener closed

If there's a permission error preventing the file frmo being truncated, a log message will be output to stderr (simulated by chowning the file to root):

$ sudo chown root:root /tmp/coder-agent.log 
$ go run ./cmd/coder/main.go agent start
2021-09-02 01:41:21.363 [INFO]  <agent.go:65>   failed to open agent log file   {"error": "open /tmp/coder-agent.log: permission denied"}
2021-09-02 01:41:21.365 [INFO]  <agent.go:93>   starting wsnet listener {"coder_access_url": "https://master.cdr.dev/"}
2021-09-02 01:41:21.365 [INFO]  <listen.go:114> connecting to broker    {"broker_url": "wss://master.cdr.dev/api/private/envagent/listen?service_token=xxx"}
2021-09-02 01:41:21.478 [INFO]  <listen.go:136> broker connection established
^C2021-09-02 01:41:23.116 [INFO]        <agent.go:99>   closing wsnet listener
2021-09-02 01:41:23.116 [INFO]  <listen.go:451> listener closed

@shortcut-integration
Copy link

@jawnsy jawnsy self-assigned this Sep 2, 2021
@coveralls
Copy link

coveralls commented Sep 2, 2021

Pull Request Test Coverage Report for Build 1194765666

  • 0 of 13 (0.0%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.04%) to 47.954%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/cmd/agent.go 0 13 0.0%
Files with Coverage Reduction New Missed Lines %
wsnet/dial.go 2 78.34%
Totals Coverage Status
Change from base Build 1163231010: -0.04%
Covered Lines: 2789
Relevant Lines: 5816

💛 - Coveralls

@jawnsy jawnsy requested a review from deansheather September 2, 2021 01:43
@jawnsy jawnsy marked this pull request as ready for review September 2, 2021 01:44
sloghuman.Sink(os.Stderr),
}

file, err := os.OpenFile(filepath.Join(os.TempDir(), "coder-agent.log"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

it didn't seem like the file we write needs to be hidden, so I made it a not-dotfile

Copy link
Member

Choose a reason for hiding this comment

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

Should be consistent for the other log files in that case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which other log files do we have? I think just a code-server one right?

Copy link
Member

Choose a reason for hiding this comment

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

code-server and envagent, they're in environment_stages.go


file, err := os.OpenFile(filepath.Join(os.TempDir(), "coder-agent.log"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err == nil && file != nil {
sinks = append(sinks, slogjson.Sink(file))
Copy link
Member

Choose a reason for hiding this comment

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

slogjson is very hard to read, and we don't have automated ingestion of this log file into stackdriver so I'd rather stick with sloghuman for now. We can change this later with no consequences

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have tools for consuming slogjson output and converting to sloghuman output? The slogjson output seems to have more info (e.g. full paths to files) and being machine-readable does seem potentially useful.

I don't have strong feelings about it though, I'll change to sloghuman 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's the only difference between them, they have the same details otherwise. coder-cli doesn't have many files so I'm not too worried about not being able to find the files due to conflicts

@jawnsy jawnsy merged commit 6389faf into main Sep 2, 2021
@jawnsy jawnsy deleted the jawnsy/ch16783/coder-agent-tee branch September 2, 2021 16:04

log := slog.Make(sinks...).Leveled(slog.LevelDebug)
if err != nil {
log.Info(ctx, "failed to open agent log file", slog.Error(err))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be an error log?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe, but I don't expect it to happen for coder workspaces and everything would be logged to our console anyways

deansheather pushed a commit that referenced this pull request Sep 8, 2021
deansheather pushed a commit that referenced this pull request Sep 13, 2021
(cherry picked from commit 6389faf)
(cherry picked from commit 45f60ba)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants