-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `container` input, deprecate `docker` in favor of it This change adds a new container input for better support of CRI based scenarios. `docker` input was acting as a catch all input for all container related cases, but its config options were very opinionated towards Docker, with some issues: * `containers.ids` setting was good to abstract logs path, but we have seen many cases were logs are not under default location, or follow a different path pattern (ie CRI logs). * `containers.*` settings have shown counter intuitive for many users, in many cases we have seen people writing `container.*` instead, ending up in a config error. * Some existing settings (`combine_partials`, `cri.parse_flags`) were introduced as a way to offer a backwards compatible upgrades, but it doesn't really make sense to disable them, as they handle actual format behaviors. This new `container` input offers the same wrapper to read log files from containers with the following changes: * It exposes `paths` as the `log` input, instead of `containers.ids` and `containers.path`. * `parse_flags` and `combine_partials` are hardcoded, as there is no good reason to disable them. * `stream` selector is still available, under root settings. * It allows to select the log format (also atodetect it), giving room for future format changes. `format` can be `auto` (default), `docker` and `CRI`. Example configurations: Get Docker logs: ``` filebeat.inputs: - type: container paths: - /var/lib/docker/containers/*/*.log ``` Get Kubernetes logs: ``` filebeat.inputs: - type: container paths: - /var/log/pods/*/*/*.log # this could also be used: #- /var/log/containers/*.log ``` Previous `docker` input is deprecated in favor of this, to be removed in 8.0
- Loading branch information
Carlos Pérez-Aradros Herce
authored
May 20, 2019
1 parent
9c848a9
commit 313e6d1
Showing
17 changed files
with
328 additions
and
66 deletions.
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,64 @@ | ||
:type: container | ||
|
||
[id="{beatname_lc}-input-{type}"] | ||
=== Container input | ||
|
||
++++ | ||
<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!: |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 | ||
} |
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,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") | ||
} | ||
|
||
// 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) | ||
} |
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
Oops, something went wrong.