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

update add_kubernetes_metadata processor to use *logp.Logger #16913

Merged
merged 2 commits into from
Mar 10, 2020
Merged
Changes from 1 commit
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
Next Next commit
update add_kubernetes_metadata processor to use *logp.Logger
  • Loading branch information
kaiyan-sheng committed Mar 9, 2020
commit f7ef149553888a52b01501ce37402c88edbca357
22 changes: 12 additions & 10 deletions filebeat/processor/add_kubernetes_metadata/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const pathSeparator = string(os.PathSeparator)
type LogPathMatcher struct {
LogsPath string
ResourceType string
logger *logp.Logger
}

func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, error) {
Expand All @@ -67,10 +68,11 @@ func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, err
}
resourceType := config.ResourceType

logp.Debug("kubernetes", "logs_path matcher log path: %s", logPath)
logp.Debug("kubernetes", "logs_path matcher resource type: %s", resourceType)
log := logp.NewLogger("kubernetes")
log.Debugf("logs_path matcher log path: %s", logPath)
log.Debugf("logs_path matcher resource type: %s", resourceType)

return &LogPathMatcher{LogsPath: logPath, ResourceType: resourceType}, nil
return &LogPathMatcher{LogsPath: logPath, ResourceType: resourceType, logger: log}, nil
}

// Docker container ID is a 64-character-long hexadecimal string
Expand All @@ -83,10 +85,10 @@ func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string {
value, err := event.GetValue("log.file.path")
if err == nil {
source := value.(string)
logp.Debug("kubernetes", "Incoming log.file.path value: %s", source)
f.logger.Debugf("Incoming log.file.path value: %s", source)

if !strings.Contains(source, f.LogsPath) {
logp.Err("Error extracting container id - source value does not contain matcher's logs_path '%s'.", f.LogsPath)
f.logger.Errorf("Error extracting container id - source value does not contain matcher's logs_path '%s'.", f.LogsPath)
return ""
}

Expand All @@ -101,31 +103,31 @@ func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string {
if len(pathDirs) > podUIDPos {
podUID := strings.Split(source, pathSeparator)[podUIDPos]

logp.Debug("kubernetes", "Using pod uid: %s", podUID)
f.logger.Debugf("Using pod uid: %s", podUID)
return podUID
}

logp.Err("Error extracting pod uid - source value contains matcher's logs_path, however it is too short to contain a Pod UID.")
f.logger.Error("Error extracting pod uid - source value contains matcher's logs_path, however it is too short to contain a Pod UID.")
}
} else {
// In case of the Kubernetes log path "/var/log/containers/",
// the container ID will be located right before the ".log" extension.
if strings.HasPrefix(f.LogsPath, containerLogsPath()) && strings.HasSuffix(source, ".log") && sourceLen >= containerIdLen+4 {
containerIDEnd := sourceLen - 4
cid := source[containerIDEnd-containerIdLen : containerIDEnd]
logp.Debug("kubernetes", "Using container id: %s", cid)
f.logger.Debugf("Using container id: %s", cid)
return cid
}

// In any other case, we assume the container ID will follow right after the log path.
// However we need to check the length to prevent "slice bound out of range" runtime errors.
if sourceLen >= logsPathLen+containerIdLen {
cid := source[logsPathLen : logsPathLen+containerIdLen]
logp.Debug("kubernetes", "Using container id: %s", cid)
f.logger.Debugf("Using container id: %s", cid)
return cid
}

logp.Err("Error extracting container id - source value contains matcher's logs_path, however it is too short to contain a Docker container ID.")
f.logger.Error("Error extracting container id - source value contains matcher's logs_path, however it is too short to contain a Docker container ID.")
}
}

Expand Down