Skip to content

Commit

Permalink
move fingerprint header labels to Reader type. Add logic to ensure He…
Browse files Browse the repository at this point in the history
…aderLabels are persisted to the datbase
  • Loading branch information
jsirianni committed Aug 20, 2021
1 parent a251b3c commit 33e6a8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
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

0 comments on commit 33e6a8f

Please sign in to comment.