Skip to content

Commit 5518c89

Browse files
authored
Merge pull request #4856 from shouhei/fix/fluentd-buffer-limit-human-readable
fix: support human-readable sizes for fluentd-buffer-limit log option
2 parents 55aee6b + ee94580 commit 5518c89

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

docs/command-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ Logging flags:
365365
- The `fluentd` logging driver supports the following logging options:
366366
- :whale: `--log-opt=fluentd-address=<ADDRESS>`: The address of the `fluentd` daemon, tcp(default) and unix sockets are supported..
367367
- :whale: `--log-opt=fluentd-async=<true|false>`: Enable async mode for fluentd. The default value is false.
368-
- :whale: `--log-opt=fluentd-buffer-limit=<LIMIT>`: The buffer limit for fluentd. If the buffer is full, the call to record logs will fail. The default is 8192. (<https://github.com/fluent/fluent-logger-golang/tree/master#bufferlimit>)
368+
- :whale: `--log-opt=fluentd-buffer-limit=<LIMIT>`: The buffer limit for fluentd. If the buffer is full, the call to record logs will fail. The default is 1MiB. Accepts human-readable sizes (e.g., `1KiB`, `1MiB`, `1GiB`) or raw byte values. (<https://github.com/fluent/fluent-logger-golang/tree/master#bufferlimit>)
369369
- :whale: `--log-opt=fluentd-retry-wait=<1s|1ms>`: The time to wait before retrying to send logs to fluentd. The default value is 1s.
370370
- :whale: `--log-opt=fluentd-max-retries=<1>`: The maximum number of retries to send logs to fluentd. The default value is MaxInt32.
371371
- :whale: `--log-opt=fluentd-sub-second-precision=<true|false>`: Enable sub-second precision for fluentd. The default value is false.

pkg/logging/fluentd_logger.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sync"
2828
"time"
2929

30+
units "github.com/docker/go-units"
3031
"github.com/fluent/fluent-logger-golang/fluent"
3132

3233
"github.com/containerd/containerd/v2/core/runtime/v2/logging"
@@ -223,10 +224,14 @@ func parseFluentdConfig(config map[string]string) (fluent.Config, error) {
223224
}
224225
bufferLimit := defaultBufferLimit
225226
if config[fluentdBufferLimit] != "" {
226-
bufferLimit, err = strconv.Atoi(config[fluentdBufferLimit])
227+
parsedBufferLimit, err := units.RAMInBytes(config[fluentdBufferLimit])
227228
if err != nil {
228229
return result, fmt.Errorf("error occurs %w,invalid buffer limit (%s)", err, config[fluentdBufferLimit])
229230
}
231+
if parsedBufferLimit > int64(math.MaxInt) {
232+
return result, fmt.Errorf("invalid buffer limit: value %d overflows int", parsedBufferLimit)
233+
}
234+
bufferLimit = int(parsedBufferLimit)
230235
}
231236
retryWait := int(defaultRetryWait)
232237
if config[fluentdRetryWait] != "" {

pkg/logging/fluentd_logger_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,27 @@ func TestParseFluentdConfig(t *testing.T) {
229229
AsyncReconnectInterval: 0,
230230
SubSecondPrecision: false,
231231
RequestAck: true}, false},
232+
{"HumanReadableBufferLimit", args{
233+
config: map[string]string{
234+
fluentdBufferLimit: "1M",
235+
}},
236+
fluent.Config{
237+
FluentPort: defaultPort,
238+
FluentHost: defaultHost,
239+
FluentNetwork: defaultProtocol,
240+
FluentSocketPath: "",
241+
BufferLimit: defaultBufferLimit,
242+
RetryWait: int(defaultRetryWait),
243+
MaxRetry: defaultMaxRetries,
244+
Async: false,
245+
AsyncReconnectInterval: 0,
246+
SubSecondPrecision: false,
247+
RequestAck: false}, false},
248+
{"InvalidHumanReadableBufferLimit", args{
249+
config: map[string]string{
250+
fluentdBufferLimit: "not-a-size",
251+
}},
252+
fluent.Config{}, true},
232253
}
233254
for _, tt := range tests {
234255
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)