Skip to content

Commit

Permalink
日志采集添加最大文件数限制
Browse files Browse the repository at this point in the history
  • Loading branch information
liguozhuang authored and 谭彪 committed Nov 2, 2024
1 parent fd9d3e4 commit 7d34927
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions internal/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/io"
"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/io/dataway"
"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/io/filter"
"gitlab.jiagouyun.com/cloudcare-tools/datakit/internal/tailer"
)

func (c *Config) loadConfdEnvs() {
Expand Down Expand Up @@ -646,6 +647,18 @@ func (c *Config) loadHTTPAPIEnvs() {
}
}

func (c *Config) setOthers() {
if v := datakit.GetEnv("ENV_LOGGING_MAX_OPEN_FILES"); v != "" {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
l.Warnf("invalid env key ENV_LOGGING_MAX_OPEN_FILES, value %s, err: %s ignored", v, err)
} else {
l.Infof("set ENV_LOGGING_MAX_OPEN_FILES to %d", n)
tailer.MaxOpenFiles = n
}
}
}

func (c *Config) setNodenameAsHostname() {
for _, x := range []string{
"ENV_K8S_NODE_NAME",
Expand Down Expand Up @@ -708,6 +721,7 @@ func (c *Config) LoadEnvs() error {
c.loadPointPoolEnvs()

c.setNodenameAsHostname()
c.setOthers()

// Don't Add to ElectionTags.
if v := datakit.GetEnv("ENV_CLUSTER_NAME_K8S"); v != "" {
Expand Down
2 changes: 2 additions & 0 deletions internal/export/non_input_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ func envOthers() []*inputs.ENVInfo {
{ENVName: "ENV_PIPELINE_DISABLE_APPEND_RUN_INFO", Type: doc.Boolean, Default: "`false`", Desc: "Disable appending the Pipeline run info", DescZh: "禁用追加 Pipeline 运行信息"},
{ENVName: "ENV_CRYPTO_AES_KEY", Type: doc.String, Example: "`0123456789abcdef`", Desc: "The crypto key(len 16)", DescZh: "AES 加解密的 key 长度是 16"},
{ENVName: "ENV_CRYPTO_AES_KEY_FILE", Type: doc.String, Example: "`/usr/local/datakit/enc4mysql`", Desc: "File path for storing AES encryption and decryption key", DescZh: "AES 加解密的 key 存放的文件路径"},

{ENVName: "ENV_LOGGING_MAX_OPEN_FILES", Type: doc.Int, Example: "`1000`", Desc: "Specify the maximum number of open files for logging collection, if the value is -1 then there is no limit, default 500", DescZh: "指定日志采集的最大文件个数,如果值是 -1 则没有限制,默认值 500"},
}

for idx := range infos {
Expand Down
13 changes: 12 additions & 1 deletion internal/tailer/tailer_single.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"io"
"os"
"sync/atomic"
"time"

"github.com/GuanceCloud/cliutils/logger"
Expand All @@ -36,6 +37,11 @@ const (
checkInterval = time.Second * 3
)

var (
MaxOpenFiles int64 = 500
currentOpenFiles atomic.Int64
)

type Single struct {
opt *option

Expand All @@ -59,8 +65,11 @@ type Single struct {
}

func NewTailerSingle(filepath string, opts ...Option) (*Single, error) {
_ = logtail.InitDefault()
if MaxOpenFiles != -1 && currentOpenFiles.Load() > MaxOpenFiles {
return nil, fmt.Errorf("too many open files, limit %d", MaxOpenFiles)
}

_ = logtail.InitDefault()
c := defaultOption()
for _, opt := range opts {
opt(c)
Expand All @@ -79,6 +88,7 @@ func NewTailerSingle(filepath string, opts ...Option) (*Single, error) {
}

openfileVec.WithLabelValues(t.opt.mode.String()).Inc()
currentOpenFiles.Add(1)
return t, nil
}

Expand Down Expand Up @@ -164,6 +174,7 @@ func (t *Single) Close() {
t.closeFile()

openfileVec.WithLabelValues(t.opt.mode.String()).Dec()
currentOpenFiles.Add(-1)
t.log.Infof("closing: file %s", t.filepath)
}

Expand Down

0 comments on commit 7d34927

Please sign in to comment.