Skip to content

Commit

Permalink
split include_file_infos into include_file_owner and include_file_group
Browse files Browse the repository at this point in the history
  • Loading branch information
tprelle committed Mar 27, 2024
1 parent 7e9c730 commit 2e6af9f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .chloggen/add_include_file_infos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ change_type: enhancement
component: filelogreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: If include_file_infos is true when reading a file on filelogreceiver, it will add the file owner as the attribute `log.file.owner` and the file group as the attribute `log.file.group`.
note: When reading a file on filelogreceiver, if include_file_owner is true, it will add the file owner as the attribute `log.file.owner` and if include_file_group is true, it will add the file group as the attribute `log.file.group`.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [30775]
Expand Down
2 changes: 2 additions & 0 deletions pkg/stanza/docs/operators/file_input.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The `file_input` operator reads logs from files. It will place the lines read in
| `include_file_path` | `false` | Whether to add the file path as the attribute `log.file.path`. |
| `include_file_name_resolved` | `false` | Whether to add the file name after symlinks resolution as the attribute `log.file.name_resolved`. |
| `include_file_path_resolved` | `false` | Whether to add the file path after symlinks resolution as the attribute `log.file.path_resolved`. |
| `include_file_owner` | `false` | Whether to add the file owner as the attribute `log.file.owner` |
| `include_file_group` | `false` | Whether to add the file group as the attribute `log.file.group` |
| `preserve_leading_whitespaces` | `false` | Whether to preserve leading whitespaces. |
| `preserve_trailing_whitespaces` | `false` | Whether to preserve trailing whitespaces. |
| `start_at` | `end` | At startup, where to start reading logs from the file. Options are `beginning` or `end`. This setting will be ignored if previously read file offsets are retrieved from a persistence mechanism. |
Expand Down
21 changes: 13 additions & 8 deletions pkg/stanza/fileconsumer/attrs/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Resolver struct {
IncludeFilePath bool `mapstructure:"include_file_path,omitempty"`
IncludeFileNameResolved bool `mapstructure:"include_file_name_resolved,omitempty"`
IncludeFilePathResolved bool `mapstructure:"include_file_path_resolved,omitempty"`
IncludeFileInfos bool `mapstructure:"include_file_infos,omitempty"`
IncludeFileOwner bool `mapstructure:"include_file_owner,omitempty"`
IncludeFileGroup bool `mapstructure:"include_file_group,omitempty"`
}

