diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f28d44eea..0d07fb100b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,4 +43,5 @@ your changes, such as: - [public] [both] [added] add new plugin: input_command - [public] [both] [added] add new plugin: processor_log_to_sls_metric - [public] [both] [fixed] fix service_go_profile nil panic -- [public] [both] [fixed] service_prometheus support scale in kubernetes \ No newline at end of file +- [public] [both] [fixed] fix broken container log path link +- [public] [both] [fixed] service_prometheus support scale in kubernetes diff --git a/pkg/go.mod b/pkg/go.mod index 36e8a755f7..afcc9c8cdb 100644 --- a/pkg/go.mod +++ b/pkg/go.mod @@ -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 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 diff --git a/pkg/helper/mount_others.go b/pkg/helper/mount_others.go index 74aacf64b4..b3596d815f 100644 --- a/pkg/helper/mount_others.go +++ b/pkg/helper/mount_others.go @@ -18,7 +18,9 @@ package helper import ( + "io/fs" "os" + "strings" ) var DefaultLogtailMountPath string @@ -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 + } + + 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) diff --git a/pkg/helper/mount_windows.go b/pkg/helper/mount_windows.go index bfa3398d01..4ac7673c58 100644 --- a/pkg/helper/mount_windows.go +++ b/pkg/helper/mount_windows.go @@ -17,6 +17,7 @@ package helper import ( "flag" "fmt" + "io/fs" "os" "strings" @@ -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 +} diff --git a/plugins/input/docker/stdout/input_docker_stdout.go b/plugins/input/docker/stdout/input_docker_stdout.go index f83e9b2ff7..8ae5e70594 100644 --- a/plugins/input/docker/stdout/input_docker_stdout.go +++ b/plugins/input/docker/stdout/input_docker_stdout.go @@ -16,7 +16,6 @@ package stdout import ( "fmt" - "os" "regexp" "sync" "time" @@ -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") } else { checkpoint.Offset = stat.Size() if checkpoint.Offset > sds.StartLogMaxOffset {