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

fix symlink broken in container log filepath #1042

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion pkg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ require (
)

replace (
github.com/VictoriaMetrics/VictoriaMetrics => github.com/iLogtail/VictoriaMetrics v1.83.3-ilogtail
github.com/VictoriaMetrics/VictoriaMetrics => github.com/iLogtail/VictoriaMetrics v1.83.4-ilogtail
henryzhx8 marked this conversation as resolved.
Show resolved Hide resolved
github.com/VictoriaMetrics/metrics => github.com/iLogtail/metrics v1.23.0-ilogtail
github.com/aliyun/alibaba-cloud-sdk-go/services/sls_inner => ../external/github.com/aliyun/alibaba-cloud-sdk-go/services/sls_inner
github.com/elastic/beats/v7 => ../external/github.com/elastic/beats/v7
Expand Down
39 changes: 39 additions & 0 deletions pkg/helper/mount_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package helper

import (
"io/fs"
"os"
"strings"
)

var DefaultLogtailMountPath string
Expand All @@ -31,6 +33,43 @@ func GetMountedFilePathWithBasePath(basePath, logPath string) string {
return basePath + logPath
}

func TryGetRealPath(path string) (string, fs.FileInfo) {
sepLen := len(string(os.PathSeparator))
index := 0 // assume path is absolute
for i := 0; i < 10; i++ {
if f, err := os.Stat(path); err == nil {
return path, f
}
for {
j := strings.IndexRune(path[index+sepLen:], os.PathSeparator)
if j == -1 {
index = len(path)
} else {
index += j + sepLen
henryzhx8 marked this conversation as resolved.
Show resolved Hide resolved
}

f, err := os.Lstat(path[:index])
if err != nil {
return "", nil
}
if f.Mode()&os.ModeSymlink != 0 {
// path[:index] is a symlink
target, _ := os.Readlink(path[:index])
partialPath := GetMountedFilePath(target)
path = partialPath + path[index:]
if _, err := os.Stat(partialPath); err != nil {
// path referenced by partialPath does not exist or has symlink
index = 0
} else {
index = len(partialPath)
}
break
}
}
}
return "", nil
}

func init() {
defaultPath := "/logtail_host"
_, err := os.Stat(defaultPath)
Expand Down
8 changes: 8 additions & 0 deletions pkg/helper/mount_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package helper
import (
"flag"
"fmt"
"io/fs"
"os"
"strings"

Expand Down Expand Up @@ -118,3 +119,10 @@ func GetMountedFilePathWithBasePath(basePath, filePath string) string {
}
return basePath + filePath[colonPos+1:]
}

func TryGetRealPath(path string) (string, fs.FileInfo) {
if f, err := os.Stat(path); err == nil {
return path, f
}
return "", nil
}
7 changes: 3 additions & 4 deletions plugins/input/docker/stdout/input_docker_stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package stdout

import (
"fmt"
"os"
"regexp"
"sync"
"time"
Expand Down Expand Up @@ -85,9 +84,9 @@ func NewDockerFileSyner(sds *ServiceDockerStdout,
}

// first watch this container
stat, err := os.Stat(checkpoint.Path)
if err != nil {
logger.Warning(sds.context.GetRuntimeContext(), "DOCKER_STDOUT_STAT_ALARM", "stat log file error, path", checkpoint.Path, "error", err.Error())
realPath, stat := helper.TryGetRealPath(checkpoint.Path)
if realPath == "" {
logger.Warning(sds.context.GetRuntimeContext(), "DOCKER_STDOUT_STAT_ALARM", "stat log file error, path", checkpoint.Path, "error", "path not found")
henryzhx8 marked this conversation as resolved.
Show resolved Hide resolved
} else {
checkpoint.Offset = stat.Size()
if checkpoint.Offset > sds.StartLogMaxOffset {
Expand Down