Skip to content

Commit

Permalink
Merge branch 'dev-li-container-log' into 'dev'
Browse files Browse the repository at this point in the history
更新容器日志的match规则,更新对应文档,修复容器名错误的bug

See merge request cloudcare-tools/datakit!626
  • Loading branch information
谭彪 committed Jul 22, 2021
2 parents acba51c + de1bdd7 commit d7e332b
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 271 deletions.
24 changes: 12 additions & 12 deletions internal/tailer/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ const (
disableHighFreqIODdata = false
)

type logs struct {
type Logs struct {
text string
fields map[string]interface{}
ts time.Time
pt *io.Point
err error
}

func newLogs(text string) *logs {
return &logs{text: text, fields: make(map[string]interface{})}
func NewLogs(text string) *Logs {
return &Logs{text: text, fields: make(map[string]interface{})}
}

func (x *logs) pipeline(p *pipeline.Pipeline) *logs {
func (x *Logs) Pipeline(p *pipeline.Pipeline) *Logs {
if x.err != nil || p == nil {
x.fields["message"] = x.text
return x
Expand All @@ -51,7 +51,7 @@ func (x *logs) pipeline(p *pipeline.Pipeline) *logs {
// checkFieldsLength 检查数据是否过长
// 只有在碰到非 message 字段,且长度超过最大限制时才会返回 error
// 防止通过 pipeline 添加巨长字段的恶意行为
func (x *logs) checkFieldsLength() *logs {
func (x *Logs) CheckFieldsLength() *Logs {
if x.err != nil {
return x
}
Expand Down Expand Up @@ -101,7 +101,7 @@ var statusMap = map[string]string{
}

// addStatus 添加默认status和status映射
func (x *logs) addStatus(disable bool) *logs {
func (x *Logs) AddStatus(disable bool) *Logs {
if x.err != nil || disable {
return x
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (x *logs) addStatus(disable bool) *logs {
}

// ignoreStatus 过滤指定status
func (x *logs) ignoreStatus(ignoreStatus []string) *logs {
func (x *Logs) IgnoreStatus(ignoreStatus []string) *Logs {
if x.err != nil || len(ignoreStatus) == 0 {
return x
}
Expand All @@ -147,7 +147,7 @@ func (x *logs) ignoreStatus(ignoreStatus []string) *logs {
return x
}

func (x *logs) takeTime() *logs {
func (x *Logs) TakeTime() *Logs {
if x.err != nil {
return x
}
Expand All @@ -170,15 +170,15 @@ func (x *logs) takeTime() *logs {
return x
}

func (x *logs) point(measurement string, tags map[string]string) *logs {
func (x *Logs) Point(measurement string, tags map[string]string) *Logs {
if x.err != nil {
return x
}
x.pt, x.err = io.MakePoint(measurement, tags, x.fields, x.ts)
return x
}

func (x *logs) feed(inputName string) *logs {
func (x *Logs) Feed(inputName string) *Logs {
if x.err != nil {
return x
}
Expand All @@ -190,14 +190,14 @@ func (x *logs) feed(inputName string) *logs {
return x
}

func (x *logs) output() string {
func (x *Logs) Output() string {
if x.pt == nil {
return ""
}
return x.pt.String()
}

func (x *logs) error() error {
func (x *Logs) Error() error {
return x.err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/tailer/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestLogsAll(t *testing.T) {
point(source, nil).
output()

tu.Assert(t, tc.text != tc.res,
tu.Assert(t, output == tc.res,
"\nexpect: %s\n got: %s",
tc.res, output)
}
Expand Down
18 changes: 9 additions & 9 deletions internal/tailer/tailer_single.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ func (t *TailerSingle) processText(text string) error {
return nil
}

err := newLogs(text).
pipeline(t.pipeline).
checkFieldsLength().
addStatus(t.opt.DisableAddStatusField).
ignoreStatus(t.opt.IgnoreStatus).
takeTime().
point(t.opt.Source, t.tags).
feed(t.opt.InputName).
error()
err := NewLogs(text).
Pipeline(t.pipeline).
CheckFieldsLength().
AddStatus(t.opt.DisableAddStatusField).
IgnoreStatus(t.opt.IgnoreStatus).
TakeTime().
Point(t.opt.Source, t.tags).
Feed(t.opt.InputName).
Error()

return err
}
Expand Down
27 changes: 24 additions & 3 deletions man/manuals/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,35 @@

对于容器日志采集,有着更细致的配置,主要解决了区分日志 `source` 和使用 pipeline 的问题。

日志采集配置项为 `[[inputs.container.logfilter]]`,该项是数组配置,意即可以有多个 logfilter 来处理采集到的容器日志,比如某个容器中既有 MySQL 日志,也有 Redis 日志,那么此时可能需要两个 logfilter 来分别处理它们。
日志采集配置项为 `[[inputs.container.log]]`,该项是数组配置,意即可以有多个 log 来处理采集到的容器日志,比如某个容器中既有 MySQL 日志,也有 Redis 日志,那么此时可能需要两个 log 来分别处理它们。

- `filter_message` 为匹配日志文本的正则表达式,该参数类型是字符串数组,只要任意一个正则匹配成功即可。未匹配的日志内容将被丢弃。[正则表达式参见这里](https://golang.org/pkg/regexp/syntax/#hdr-Syntax)
- `match_by` 为匹配类型,只支持填写 `contianer-name``deployment-name`(注意是中横线)。例如该项为 `container_name`,则会以容器名进行后续的正则匹配,当匹配成功时使用此 `source` 和 pipeline
- `match` 为匹配日志文本的正则表达式,该参数类型是字符串数组,只要任意一个正则匹配成功即可。未匹配的容器,其日志将执行默认处理方式。[正则表达式参见这里](https://golang.org/pkg/regexp/syntax/#hdr-Syntax)
>Tips:为保证此处正则表达式的正确书写,请务必将正则表达式用 `'''这里是一个正则表达式'''` 这种形式来配置(即两边用三个单引号来包围正则文本),否则可能导致正则转义问题。
- `source` 指定数据来源,如果为空值,则默认使用容器名
- `source` 指定数据来源,其值不可为空
- `service` 指定该条日志的服务名,如果为空值,则使用 `source` 字段值
- `pipeline` 只需写文件名即可,不需要写全路径,使用方式见 [Pipeline 文档](pipeline)。当此值为空值或该文件不存在时,将不使用 pipeline 功能

如果一个容器的 `container name``deployment` 分别匹配两个 log,会优先使用 `deployment` 所匹配的 log。例如容器的 `container name``containerAAA``deployment``deploymentAAA`,且配置如下:

```
[[inputs.container.log]]
match_by = "container-name"
match = ['''container*''']
source = "dummy1"
service = "dummy1"
pipeline = "dummy1.p"
[[inputs.container.log]]
match_by = "deployment-name"
match = ['''deployment*''']
source = "dummy2"
service = "dummy2"
pipeline = "dummy2.p"
```

此时该容器能够匹配两个 log,优先使用第二个 `deployment`

#### 日志切割注意事项

使用 pipeline 功能时,如果切割成功,则:
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/container/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ const sampleCfg = `
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
#[[inputs.container.logfilter]]
# filter_message = [
# '''<this-is-message-regexp''',
# '''<this-is-another-message-regexp''',
#[[inputs.container.log]]
# match_by = "container-name"
# match = [
# '''<this-is-regexp''',
# ]
# source = "<your-source-name>"
# service = "<your-service-name>"
Expand Down
Loading

0 comments on commit d7e332b

Please sign in to comment.