Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label regex persistence #399

Merged
merged 1 commit into from
Aug 21, 2021
Merged
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
6 changes: 0 additions & 6 deletions operator/builtin/input/file/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ const minFingerprintSize = 16 // bytes
type Fingerprint struct {
// FirstBytes represents the first N bytes of a file
FirstBytes []byte

// Labels is an optional map that contains entry labels
// added to every record from a given file
Labels map[string]string
}

// NewFingerprint creates a new fingerprint from an open file
Expand All @@ -35,8 +31,6 @@ func (f *InputOperator) NewFingerprint(file *os.File) (*Fingerprint, error) {
FirstBytes: buf[:n],
}

fp.Labels = make(map[string]string)

return fp, nil
}

Expand Down
16 changes: 12 additions & 4 deletions operator/builtin/input/file/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type Reader struct {
Fingerprint *Fingerprint
Offset int64

// HeaderLabels is an optional map that contains entry labels
// derived from a log files' headers, added to every record
HeaderLabels map[string]string

generation int
fileInput *InputOperator
file *os.File
Expand All @@ -64,6 +68,7 @@ type Reader struct {
func (f *InputOperator) NewReader(path string, file *os.File, fp *Fingerprint) (*Reader, error) {
r := &Reader{
Fingerprint: fp,
HeaderLabels: make(map[string]string),
file: file,
fileInput: f,
SugaredLogger: f.SugaredLogger.With("path", path),
Expand All @@ -81,6 +86,9 @@ func (f *Reader) Copy(file *os.File) (*Reader, error) {
return nil, err
}
reader.Offset = f.Offset
for k, v := range f.HeaderLabels {
reader.HeaderLabels[k] = v
}
return reader, nil
}

Expand Down Expand Up @@ -155,10 +163,10 @@ func (f *Reader) readHeaders(ctx context.Context, msgBuf []byte) error {
for i, byteSlice := range byteMatches {
matches[i] = string(byteSlice)
}
if f.Fingerprint.Labels == nil {
f.Fingerprint.Labels = make(map[string]string)
if f.HeaderLabels == nil {
f.HeaderLabels = make(map[string]string)
}
f.Fingerprint.Labels[matches[1]] = matches[2]
f.HeaderLabels[matches[1]] = matches[2]
return nil
}

Expand Down Expand Up @@ -204,7 +212,7 @@ func (f *Reader) emit(ctx context.Context, msgBuf []byte) error {
}

// Set W3C headers as labels
for k, v := range f.Fingerprint.Labels {
for k, v := range f.HeaderLabels {
field := entry.NewLabelField(k)
if err := e.Set(field, v); err != nil {
return err
Expand Down