forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 4
/
indexing.go
66 lines (52 loc) · 1.68 KB
/
indexing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package add_kubernetes_metadata
import (
"fmt"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/processors/add_kubernetes_metadata"
)
func init() {
add_kubernetes_metadata.Indexing.AddMatcher(LogPathMatcherName, newLogsPathMatcher)
cfg := common.NewConfig()
//Add a container indexer config by default.
add_kubernetes_metadata.Indexing.AddDefaultIndexerConfig(add_kubernetes_metadata.ContainerIndexerName, *cfg)
//Add a log path matcher which can extract container ID from the "source" field.
add_kubernetes_metadata.Indexing.AddDefaultMatcherConfig(LogPathMatcherName, *cfg)
}
const LogPathMatcherName = "logs_path"
type LogPathMatcher struct {
LogsPath string
}
func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, error) {
config := struct {
LogsPath string `config:"logs_path"`
}{
LogsPath: "/var/lib/docker/containers/",
}
err := cfg.Unpack(&config)
if err != nil || config.LogsPath == "" {
return nil, fmt.Errorf("fail to unpack the `logs_path` configuration: %s", err)
}
logPath := config.LogsPath
if logPath[len(logPath)-1:] != "/" {
logPath = logPath + "/"
}
return &LogPathMatcher{LogsPath: logPath}, nil
}
func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string {
if value, ok := event["source"]; ok {
source := value.(string)
logp.Debug("kubernetes", "Incoming source value: ", source)
cid := ""
if strings.Contains(source, f.LogsPath) {
//Docker container is 64 chars in length
cid = source[len(f.LogsPath) : len(f.LogsPath)+64]
}
logp.Debug("kubernetes", "Using container id: ", cid)
if cid != "" {
return cid
}
}
return ""
}