-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent the constant logging of cgroup errors on non-linux machi…
…nes (#2264) * Prevent the constant logging of cgroup errors on non-linux machines * Update changelog * Document AUTOMEMLIMIT_EXPERIMENT * Add test
- Loading branch information
Showing
8 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package alloycli | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/KimMachineGun/automemlimit/memlimit" | ||
"github.com/grafana/alloy/internal/runtime/logging" | ||
) | ||
|
||
func applyAutoMemLimit(l *logging.Logger) { | ||
// For non-linux builds without cgroups, memlimit will always report an error. | ||
// However, if the system experiment is requested, we can use the system memory limit provider. | ||
// This logic is similar to https://github.com/KimMachineGun/automemlimit/blob/main/memlimit/experiment.go | ||
if v, ok := os.LookupEnv("AUTOMEMLIMIT_EXPERIMENT"); ok { | ||
if slices.Contains(strings.Split(v, ","), "system") { | ||
memlimit.SetGoMemLimitWithOpts(memlimit.WithProvider(memlimit.FromSystem), memlimit.WithLogger(slog.New(l.Handler()))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package alloycli | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/KimMachineGun/automemlimit/memlimit" | ||
"github.com/grafana/alloy/internal/runtime/logging" | ||
) | ||
|
||
func applyAutoMemLimit(l *logging.Logger) { | ||
memlimit.SetGoMemLimitWithOpts(memlimit.WithLogger(slog.New(l.Handler()))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package alloycli | ||
|
||
import ( | ||
"bytes" | ||
"log/slog" | ||
"testing" | ||
|
||
"github.com/KimMachineGun/automemlimit/memlimit" | ||
"github.com/grafana/alloy/internal/runtime/logging" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNoMemlimitErrorLogs(t *testing.T) { | ||
buffer := bytes.NewBuffer(nil) | ||
|
||
l, err := logging.New(buffer, logging.DefaultOptions) | ||
require.NoError(t, err) | ||
|
||
applyAutoMemLimit(l) | ||
|
||
require.Equal(t, "", buffer.String()) | ||
|
||
// Linux behavior, to confirm error is logged | ||
memlimit.SetGoMemLimitWithOpts(memlimit.WithLogger(slog.New(l.Handler()))) | ||
|
||
require.Contains(t, buffer.String(), "cgroups is not supported on this system") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters