-
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
Add container
input, deprecate docker
in favor of it
#12162
Changes from all commits
cdf0ec4
67644b2
47d4329
59137f7
dfd29cd
cf735ca
2933c41
a56e7f5
d7f3dda
9b686dd
2872f37
1236d5b
5ff8036
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
:type: container | ||
|
||
[id="{beatname_lc}-input-{type}"] | ||
=== Container input | ||
dedemorton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
++++ | ||
<titleabbrev>Container</titleabbrev> | ||
++++ | ||
|
||
Use the `container` input to read containers log files. | ||
|
||
This input searches for container logs under the given path, and parse them into | ||
common message lines, extracting timestamps too. Everything happens before line | ||
filtering, multiline, and JSON decoding, so this input can be used in | ||
combination with those settings. | ||
|
||
Example configuration: | ||
|
||
["source","yaml",subs="attributes"] | ||
---- | ||
{beatname_lc}.inputs: | ||
- type: container | ||
paths: <1> | ||
- '/var/lib/docker/containers/*/*.log' | ||
---- | ||
|
||
<1> `paths` is required. All other settings are optional. | ||
|
||
==== Configuration options | ||
|
||
The `container` input supports the following configuration options plus the | ||
<<{beatname_lc}-input-{type}-common-options>> described later. | ||
|
||
===== `stream` | ||
|
||
Reads from the specified streams only: `all`, `stdout` or `stderr`. The default | ||
is `all`. | ||
|
||
===== `format` | ||
|
||
Use the given format when reading the log file: `auto`, `docker` or `cri`. The | ||
default is `auto`, it will automatically detect the format. To disable | ||
autodetection set any of the other options. | ||
|
||
|
||
The following input configures {beatname_uc} to read the `stdout` stream from | ||
all containers under the default Kubernetes logs path: | ||
|
||
[source,yaml] | ||
---- | ||
- type: container | ||
stream: stdout | ||
paths: | ||
- "/var/log/containers/*.log" | ||
---- | ||
|
||
include::../inputs/input-common-harvester-options.asciidoc[] | ||
|
||
include::../inputs/input-common-file-options.asciidoc[] | ||
|
||
[id="{beatname_lc}-input-{type}-common-options"] | ||
include::../inputs/input-common-options.asciidoc[] | ||
|
||
:type!: |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package container | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var defaultConfig = config{ | ||
Stream: "all", | ||
Format: "auto", | ||
} | ||
|
||
type config struct { | ||
// Stream can be all, stdout or stderr | ||
Stream string `config:"stream"` | ||
|
||
// Format can be auto, cri, json-file | ||
Format string `config:"format"` | ||
} | ||
|
||
// Validate validates the config. | ||
func (c *config) Validate() error { | ||
if !stringInSlice(c.Stream, []string{"all", "stdout", "stderr"}) { | ||
return fmt.Errorf("invalid value for stream: %s, supported values are: all, stdout, stderr", c.Stream) | ||
} | ||
|
||
if !stringInSlice(strings.ToLower(c.Format), []string{"auto", "docker", "cri"}) { | ||
return fmt.Errorf("invalid value for format: %s, supported values are: auto, docker, cri", c.Format) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func stringInSlice(str string, list []string) bool { | ||
for _, v := range list { | ||
if v == str { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package container | ||
|
||
import ( | ||
"github.com/elastic/beats/filebeat/channel" | ||
"github.com/elastic/beats/filebeat/input" | ||
"github.com/elastic/beats/filebeat/input/log" | ||
"github.com/elastic/beats/libbeat/common" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func init() { | ||
err := input.Register("container", NewInput) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// NewInput creates a new container input | ||
func NewInput( | ||
cfg *common.Config, | ||
outletFactory channel.Connector, | ||
context input.Context, | ||
) (input.Input, error) { | ||
// Wrap log input with custom docker settings | ||
config := defaultConfig | ||
if err := cfg.Unpack(&config); err != nil { | ||
return nil, errors.Wrap(err, "reading container input config") | ||
} | ||
|
||
err := cfg.Merge(common.MapStr{ | ||
"docker-json.partial": true, | ||
"docker-json.cri_flags": true, | ||
|
||
// Allow stream selection (stdout/stderr/all) | ||
"docker-json.stream": config.Stream, | ||
|
||
// Select file format (auto/cri/docker) | ||
"docker-json.format": config.Format, | ||
|
||
// Set symlinks to true as CRI-O paths could point to symlinks instead of the actual path. | ||
"symlinks": true, | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "update input config") | ||
} | ||
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. A many setters can be replaced by a single call to 'Merge':
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. thanks for this, I didn't know it could be so simple! |
||
|
||
// Add stream to meta to ensure different state per stream | ||
if config.Stream != "all" { | ||
if context.Meta == nil { | ||
context.Meta = map[string]string{} | ||
} | ||
context.Meta["stream"] = config.Stream | ||
} | ||
|
||
return log.NewInput(cfg, outletFactory, context) | ||
} |
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.
Can we use a less docker-specific path here? Or add a
runtime
option that sets up the proper defaults depending on the runtime?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.
I'm currently working on a different PR to make autodiscover more CRI friendly, this is meant to keep it working as of now