Skip to content

Commit

Permalink
add_kubernetes_metadata processor: format source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenWoltmann committed Aug 25, 2017
1 parent d63ee30 commit 55ad9ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
10 changes: 4 additions & 6 deletions filebeat/processor/add_kubernetes_metadata/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string {

// 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, "/var/log/containers/") &&
strings.HasSuffix(source, ".log") &&
sourceLen >= containerIdLen + 4 {
if strings.HasPrefix(f.LogsPath, "/var/log/containers/") && strings.HasSuffix(source, ".log") && sourceLen >= containerIdLen+4 {
containerIdEnd := sourceLen - 4
cid := source[containerIdEnd - containerIdLen : containerIdEnd]
cid := source[containerIdEnd-containerIdLen : containerIdEnd]
logp.Debug("kubernetes", "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]
if sourceLen >= logsPathLen+containerIdLen {
cid := source[logsPathLen : logsPathLen+containerIdLen]
logp.Debug("kubernetes", "Using container id: %s", cid)
return cid
}
Expand Down
20 changes: 10 additions & 10 deletions filebeat/processor/add_kubernetes_metadata/indexing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,45 @@ import (
const cid = "0069869de9adf97f574c62029aeba65d1ecd85a2a112e87fbc28afe4dec2b843"

func TestLogsPathMatcher_InvalidSource1(t *testing.T) {
cfgLogsPath := "" // use the default matcher configuration
cfgLogsPath := "" // use the default matcher configuration
source := "/var/log/messages"
expectedResult := ""
executeTest(t, cfgLogsPath, source, expectedResult);
executeTest(t, cfgLogsPath, source, expectedResult)
}

func TestLogsPathMatcher_InvalidSource2(t *testing.T) {
cfgLogsPath := "" // use the default matcher configuration
source := "/var/lib/docker/containers/01234567/89abcdef-json.log"
expectedResult := ""
executeTest(t, cfgLogsPath, source, expectedResult);
executeTest(t, cfgLogsPath, source, expectedResult)
}

func TestLogsPathMatcher_InvalidSource3(t *testing.T) {
cfgLogsPath := "/var/log/containers/"
source := "/var/log/containers/pod_ns_container_01234567.log"
expectedResult := ""
executeTest(t, cfgLogsPath, source, expectedResult);
executeTest(t, cfgLogsPath, source, expectedResult)
}

func TestLogsPathMatcher_VarLibDockerContainers(t *testing.T) {
cfgLogsPath := "" // use the default matcher configuration
source := fmt.Sprintf("/var/lib/docker/containers/%s/%s-json.log", cid, cid)
expectedResult := cid;
executeTest(t, cfgLogsPath, source, expectedResult);
expectedResult := cid
executeTest(t, cfgLogsPath, source, expectedResult)
}

func TestLogsPathMatcher_VarLogContainers(t *testing.T) {
cfgLogsPath := "/var/log/containers/"
source := fmt.Sprintf("/var/log/containers/kube-proxy-4d7nt_kube-system_kube-proxy-%s.log", cid)
expectedResult := cid;
executeTest(t, cfgLogsPath, source, expectedResult);
expectedResult := cid
executeTest(t, cfgLogsPath, source, expectedResult)
}

func TestLogsPathMatcher_AnotherLogDir(t *testing.T) {
cfgLogsPath := "/var/log/other/"
source := fmt.Sprintf("/var/log/other/%s.log", cid)
expectedResult := cid;
executeTest(t, cfgLogsPath, source, expectedResult);
expectedResult := cid
executeTest(t, cfgLogsPath, source, expectedResult)
}

func executeTest(t *testing.T, cfgLogsPath string, source string, expectedResult string) {
Expand Down

0 comments on commit 55ad9ab

Please sign in to comment.