-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Keep different registry entry per container stream #7281
Changes from all commits
9d7b607
3193611
8cfe2d7
b1e4c94
46c9c45
a2785d2
f66d075
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,31 @@ package file | |
|
||
import ( | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/mitchellh/hashstructure" | ||
|
||
"github.com/elastic/beats/libbeat/common/file" | ||
) | ||
|
||
// State is used to communicate the reading state of a file | ||
type State struct { | ||
Id string `json:"-"` // local unique id to make comparison more efficient | ||
Finished bool `json:"-"` // harvester state | ||
Fileinfo os.FileInfo `json:"-"` // the file info | ||
Source string `json:"source"` | ||
Offset int64 `json:"offset"` | ||
Timestamp time.Time `json:"timestamp"` | ||
TTL time.Duration `json:"ttl"` | ||
Type string `json:"type"` | ||
Id string `json:"-"` // local unique id to make comparison more efficient | ||
Finished bool `json:"-"` // harvester state | ||
Fileinfo os.FileInfo `json:"-"` // the file info | ||
Source string `json:"source"` | ||
Offset int64 `json:"offset"` | ||
Timestamp time.Time `json:"timestamp"` | ||
TTL time.Duration `json:"ttl"` | ||
Type string `json:"type"` | ||
Meta map[string]string `json:"meta"` | ||
FileStateOS file.StateOS | ||
} | ||
|
||
// NewState creates a new file state | ||
func NewState(fileInfo os.FileInfo, path string, t string) State { | ||
func NewState(fileInfo os.FileInfo, path string, t string, meta map[string]string) State { | ||
return State{ | ||
Fileinfo: fileInfo, | ||
Source: path, | ||
|
@@ -30,15 +35,33 @@ func NewState(fileInfo os.FileInfo, path string, t string) State { | |
Timestamp: time.Now(), | ||
TTL: -1, // By default, state does have an infinite ttl | ||
Type: t, | ||
Meta: meta, | ||
} | ||
} | ||
|
||
// ID returns a unique id for the state as a string | ||
func (s *State) ID() string { | ||
// Generate id on first request. This is needed as id is not set when converting back from json | ||
if s.Id == "" { | ||
s.Id = s.FileStateOS.String() | ||
if s.Meta == nil { | ||
s.Id = s.FileStateOS.String() | ||
} else { | ||
hashValue, _ := hashstructure.Hash(s.Meta, nil) | ||
var hashBuf [17]byte | ||
hash := strconv.AppendUint(hashBuf[:0], hashValue, 16) | ||
hash = append(hash, '-') | ||
|
||
fileID := s.FileStateOS.String() | ||
|
||
var b strings.Builder | ||
b.Grow(len(hash) + len(fileID)) | ||
b.Write(hash) | ||
b.WriteString(fileID) | ||
|
||
s.Id = b.String() | ||
} | ||
} | ||
|
||
return s.Id | ||
} | ||
|
||
|
@@ -49,5 +72,8 @@ func (s *State) IsEqual(c *State) bool { | |
|
||
// IsEmpty returns true if the state is empty | ||
func (s *State) IsEmpty() bool { | ||
return *s == State{} | ||
return s.FileStateOS == file.StateOS{} && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you share some details here on why not only add s.Meta but also the other chnages? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I added a map to the |
||
s.Source == "" && | ||
s.Meta == nil && | ||
s.Timestamp.IsZero() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1506,3 +1506,58 @@ def test_registrar_files_with_input_level_processors(self): | |
"inode": stat.st_ino, | ||
"device": stat.st_dev, | ||
}, file_state_os) | ||
|
||
def test_registrar_meta(self): | ||
""" | ||
Check that multiple entries for the same file are on the registry when they have | ||
different meta | ||
""" | ||
|
||
self.render_config_template( | ||
type='docker', | ||
input_raw=''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw. check out the |
||
containers: | ||
path: {path} | ||
stream: stdout | ||
ids: | ||
- container_id | ||
- type: docker | ||
containers: | ||
path: {path} | ||
stream: stderr | ||
ids: | ||
- container_id | ||
'''.format(path=os.path.abspath(self.working_dir) + "/log/") | ||
) | ||
os.mkdir(self.working_dir + "/log/") | ||
os.mkdir(self.working_dir + "/log/container_id") | ||
testfile_path1 = self.working_dir + "/log/container_id/test.log" | ||
|
||
with open(testfile_path1, 'w') as f: | ||
for i in range(0, 10): | ||
f.write('{"log":"hello\\n","stream":"stdout","time":"2018-04-13T13:39:57.924216596Z"}\n') | ||
f.write('{"log":"hello\\n","stream":"stderr","time":"2018-04-13T13:39:57.924216596Z"}\n') | ||
|
||
filebeat = self.start_beat() | ||
|
||
self.wait_until( | ||
lambda: self.output_has(lines=20), | ||
max_timeout=15) | ||
|
||
# wait until the registry file exist. Needed to avoid a race between | ||
# the logging and actual writing the file. Seems to happen on Windows. | ||
|
||
self.wait_until( | ||
lambda: os.path.isfile(os.path.join(self.working_dir, | ||
"registry")), | ||
max_timeout=1) | ||
|
||
filebeat.check_kill_and_wait() | ||
|
||
# Check registry contains 2 entries with meta | ||
data = self.get_registry() | ||
assert len(data) == 2 | ||
assert data[0]["source"] == data[1]["source"] | ||
assert data[0]["meta"]["stream"] in ("stdout", "stderr") | ||
assert data[1]["meta"]["stream"] in ("stdout", "stderr") | ||
assert data[0]["meta"]["stream"] != data[1]["meta"]["stream"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct field Id should be ID