Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/feat_42389.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: "enhancement"

# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
component: "pkg/stanza"

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Ensure container operator does not split batches of entries"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [42389]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
2 changes: 1 addition & 1 deletion pkg/stanza/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Operators that support batching:

- `add`
- `assign_keys`
- `container`
- `copy`
- `flatten`
- `json_array_parser`
Expand All @@ -112,7 +113,6 @@ Operators that support batching:

Operators that do not support batching:

- `container`
- `csv_parser`
- `filter`
- `router`
Expand Down
110 changes: 104 additions & 6 deletions pkg/stanza/operator/parser/container/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,108 @@ type Parser struct {
}

func (p *Parser) ProcessBatch(ctx context.Context, entries []*entry.Entry) error {
return p.TransformerOperator.ProcessBatchWith(ctx, entries, p.Process)
processedEntries := make([]*entry.Entry, 0, len(entries))
write := func(_ context.Context, ent *entry.Entry) error {
processedEntries = append(processedEntries, ent)
return nil
}
var errs []error
var criEntries []*entry.Entry

for _, ent := range entries {
skip, err := p.Skip(ctx, ent)
if err != nil {
errs = append(errs, p.HandleEntryErrorWithWrite(ctx, ent, err, write))
continue
}
if skip {
_ = write(ctx, ent)
continue
}

format := p.format
if format == "" {
format, err = p.detectFormat(ent)
if err != nil {
errs = append(errs, p.HandleEntryErrorWithWrite(ctx, ent, fmt.Errorf("failed to detect a valid container log format: %w", err), write))
continue
}
}

switch format {
case dockerFormat:
p.timeLayout = goTimeLayout
if err = p.ParseWith(ctx, ent, p.parseDocker, write); err != nil {
if p.OnError != helper.DropOnErrorQuiet && p.OnError != helper.SendOnErrorQuiet {
errs = append(errs, fmt.Errorf("failed to process the docker log: %w", err))
}
continue
}
if err = p.handleTimeAndAttributeMappings(ent); err != nil {
errs = append(errs, p.HandleEntryErrorWithWrite(ctx, ent, err, write))
continue
}
_ = write(ctx, ent)

case containerdFormat, crioFormat:
p.criConsumerStartOnce.Do(func() {
err = p.criLogEmitter.Start(nil)
if err != nil {
p.Logger().Error("unable to start the internal LogEmitter", zap.Error(err))
return
}
err = p.recombineParser.Start(nil)
if err != nil {
p.Logger().Error("unable to start the internal recombine operator", zap.Error(err))
return
}
p.asyncConsumerStarted = true
})

if format == containerdFormat {
err = p.ParseWith(ctx, ent, p.parseContainerd, write)
if err != nil {
if p.OnError != helper.DropOnErrorQuiet && p.OnError != helper.SendOnErrorQuiet {
errs = append(errs, fmt.Errorf("failed to parse containerd log: %w", err))
}
continue
}
p.timeLayout = goTimeLayout
} else {
err = p.ParseWith(ctx, ent, p.parseCRIO, write)
if err != nil {
if p.OnError != helper.DropOnErrorQuiet && p.OnError != helper.SendOnErrorQuiet {
errs = append(errs, fmt.Errorf("failed to parse crio log: %w", err))
}
continue
}
p.timeLayout = crioTimeLayout
}

if err = p.handleTimeAndAttributeMappings(ent); err != nil {
errs = append(errs, p.HandleEntryErrorWithWrite(ctx, ent, err, write))
continue
}
criEntries = append(criEntries, ent)

default:
errs = append(errs, p.HandleEntryErrorWithWrite(ctx, ent, errors.New("failed to detect a valid container log format"), write))
}
}

// Send CRI entries as a batch to recombine
if len(criEntries) > 0 {
if err := p.recombineParser.ProcessBatch(ctx, criEntries); err != nil {
errs = append(errs, fmt.Errorf("failed to recombine cri logs: %w", err))
}
}

// Write all docker/skipped entries as a batch
if len(processedEntries) > 0 {
errs = append(errs, p.WriteBatch(ctx, processedEntries))
}

return errors.Join(errs...)
}

// Process will parse an entry of Container logs
Expand Down Expand Up @@ -300,11 +401,8 @@ func (p *Parser) extractk8sMetaFromFilePath(e *entry.Entry) error {
}

func (p *Parser) consumeEntries(ctx context.Context, entries []*entry.Entry) {
for _, e := range entries {
err := p.Write(ctx, e)
if err != nil {
p.Logger().Error("failed to write entry", zap.Error(err))
}
if err := p.WriteBatch(ctx, entries); err != nil {
p.Logger().Error("failed to write batch of entries", zap.Error(err))
}
}

Expand Down
Loading