Skip to content

Commit

Permalink
feat: use router to detect need for trailing newline
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
Dominik Rosiek committed Jun 12, 2024
1 parent 69f2592 commit a4fde17
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ filelog/containers:
## - k8s.namespace.name
## - log.file.path
- type: retain
id: keep-fields
fields:
- attributes["stream"]
- attributes["k8s.pod.name"]
Expand All @@ -66,6 +67,18 @@ filelog/containers:
- attributes["time"]
{{ end }}

## Strip trailing "\n" from the log body
- id: strip-trailing-newline-router
{{- if .Values.sumologic.logs.multiline.enabled }}
default: multiline
{{- else }}
default: merge-multiline-logs
{{- end }}
routes:
- expr: body matches "^.*\n$"
output: strip-trailing-newline
type: router

## strip-trailing-newline removes the trailing "\n" from the `log` key. This is required for logs coming from Docker container runtime.
## Uses attributes.log as temporary cotainer for new log
## Input Body (JSON): { "log": "2001-02-03 04:05:06 very long line that was split by the logging driver\n", "stream": "stdout" }
Expand All @@ -75,14 +88,12 @@ filelog/containers:
parse_to: attributes
output: replace-body
regex: "^(?P<log>.*)\n$"
if: body matches "^.*\n$"
type: regex_parser

- id: replace-body
type: move
from: attributes.log
to: body
if: attributes.log != nil

{{- if .Values.sumologic.logs.multiline.enabled }}
## Perform multiline detection
Expand Down
13 changes: 10 additions & 3 deletions tests/helm/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ sumologic:
Id string
Type string
Output string
Fields []string
}
} `yaml:"filelog/containers"`
}
Expand All @@ -472,10 +473,16 @@ sumologic:
require.NoError(t, err)

keepTimeOperatorFound := false

operatorLoop:
for _, operator := range otelConfig.Receivers.Filelog.Operators {
if operator.Id == "move-time-attribute" {
keepTimeOperatorFound = true
break
if operator.Id == "keep-fields" {
for _, field := range operator.Fields {
if field == "attributes[\"time\"]" {
keepTimeOperatorFound = true
break operatorLoop
}
}
}
}
require.True(t, keepTimeOperatorFound)
Expand Down
9 changes: 7 additions & 2 deletions tests/helm/testdata/goldenfile/logs_otc/basic.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ data:
- attributes["k8s.container.name"]
- attributes["k8s.namespace.name"]
- attributes["log.file.path"]
id: keep-fields
type: retain
- default: multiline
id: strip-trailing-newline-router
routes:
- expr: body matches "^.*\n$"
output: strip-trailing-newline
type: router
- id: strip-trailing-newline
if: body matches "^.*\n$"
output: replace-body
parse_from: body
parse_to: attributes
Expand All @@ -85,7 +91,6 @@ data:
type: regex_parser
- from: attributes.log
id: replace-body
if: attributes.log != nil
to: body
type: move
- default: merge-multiline-logs
Expand Down
9 changes: 7 additions & 2 deletions tests/helm/testdata/goldenfile/logs_otc/debug.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ data:
- attributes["k8s.container.name"]
- attributes["k8s.namespace.name"]
- attributes["log.file.path"]
id: keep-fields
type: retain
- default: multiline
id: strip-trailing-newline-router
routes:
- expr: body matches "^.*\n$"
output: strip-trailing-newline
type: router
- id: strip-trailing-newline
if: body matches "^.*\n$"
output: replace-body
parse_from: body
parse_to: attributes
Expand All @@ -95,7 +101,6 @@ data:
type: regex_parser
- from: attributes.log
id: replace-body
if: attributes.log != nil
to: body
type: move
- default: merge-multiline-logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ data:
- attributes["k8s.container.name"]
- attributes["k8s.namespace.name"]
- attributes["log.file.path"]
id: keep-fields
type: retain
- default: multiline
id: strip-trailing-newline-router
routes:
- expr: body matches "^.*\n$"
output: strip-trailing-newline
type: router
- id: strip-trailing-newline
if: body matches "^.*\n$"
output: replace-body
parse_from: body
parse_to: attributes
Expand All @@ -85,7 +91,6 @@ data:
type: regex_parser
- from: attributes.log
id: replace-body
if: attributes.log != nil
to: body
type: move
- default: merge-multiline-logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ data:
- attributes["k8s.container.name"]
- attributes["k8s.namespace.name"]
- attributes["log.file.path"]
id: keep-fields
type: retain
- default: multiline
id: strip-trailing-newline-router
routes:
- expr: body matches "^.*\n$"
output: strip-trailing-newline
type: router
- id: strip-trailing-newline
if: body matches "^.*\n$"
output: replace-body
parse_from: body
parse_to: attributes
Expand All @@ -64,7 +70,6 @@ data:
type: regex_parser
- from: attributes.log
id: replace-body
if: attributes.log != nil
to: body
type: move
- default: merge-multiline-logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ data:
- attributes["k8s.container.name"]
- attributes["k8s.namespace.name"]
- attributes["log.file.path"]
id: keep-fields
type: retain
- default: multiline
id: strip-trailing-newline-router
routes:
- expr: body matches "^.*\n$"
output: strip-trailing-newline
type: router
- id: strip-trailing-newline
if: body matches "^.*\n$"
output: replace-body
parse_from: body
parse_to: attributes
Expand All @@ -75,7 +81,6 @@ data:
type: regex_parser
- from: attributes.log
id: replace-body
if: attributes.log != nil
to: body
type: move
- default: merge-multiline-logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ data:
- attributes["k8s.container.name"]
- attributes["k8s.namespace.name"]
- attributes["log.file.path"]
id: keep-fields
type: retain
- default: multiline
id: strip-trailing-newline-router
routes:
- expr: body matches "^.*\n$"
output: strip-trailing-newline
type: router
- id: strip-trailing-newline
if: body matches "^.*\n$"
output: replace-body
parse_from: body
parse_to: attributes
Expand All @@ -64,7 +70,6 @@ data:
type: regex_parser
- from: attributes.log
id: replace-body
if: attributes.log != nil
to: body
type: move
- default: merge-multiline-logs
Expand Down

0 comments on commit a4fde17

Please sign in to comment.