-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Kubernetes annotator #3888
Merged
+184,909
−213
Merged
Kubernetes annotator #3888
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/libbeat/processors/annotate/kubernetes" | ||
) | ||
|
||
func init() { | ||
kubernetes.Indexing.AddMatcher(LogPathMatcherName, newLogsPathMatcher) | ||
|
||
indexer := kubernetes.Indexing.GetIndexer(kubernetes.ContainerIndexerName) | ||
//Add a container indexer by default. | ||
if indexer != nil { | ||
cfg := common.NewConfig() | ||
container, err := indexer(*cfg) | ||
|
||
if err == nil { | ||
kubernetes.Indexing.AddDefaultIndexer(container) | ||
} else { | ||
logp.Err("Unable to load indexer plugin due to error: %v", err) | ||
} | ||
} else { | ||
logp.Err("Unable to get indexer plugin %s", kubernetes.ContainerIndexerName) | ||
} | ||
|
||
//Add a log path matcher which can extract container ID from the "source" field. | ||
matcher := kubernetes.Indexing.GetMatcher(LogPathMatcherName) | ||
|
||
if matcher != nil { | ||
cfg := common.NewConfig() | ||
logsPathMatcher, err := matcher(*cfg) | ||
if err == nil { | ||
kubernetes.Indexing.AddDefaultMatcher(logsPathMatcher) | ||
} else { | ||
logp.Err("Unable to load matcher plugin due to error: %v", err) | ||
} | ||
} else { | ||
logp.Err("Unable to get matcher plugin %s", LogPathMatcherName) | ||
} | ||
|
||
} | ||
|
||
const LogPathMatcherName = "logs_path" | ||
|
||
type LogPathMatcher struct { | ||
LogsPath string | ||
} | ||
|
||
func newLogsPathMatcher(cfg common.Config) (kubernetes.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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestLogsPathMatcher(t *testing.T) { | ||
var testConfig = common.NewConfig() | ||
|
||
logMatcher, err := newLogsPathMatcher(*testConfig) | ||
assert.Nil(t, err) | ||
|
||
cid := "0069869de9adf97f574c62029aeba65d1ecd85a2a112e87fbc28afe4dec2b843" | ||
logPath := fmt.Sprintf("/var/lib/docker/containers/%s/%s-json.log", cid, cid) | ||
|
||
input := common.MapStr{ | ||
"source": "/var/log/messages", | ||
} | ||
|
||
output := logMatcher.MetadataIndex(input) | ||
assert.Equal(t, output, "") | ||
|
||
input["source"] = logPath | ||
output = logMatcher.MetadataIndex(input) | ||
|
||
assert.Equal(t, output, cid) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we split the code into 2
func init()
functions which I think Golang allows, we could make the code quite a bit nicer. Also it allows to check forerr != nil
and then return which is more the "standard" golang way.