Skip to content

Commit

Permalink
add include_file_infos on file log consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
tprelle committed Mar 27, 2024
1 parent 80bbf5e commit 7e9c730
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .chloggen/add_include_file_infos.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. filelogreceiver)
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`.

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

# (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]
24 changes: 24 additions & 0 deletions pkg/stanza/fileconsumer/attrs/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ package attrs // import "github.com/open-telemetry/opentelemetry-collector-contr

import (
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"syscall"
)

const (
LogFileName = "log.file.name"
LogFilePath = "log.file.path"
LogFileNameResolved = "log.file.name_resolved"
LogFilePathResolved = "log.file.path_resolved"
LogFileOwner = "log.file.owner"
LogFileGroup = "log.file.group"
)

type Resolver struct {
IncludeFileName bool `mapstructure:"include_file_name,omitempty"`
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"`
}

func (r *Resolver) Resolve(path string) (attributes map[string]any, err error) {
Expand All @@ -32,6 +38,24 @@ func (r *Resolver) Resolve(path string) (attributes map[string]any, err error) {
if r.IncludeFilePath {
attributes[LogFilePath] = path
}
if r.IncludeFileInfos {
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
}
var fileGroup, errFileGroup = user.LookupGroupId(fmt.Sprint(fileStat.Gid))
if errFileGroup == nil {
attributes[LogFileGroup] = fileGroup.Name
}
}
defer file.Close()
}
}
if !r.IncludeFileNameResolved && !r.IncludeFilePathResolved {
return attributes, nil
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/stanza/fileconsumer/attrs/attrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ import (
func TestResolver(t *testing.T) {
t.Parallel()

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

// Create a 4 bit string where each bit represents the value of a config option
bitString := fmt.Sprintf("%04b", i)
bitString := fmt.Sprintf("%05b", 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',
}

t.Run(bitString, func(t *testing.T) {
Expand Down Expand Up @@ -67,6 +68,17 @@ func TestResolver(t *testing.T) {
} else {
assert.Empty(t, attributes[LogFilePathResolved])
}
if r.IncludeFileInfos {
expectLen++
assert.NotNil(t, attributes[LogFileOwner])
assert.IsType(t, "", attributes[LogFileOwner])
expectLen++
assert.NotNil(t, attributes[LogFileGroup])
assert.IsType(t, "", attributes[LogFileGroup])
} else {
assert.Empty(t, attributes[LogFileOwner])
assert.Empty(t, attributes[LogFileOwner])
}
assert.Equal(t, expectLen, len(attributes))
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/stanza/fileconsumer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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)
}

func TestUnmarshal(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/stanza/operator/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package file

import (
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"syscall"
"testing"
"time"

Expand All @@ -28,6 +31,7 @@ func TestAddFileResolvedFields(t *testing.T) {
cfg.IncludeFilePath = true
cfg.IncludeFileNameResolved = true
cfg.IncludeFilePathResolved = true
cfg.IncludeFileInfos = true
})

// Create temp dir with log file
Expand Down Expand Up @@ -63,6 +67,12 @@ func TestAddFileResolvedFields(t *testing.T) {
require.Equal(t, symLinkPath, e.Attributes["log.file.path"])
require.Equal(t, filepath.Base(resolved), e.Attributes["log.file.name_resolved"])
require.Equal(t, resolved, e.Attributes["log.file.path_resolved"])
var fileInfo, _ = file.Stat()
var fileStat = fileInfo.Sys().(*syscall.Stat_t)
var fileOwner, _ = user.LookupId(fmt.Sprint(fileStat.Uid))
require.Equal(t, fileOwner.Username, e.Attributes["log.file.owner"])
var fileGroup, _ = user.LookupGroupId(fmt.Sprint(fileStat.Gid))
require.Equal(t, fileGroup.Name, e.Attributes["log.file.group"])
}

// ReadExistingLogs tests that, when starting from beginning, we
Expand Down
1 change: 1 addition & 0 deletions receiver/filelogreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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`. |
| `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 7e9c730

Please sign in to comment.