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 3 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
12 changes: 8 additions & 4 deletions logp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ const (
// Beat is supposed to be run within.
func DefaultConfig(environment Environment) Config {
toFiles := true
toStderr := false

// If running in a container environment, don't write to files by default.
if environment == ContainerEnvironment {
// For container and systemd environments, we don't write to files by default.
switch environment {
case ContainerEnvironment, SystemdEnvironment:
Comment on lines +76 to +78
Copy link
Contributor

Choose a reason for hiding this comment

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

The fact that we have this logic in two different places, bugs me... Because the FF for 8.15.3 was Tue, I'd get this PR in as it is and backport it if we still have time to make into the release. Then create an issue to re-visit it at some point in the future.

I believe that's the correct function to define the default behaviour, createLogOutput should not need the environment switch.

toFiles = false
toStderr = true
}

return Config{
Level: defaultLevel,
ToFiles: toFiles,
Level: defaultLevel,
ToFiles: toFiles,
ToStderr: toStderr,
Files: FileConfig{
MaxSize: 10 * 1024 * 1024,
MaxBackups: 7,
Expand Down
21 changes: 14 additions & 7 deletions logp/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,23 @@ func TestDefaultConfig(t *testing.T) {
}
}

func TestDefaultConfigContainer(t *testing.T) {
func TestDefaultConfigContainerLogsToStderr(t *testing.T) {
runTestEnvStderr(t, logp.ContainerEnvironment)
}

func TestDefaultConfigSystemdLogsToStderr(t *testing.T) {
runTestEnvStderr(t, logp.SystemdEnvironment)
}

func runTestEnvStderr(t *testing.T, envType logp.Environment) {
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")
if os.Getenv("TEST_DEFAULT_CONFIG_STDERR") != "1" {
cmd := exec.Command(os.Args[0], fmt.Sprintf("-test.run=^%s$", t.Name()), "-test.v") //nolint:gosec // This is intentionally running a subprocess
cmd.Env = append(cmd.Env, "TEST_DEFAULT_CONFIG_STDERR=1")

var stderr bytes.Buffer
cmd.Stderr = &stderr
Expand Down Expand Up @@ -159,9 +167,8 @@ func TestDefaultConfigContainer(t *testing.T) {
return
}

// This is running in a separate process. By default the
// container environment should be logging to stderr.
cfg := logp.DefaultConfig(logp.ContainerEnvironment)
// This is running in a separate process to make sure we capture stderr.
cfg := logp.DefaultConfig(envType)
assert.NoError(t, logp.Configure(cfg))
logger := logp.L()
defer logger.Close()
Expand Down
Loading