멀티라인 (Multiline)
여러 줄로 나뉘어 있거나 스택 트레이스 형태의 로그 메시지를 하나로 합쳐 주는 필터예요.
지원 이벤트 유형:
logs
Multiline 필터는 원래 하나의 컨텍스트에 속했지만 여러 레코드나 로그 줄로 쪼개진 메시지들을 이어 붙이는 데 도움을 줘요. 대표적인 예로 스택 트레이스나 여러 줄에 걸쳐 로그를 출력하는 애플리케이션이 있어요.
멀티라인 필터와 함께, 자동 감지와 다중 포맷을 지원하는 다음 내장 Fluent Bit 파서 중 하나를 활성화할 수 있어요.
- Go
- Python
- Ruby
- Java (Google Cloud Platform Java 스택 트레이스 형식)
내장 json 멀티라인 파서는 여러 줄로 나뉜 예쁘게 출력된(pretty-printed) JSON 객체를 그룹으로 묶어 줘요. 로그 파일을 읽을 때는 이 필터보다 Tail 플러그인에 설정하는 걸 권장해요. 자세한 내용은 멀티라인 파싱: JSON을 참고해요.
이 필터를 사용할 때는 다음을 기억해야 해요.
- 이 필터를 사용하려면 먼저 멀티라인 파서 정의를 구성해 두어야 해요.
- 로그 파일에서 읽어온 메시지를 합칠 때는 Tail 플러그인 자체의 멀티라인 지원을 사용하는 걸 적극 권장해요. 로그 파일을 읽는 동시에 합치기를 수행하는 것이 성능상 더 좋기 때문이에요. 원래 한 줄이었지만 크기 때문에 Docker나 CRI 컨테이너 엔진이 쪼갠 메시지를 합치는 것은 Tail 플러그인에서
docker또는cri파서와 함께 지원돼요. 스택 트레이스 같은 애플리케이션 로그를 그 위에 추가로 합치려면 이 멀티라인 필터를 사용할 수 있어요.
경고 — 이 필터는
Buffer가 활성화되어 있을 때만 서로 다른 Chunk에 걸쳐 지속되는 버퍼링을 수행해요. 그렇지 않으면 필터는 한 번에 하나의 chunk만 처리하므로, 멀티라인 메시지를 별도의 chunk로 보낼 수 있는 대부분의 입력에는 적합하지 않아요.버퍼링이 활성화되면 필터는 수신한 메시지를 즉시 내보내지 않아요. Rewrite Tag 필터와 유사하게
in_emitter플러그인을 사용하며, 메시지가 완전히 합쳐지거나 타임아웃에 도달하면 메시지를 내보내요.
경고 — 합쳐진 레코드는 Fluent Bit 로그 파이프라인의 맨 앞으로 다시 내보내지기 때문에, 같은 태그를 매칭하는 멀티라인 필터 정의를 여러 개 구성할 수 없어요. 이렇게 하면 Fluent Bit 파이프라인에서 무한 루프가 발생해요. 같은 로그에 여러 파서를 사용하려면
multiline.parser에 파서를 쉼표로 구분한 목록으로 넣는 단일 필터 정의를 구성해야 해요. 자세한 내용은 이슈 #5235를 참고해요.두 번째로, 같은 이유 때문에 멀티라인 필터는 첫 번째 필터여야 해요. 로그는 멀티라인 필터에 의해 파이프라인 맨 앞으로 다시 내보내지는데, 필터는 자기가 다시 내보낸 레코드는 무시하지만 다른 필터는 그렇지 않아요. 멀티라인 필터 앞에 다른 필터가 있으면 그 필터가 두 번 적용돼요.
출처: 문서
본문
구성 파라미터
이 플러그인은 다음 구성 파라미터를 지원해요.
| Key | Description |
|---|---|
buffer |
Enable buffered mode. In buffered mode, the filter can concatenate multiple lines from inputs that ingest records one by one, such as Forward, rather than in chunks. It re-emits them into the beginning of the pipeline with the same tag, using the in_emitter instance. With buffer off, this filter won't work with most inputs, except Tail. |
debug_flush |
Enable debug logging for flush operations. Default: false. |
emitter_mem_buf_limit |
Set a limit on the amount of memory the emitter can consume if the outputs provide backpressure. The default for this limit is 10M. The pipeline will pause once the buffer exceeds the value of this setting. For example, if the value is set to 10M then the pipeline pauses if the buffer exceeds 10M. The pipeline will remain paused until the output drains the buffer under the 10M limit. |
emitter_name |
Name for the emitter input instance which re-emits the completed records at the beginning of the pipeline. |
emitter_storage.type |
The storage type for the emitter input instance. This option supports the values memory (default) and filesystem. |
flush_ms |
Flush time for pending multiline records. Default: 2000. |
mode |
Mode can be parser for regular expression concatenation, or partial_message to concatenate split Docker logs. |
multiline.key_content |
Key name that holds the content to process. A multiline parser definition can specify the key_content This option allows for overwriting that value for the purpose of the filter. |
multiline.parser |
Specify one or multiple Multiline Parser definitions to apply to the content. You can specify multiple multiline parsers to detect different formats by separating them with a comma. |
구성 예시
다음 예시는 일반 줄, 커스텀 Java 스택 트레이스, 그리고 Go 스택 트레이스가 섞여 있는 test.log라는 로그 파일을 파싱하는 걸 목표로 해요.
다음 예시 파일들은 Fluent Bit 저장소에서 찾을 수 있어요.
예시 파일 내용:
fluent-bit.yaml — 이것은 기본 Fluent Bit YAML 설정 파일이에요. parsers_multiline.yaml을 포함하고, 멀티라인 파서 multiline-regex-test와 go를 적용해서 test.log 파일을 tail해요. 그런 다음 처리 결과를 표준 출력으로 보내요.
service:
flush: 1
log_level: info
# Configure this to load your custom parser if required
# parsers_file: parsers_multiline.yaml
pipeline:
inputs:
- name: tail
path: test.log
read_from_head: true
filters:
- name: multiline
match: '*'
multiline.key_content: log
multiline.parser: go,multiline-regex-test
outputs:
- name: stdout
match: '*'
fluent-bit.conf — 이것은 기본 Fluent Bit 클래식 설정 파일이에요. parsers_multiline.conf를 포함하고, 멀티라인 파서 multiline-regex-test와 go를 적용해서 test.log 파일을 tail해요. 그런 다음 처리 결과를 표준 출력으로 보내요.
[SERVICE]
Flush 1
Log_Level info
# Configure this to load your custom parser if required
# Parsers_File parsers_multiline.conf
[INPUT]
Name tail
Path test.log
Read_From_Head true
[FILTER]
Name multiline
Match *
multiline.key_content log
multiline.parser go, multiline-regex-test
[OUTPUT]
Name stdout
Match *
parsers_multiline.yaml — 이 파일은 예시를 위한 멀티라인 파서를 정의해요. fluent-bit.yaml에서 go라는 두 번째 멀티라인 파서를 사용하는데, 이것은 내장 파서예요.
multiline_parsers:
- name: multiline-regex-test
type: regex
flush_timeout: 1000
#
# Regex rules for multiline parsing
# ---------------------------------
#
# configuration hints:
#
# - first state always has the name: start_state
# - every field in the rule must be inside double quotes
#
# rules | state name | regex pattern | next state
# ------|---------------|--------------------------------------------
rules:
- state: start_state
regex: '/([a-zA-Z]+ \d+ \d+\:\d+\:\d+)(.*)/'
next_state: cont
- state: cont
regex: '/^\s+at.*/'
next_state: cont
parsers_multiline.conf — 이 파일은 예시를 위한 멀티라인 파서를 정의해요. fluent-bit.conf에서 go라는 두 번째 멀티라인 파서를 사용하는데, 이것은 내장 파서예요.
[MULTILINE_PARSER]
Name multiline-regex-test
Type regex
Flush_Timeout 1000
#
# Regex rules for multiline parsing
# ---------------------------------
#
# configuration hints:
#
# - first state always has the name: start_state
# - every field in the rule must be inside double quotes
#
# rules | state name | regex pattern | next state
# ------|---------------|--------------------------------------------
Rule "start_state" "/([A-Za-z]+ \d+ \d+\:\d+\:\d+)(.*)/" "cont"
Rule "cont" "/^\s+at.*/" "cont"
test.log — 멀티라인과 다중 포맷 내용을 담은 예시 파일이에요.
single line...
Dec 14 06:41:08 Exception in thread "main" java.lang.RuntimeException: Something has gone wrong, aborting!
at com.myproject.module.MyProject.badMethod(MyProject.java:22)
at com.myproject.module.MyProject.oneMoreMethod(MyProject.java:18)
at com.myproject.module.MyProject.anotherMethod(MyProject.java:14)
at com.myproject.module.MyProject.someMethod(MyProject.java:10)
at com.myproject.module.MyProject.main(MyProject.java:6)
another line...
panic: my panic
goroutine 4 [running]:
panic(0x45cb40, 0x47ad70)
/usr/local/go/src/runtime/panic.go:542 +0x46c fp=0xc42003f7b8 sp=0xc42003f710 pc=0x422f7c
main.main.func1(0xc420024120)
foo.go:6 +0x39 fp=0xc42003f7d8 sp=0xc42003f7b8 pc=0x451339
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc42003f7e0 sp=0xc42003f7d8 pc=0x44b4d1
created by main.main
foo.go:5 +0x58
goroutine 1 [chan receive]:
runtime.gopark(0x4739b8, 0xc420024178, 0x46fcd7, 0xc, 0xc420028e17, 0x3)
/usr/local/go/src/runtime/proc.go:280 +0x12c fp=0xc420053e30 sp=0xc420053e00 pc=0x42503c
runtime.goparkunlock(0xc420024178, 0x46fcd7, 0xc, 0x1000f010040c217, 0x3)
/usr/local/go/src/runtime/proc.go:286 +0x5e fp=0xc420053e70 sp=0xc420053e30 pc=0x42512e
runtime.chanrecv(0xc420024120, 0x0, 0xc420053f01, 0x4512d8)
/usr/local/go/src/runtime/chan.go:506 +0x304 fp=0xc420053f20 sp=0xc420053e70 pc=0x4046b4
runtime.chanrecv1(0xc420024120, 0x0)
/usr/local/go/src/runtime/chan.go:388 +0x2b fp=0xc420053f50 sp=0xc420053f20 pc=0x40439b
main.main()
foo.go:9 +0x6f fp=0xc420053f80 sp=0xc420053f50 pc=0x4512ef
runtime.main()
/usr/local/go/src/runtime/proc.go:185 +0x20d fp=0xc420053fe0 sp=0xc420053f80 pc=0x424bad
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc420053fe8 sp=0xc420053fe0 pc=0x44b4d1
goroutine 2 [force gc (idle)]:
runtime.gopark(0x4739b8, 0x4ad720, 0x47001e, 0xf, 0x14, 0x1)
/usr/local/go/src/runtime/proc.go:280 +0x12c fp=0xc42003e768 sp=0xc42003e738 pc=0x42503c
runtime.goparkunlock(0x4ad720, 0x47001e, 0xf, 0xc420000114, 0x1)
/usr/local/go/src/runtime/proc.go:286 +0x5e fp=0xc42003e7a8 sp=0xc42003e768 pc=0x42512e
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:238 +0xcc fp=0xc42003e7e0 sp=0xc42003e7a8 pc=0x424e5c
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc42003e7e8 sp=0xc42003e7e0 pc=0x44b4d1
created by runtime.init.4
/usr/local/go/src/runtime/proc.go:227 +0x35
goroutine 3 [GC sweep wait]:
runtime.gopark(0x4739b8, 0x4ad7e0, 0x46fdd2, 0xd, 0x419914, 0x1)
/usr/local/go/src/runtime/proc.go:280 +0x12c fp=0xc42003ef60 sp=0xc42003ef30 pc=0x42503c
runtime.goparkunlock(0x4ad7e0, 0x46fdd2, 0xd, 0x14, 0x1)
/usr/local/go/src/runtime/proc.go:286 +0x5e fp=0xc42003efa0 sp=0xc42003ef60 pc=0x42512e
runtime.bgsweep(0xc42001e150)
/usr/local/go/src/runtime/mgcsweep.go:52 +0xa3 fp=0xc42003efd8 sp=0xc42003efa0 pc=0x419973
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc42003efe0 sp=0xc42003efd8 pc=0x44b4d1
created by runtime.gcenable
/usr/local/go/src/runtime/mgc.go:216 +0x58
one more line, no multiline
위 설정 파일로 Fluent Bit를 실행하면:
fluent-bit -c fluent-bit.conf
다음과 같은 결과가 나올 거예요.
[0] tail.0: [1626736433.143567481, {"log"=>"single line..."}]
[1] tail.0: [1626736433.143570538, {"log"=>"Dec 14 06:41:08 Exception in thread "main" java.lang.RuntimeException: Something has gone wrong, aborting!
at com.myproject.module.MyProject.badMethod(MyProject.java:22)
at com.myproject.module.MyProject.oneMoreMethod(MyProject.java:18)
at com.myproject.module.MyProject.anotherMethod(MyProject.java:14)
at com.myproject.module.MyProject.someMethod(MyProject.java:10)
at com.myproject.module.MyProject.main(MyProject.java:6)"}]
[2] tail.0: [1626736433.143572538, {"log"=>"another line..."}]
[3] tail.0: [1626736433.143572894, {"log"=>"panic: my panic
goroutine 4 [running]:
panic(0x45cb40, 0x47ad70)
/usr/local/go/src/runtime/panic.go:542 +0x46c fp=0xc42003f7b8 sp=0xc42003f710 pc=0x422f7c
main.main.func1(0xc420024120)
foo.go:6 +0x39 fp=0xc42003f7d8 sp=0xc42003f7b8 pc=0x451339
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc42003f7e0 sp=0xc42003f7d8 pc=0x44b4d1
created by main.main
foo.go:5 +0x58
goroutine 1 [chan receive]:
runtime.gopark(0x4739b8, 0xc420024178, 0x46fcd7, 0xc, 0xc420028e17, 0x3)
/usr/local/go/src/runtime/proc.go:280 +0x12c fp=0xc420053e30 sp=0xc420053e00 pc=0x42503c
runtime.goparkunlock(0xc420024178, 0x46fcd7, 0xc, 0x1000f010040c217, 0x3)
/usr/local/go/src/runtime/proc.go:286 +0x5e fp=0xc420053e70 sp=0xc420053e30 pc=0x42512e
runtime.chanrecv(0xc420024120, 0x0, 0xc420053f01, 0x4512d8)
/usr/local/go/src/runtime/chan.go:506 +0x304 fp=0xc420053f20 sp=0xc420053e70 pc=0x4046b4
runtime.chanrecv1(0xc420024120, 0x0)
/usr/local/go/src/runtime/chan.go:388 +0x2b fp=0xc420053f50 sp=0xc420053f20 pc=0x40439b
main.main()
foo.go:9 +0x6f fp=0xc420053f80 sp=0xc420053f50 pc=0x4512ef
runtime.main()
/usr/local/go/src/runtime/proc.go:185 +0x20d fp=0xc420053fe0 sp=0xc420053f80 pc=0x424bad
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc420053fe8 sp=0xc420053fe0 pc=0x44b4d1
goroutine 2 [force gc (idle)]:
runtime.gopark(0x4739b8, 0x4ad720, 0x47001e, 0xf, 0x14, 0x1)
/usr/local/go/src/runtime/proc.go:280 +0x12c fp=0xc42003e768 sp=0xc42003e738 pc=0x42503c
runtime.goparkunlock(0x4ad720, 0x47001e, 0xf, 0xc420000114, 0x1)
/usr/local/go/src/runtime/proc.go:286 +0x5e fp=0xc42003e7a8 sp=0xc42003e768 pc=0x42512e
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:238 +0xcc fp=0xc42003e7e0 sp=0xc42003e7a8 pc=0x424e5c
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc42003e7e8 sp=0xc42003e7e0 pc=0x44b4d1
created by runtime.init.4
/usr/local/go/src/runtime/proc.go:227 +0x35
goroutine 3 [GC sweep wait]:
runtime.gopark(0x4739b8, 0x4ad7e0, 0x46fdd2, 0xd, 0x419914, 0x1)
/usr/local/go/src/runtime/proc.go:280 +0x12c fp=0xc42003ef60 sp=0xc42003ef30 pc=0x42503c
runtime.goparkunlock(0x4ad7e0, 0x46fdd2, 0xd, 0x14, 0x1)
/usr/local/go/src/runtime/proc.go:286 +0x5e fp=0xc42003efa0 sp=0xc42003ef60 pc=0x42512e
runtime.bgsweep(0xc42001e150)
/usr/local/go/src/runtime/mgcsweep.go:52 +0xa3 fp=0xc42003efd8 sp=0xc42003efa0 pc=0x419973
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc42003efe0 sp=0xc42003efd8 pc=0x44b4d1
created by runtime.gcenable
/usr/local/go/src/runtime/mgc.go:216 +0x58"}]
[4] tail.0: [1626736433.143585473, {"log"=>"one more line, no multiline"}]
패턴과 일치하지 않는 줄은 멀티라인 메시지의 일부로 간주되지 않고, 규칙과 일치한 줄들은 제대로 이어 붙여진 모습이에요.
Docker 부분 메시지 사용 사례
Docker 같은 컨테이너 런타임에서 로그를 소비할 때, 로그가 일정 한도(보통 16 KB)를 넘으면 쪼개져요. 애플리케이션이 100K 로그 줄을 내보내면 일곱 개의 부분 메시지로 나뉘게 돼요. docker 파서는 이 줄들을 다시 하나로 합쳐 줘요. 대신 Fluentd Docker Log Driver를 사용해 로그를 Fluent Bit로 보낸다면 다음과 같이 보일 수 있어요.
{"source": "stdout", "log": "... omitted for brevity...", "partial_message": "true", "partial_id": "dc37eb08b4242c41757d4cd995d983d1cdda4589193755a22fcf47a638317da0", "partial_ordinal": "1", "partial_last": "false", "container_id": "a96998303938eab6087a7f8487ca40350f2c252559bc6047569a0b11b936f0f2", "container_name": "/hopeful_taussig"}]
Fluent Bit는 런타임에 쪼개진 이 로그들을 다시 합치고 부분 메시지 필드들을 제거할 수 있어요. 다음 필터 예시는 바로 이 사용 사례를 위한 거예요.
fluent-bit.yaml:
pipeline:
filters:
- name: multiline
match: '*'
multiline.key_content: log
mode: partial_message
fluent-bit.conf:
[FILTER]
Name multiline
Match *
multiline.key_content log
Mode partial_message
필터에서 mode의 두 옵션은 서로 배타적이에요. mode를 partial_message로 설정하면 multiline.parser 옵션은 허용되지 않아요.