func (r *Resolver) Resolve(path string) (attributes map[string]any, err error) {
Expand All @@ -38,19 +39,23 @@ func (r *Resolver) Resolve(path string) (attributes map[string]any, err error) {
if r.IncludeFilePath {
attributes[LogFilePath] = path
}
if r.IncludeFileInfos {
if r.IncludeFileOwner || r.IncludeFileGroup {
var file, fileErr = os.OpenFile(fmt.Sprint(path), os.O_RDONLY, 0000)
if fileErr == nil {
var fileInfo, errStat = file.Stat()
if errStat == nil {
var fileStat = fileInfo.Sys().(*syscall.Stat_t)
var fileOwner, errFileUser = user.LookupId(fmt.Sprint(fileStat.Uid))
if errFileUser == nil {
attributes[LogFileOwner] = fileOwner.Username
if r.IncludeFileOwner {
var fileOwner, errFileUser = user.LookupId(fmt.Sprint(fileStat.Uid))
if errFileUser == nil {
attributes[LogFileOwner] = fileOwner.Username
}
}
var fileGroup, errFileGroup = user.LookupGroupId(fmt.Sprint(fileStat.Gid))
if errFileGroup == nil {
attributes[LogFileGroup] = fileGroup.Name
if r.IncludeFileGroup {
var fileGroup, errFileGroup = user.LookupGroupId(fmt.Sprint(fileStat.Gid))
if errFileGroup == nil {
attributes[LogFileGroup] = fileGroup.Name
}
}
}
defer file.Close()
Expand Down
18 changes: 12 additions & 6 deletions pkg/stanza/fileconsumer/attrs/attrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ import (
func TestResolver(t *testing.T) {
t.Parallel()

for i := 0; i < 32; i++ {
for i := 0; i < 64; i++ {

// Create a 4 bit string where each bit represents the value of a config option
bitString := fmt.Sprintf("%05b", i)
bitString := fmt.Sprintf("%06b", i)

// Create a resolver with a config that matches the bit pattern of i
r := Resolver{
IncludeFileName: bitString[0] == '1',
IncludeFilePath: bitString[1] == '1',
IncludeFileNameResolved: bitString[2] == '1',
IncludeFilePathResolved: bitString[3] == '1',
IncludeFileInfos: bitString[4] == '1',
IncludeFileOwner: bitString[4] == '1',
IncludeFileGroup: bitString[5] == '1',
}

t.Run(bitString, func(t *testing.T) {
Expand Down Expand Up @@ -68,16 +69,21 @@ func TestResolver(t *testing.T) {
} else {
assert.Empty(t, attributes[LogFilePathResolved])
}
if r.IncludeFileInfos {
if r.IncludeFileOwner {
expectLen++
assert.NotNil(t, attributes[LogFileOwner])
assert.IsType(t, "", attributes[LogFileOwner])
} else {
assert.Empty(t, attributes[LogFileOwner])
assert.Empty(t, attributes[LogFileOwner])
}
if r.IncludeFileGroup {
expectLen++
assert.NotNil(t, attributes[LogFileGroup])
assert.IsType(t, "", attributes[LogFileGroup])
} else {
assert.Empty(t, attributes[LogFileOwner])
assert.Empty(t, attributes[LogFileOwner])
assert.Empty(t, attributes[LogFileGroup])
assert.Empty(t, attributes[LogFileGroup])
}
assert.Equal(t, expectLen, len(attributes))
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/stanza/fileconsumer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func TestNewConfig(t *testing.T) {
assert.False(t, cfg.IncludeFilePath)
assert.False(t, cfg.IncludeFileNameResolved)
assert.False(t, cfg.IncludeFilePathResolved)
assert.False(t, cfg.IncludeFileInfos)
assert.False(t, cfg.IncludeFileOwner)
assert.False(t, cfg.IncludeFileGroup)
}

func TestUnmarshal(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/stanza/operator/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func TestAddFileResolvedFields(t *testing.T) {
cfg.IncludeFilePath = true
cfg.IncludeFileNameResolved = true
cfg.IncludeFilePathResolved = true
cfg.IncludeFileInfos = true
cfg.IncludeFileOwner = true
cfg.IncludeFileGroup = true
})

// Create temp dir with log file
Expand Down
3 changes: 2 additions & 1 deletion receiver/filelogreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Tails and parses logs from files.
| `include_file_path` | `false` | Whether to add the file path as the attribute `log.file.path`. |
| `include_file_name_resolved` | `false` | Whether to add the file name after symlinks resolution as the attribute `log.file.name_resolved`. |
| `include_file_path_resolved` | `false` | Whether to add the file path after symlinks resolution as the attribute `log.file.path_resolved`. |
| `include_file_infos` | `false` | Whether to add the file owner as the attribute `log.file.owner` and the file group as the attribute `log.file.group`. |
| `include_file_owner` | `false` | Whether to add the file owner as the attribute `log.file.owner` |
| `include_file_group` | `false` | Whether to add the file group as the attribute `log.file.group` |
| `poll_interval` | 200ms | The [duration](#time-parameters) between filesystem polls. |
| `fingerprint_size` | `1kb` | The number of bytes with which to identify a file. The first bytes in the file are used as the fingerprint. Decreasing this value at any point will cause existing fingerprints to forgotten, meaning that all files will be read from the beginning (one time) |
| `max_log_size` | `1MiB` | The maximum size of a log entry to read. A log entry will be truncated if it is larger than `max_log_size`. Protects against reading large amounts of data into memory. |
Expand Down
10 changes: 5 additions & 5 deletions receiver/otlpjsonfilereceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
otlpjsonfile:
include:
- "/var/log/*.log"
- "/var/log/*.log"
exclude:
- "/var/log/example.log"
- "/var/log/example.log"
otlpjsonfile/all:
include_file_name: true
include_file_path: true
Expand All @@ -17,7 +17,7 @@ otlpjsonfile/all:
line_start_pattern: "<"
line_end_pattern: ">"
include:
- "/var/log/*.log"
- "/tmp/*.log"
- "/var/log/*.log"
- "/tmp/*.log"
exclude:
- "/var/log/example.log"
- "/var/log/example.log"

0 comments on commit 2e6af9f

Please sign in to comment.