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

logp: don't write to files by default if running in a container environment #236

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion logp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ const (
// DefaultConfig returns the default config options for a given environment the
// Beat is supposed to be run within.
func DefaultConfig(environment Environment) Config {
toFiles := true

// If running in a container environment, don't write to files by default.
if environment == ContainerEnvironment {
Copy link
Member

Choose a reason for hiding this comment

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

The elastic-agent container always logs to files in the file system by default, even if logging to stdout+stderr is disabled. These are the logs that are included in diagnostics from the container.

Let's make sure we don't break agent diagnostics when this merges into elastic-agent.

Copy link
Member Author

Choose a reason for hiding this comment

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

ACK. This is reverting to the previous behavior. When we silently made this change earlier, it didn’t break elastic-agent, nor was it broken before the change. I don’t believe it's affected at all, but I could be wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

So looking at changes in elastic-agent, there is only two occurrences of logp.DefaultConfig and both use logp.DefaultEnvironment which always gets ToFiles = true.

toFiles = false
}

return Config{
Level: defaultLevel,
ToFiles: true,
ToFiles: toFiles,
Files: FileConfig{
MaxSize: 10 * 1024 * 1024,
MaxBackups: 7,
Expand Down
51 changes: 51 additions & 0 deletions logp/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package logp_test

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -117,6 +119,55 @@ func TestDefaultConfig(t *testing.T) {
}
}

func TestDefaultConfigContainer(t *testing.T) {
switch runtime.GOOS {
case "wasip1", "js", "ios":
t.Skipf("cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH)
}

if os.Getenv("TEST_DEFAULT_CONFIG_CONTAINER") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=^TestDefaultConfigContainer$", "-test.v") //nolint:gosec // This is intentionally running a subprocess
cmd.Env = append(cmd.Env, "TEST_DEFAULT_CONFIG_CONTAINER=1")

var stderr bytes.Buffer
cmd.Stderr = &stderr

err := cmd.Run()
data := stderr.Bytes()
assert.NoError(t, err, "command failed with error: %s\nstderr: %s", err, data)
t.Logf("output:\n%s", data)

logEntry := struct {
LogLevel string `json:"log.level"`
LogOrigin struct {
FileName string `json:"file.name"`
FileLine int `json:"file.line"`
} `json:"log.origin"`
Message string `json:"message"`
}{}

assert.NoError(t, json.Unmarshal(data, &logEntry), "cannot unmarshal log entry from stderr")

assert.Equal(t, "info", logEntry.LogLevel)
assert.Equal(t, "foo", logEntry.Message)

_, fileName, _, _ := runtime.Caller(0)
expectedFileName := filepath.Base(fileName)
gotFileName := filepath.Base(logEntry.LogOrigin.FileName)
assert.Equal(t, expectedFileName, gotFileName)

return
}

// This is running in a separate process. By default the
// container environment should be logging to stderr.
cfg := logp.DefaultConfig(logp.ContainerEnvironment)
assert.NoError(t, logp.Configure(cfg))
logger := logp.L()
defer logger.Close()
logger.Info("foo")
}

func TestWith(t *testing.T) {
tempDir := t.TempDir()

Expand Down
Loading