Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Change the `event.created` in Netflow events to be the time the event was created by Filebeat
to be consistent with ECS. {pull}23094[23094]
- Update `filestream` reader offset when a line is skipped. {pull}23417[23417]
- Fix goroutines leak with some inputs in autodiscover. {pull}23722[23722]

*Filebeat*

Expand Down
48 changes: 35 additions & 13 deletions filebeat/fileset/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package fileset

import (
"fmt"

"github.com/gofrs/uuid"
"github.com/mitchellh/hashstructure"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
Expand All @@ -27,9 +30,6 @@ import (
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/monitoring"
"github.com/elastic/beats/v7/libbeat/outputs/elasticsearch"
pubpipeline "github.com/elastic/beats/v7/libbeat/publisher/pipeline"

"github.com/mitchellh/hashstructure"
)

var (
Expand Down Expand Up @@ -77,15 +77,9 @@ func NewFactory(

// Create creates a module based on a config
func (f *Factory) Create(p beat.PipelineConnector, c *common.Config) (cfgfile.Runner, error) {
// Start a registry of one module:
m, err := NewModuleRegistry([]*common.Config{c}, f.beatInfo, false)
if err != nil {
return nil, err
}

pConfigs, err := m.GetInputConfigs()
m, pConfigs, err := f.createRegistry(c)
if err != nil {
return nil, err
return nil, fmt.Errorf("create module registry for filesets: %w", err)
Copy link
Member

@ChrsMark ChrsMark Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("create module registry for filesets: %w", err)
return nil, fmt.Errorf("could not create module registry for filesets: %w", err)

Copy link

@urso urso Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on changing the wording that something did not work out

Disclaimer: I'm no native speaker

I'd prefer if our logs do not use didn't, couldn't or similar. Instead put some emphasis on the not by using could not, can not, must not. If something "Failed", let's use failed to

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 edited to use the full form of negation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, adding could not.

I have been changing my opinion on these kind of messages over time. Lately I am wondering if, when wrapping, it is not enough to mention what was being done. The wrapped error err already has the root cause, and the caller knows how it affects upper layers or the user, the context provided in places like here give an orientation of what was being done.

Thinking in a made up example, these are the kind of messages logged when one or the other strategy are used:

  • Failed to read configuration: could not read configuration from file /somefile: could not open file: permission denied.
  • Failed to read configuration: read configuration from file /somefile: open file: permission denied.

Both messages provide the same information: what failed, what was it trying to do and what was the root cause of the issue. In the first version the could nots don't provide additional information, and when many errors are chained they can look too redundant. Second option looks enough, more compact and easier to read.

In the code, the message looks more complete when mentioning that something didn't went as expected, but this information is actually redundant with the fact that an error is being wrapped and returned.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, part of the problem is we do not capture stack traces (or locations) on errors + generic error types normally do not carry additional 'context' (and we do not define our own error types when we should). Plus there is no generic support to add diagnostic context to add extra information to an error value (I'm using sderr like sderr.With("file", ...).Wrap(err, "read configuration") if I need this). Aaannnd we do not spend too much time thinking how the final log message might look like :)

If you look at the error from the stdlib, it includes the operation and the cause like open file: permission denied. In order to create user-readable errors one might use an encoding like <operation>: <optional context>: <optional message>: <cause>, where <cause> should follow a similar pattern.

Plus our logs are sometimes redundant to our error message. Following the aforementioned schema + reduce redundancy in the log (we do not need to mention that "reading configuration" failed), the log message would become:

Error: read configuration: filename /somefile: open file: permission denied.

Beside the Error and the permission denied there is no mention of failing, can not, or similar. The message more or less resembles that call stack (at the cost of having "readable" english).

But our logs/errors normally follow the schema Error doing X: failed to <message similar to X>: could not <message>: <cause>. Although it does not read nice, we start each sentence with "failed" and might want to keep that.

Some related readings:

}

// Hash module ID
Expand Down Expand Up @@ -116,8 +110,36 @@ func (f *Factory) Create(p beat.PipelineConnector, c *common.Config) (cfgfile.Ru
}

func (f *Factory) CheckConfig(c *common.Config) error {
_, err := f.Create(pubpipeline.NewNilPipeline(), c)
return err
_, pConfigs, err := f.createRegistry(c)
if err != nil {
return fmt.Errorf("create module registry for filesets: %w", err)
Copy link
Member

@ChrsMark ChrsMark Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("create module registry for filesets: %w", err)
return fmt.Errorf("could not create module registry for filesets: %w", err)

}

for _, pConfig := range pConfigs {
err = f.inputFactory.CheckConfig(pConfig)
if err != nil {
logp.Err("Error checking input configuration: %s", err)
return err
}
}

return nil
}

// createRegistry starts a registry for a set of filesets, it returns the registry and
// its input configurations
func (f *Factory) createRegistry(c *common.Config) (*ModuleRegistry, []*common.Config, error) {
m, err := NewModuleRegistry([]*common.Config{c}, f.beatInfo, false)
if err != nil {
return nil, nil, err
}

pConfigs, err := m.GetInputConfigs()
if err != nil {
return nil, nil, err
}

return m, pConfigs, err
}

func (p *inputsRunner) Start() {
Expand Down
36 changes: 36 additions & 0 deletions filebeat/input/container/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

// +build !integration

package container

import (
"os"
"path"
"testing"

"github.com/elastic/beats/v7/filebeat/input/inputtest"
"github.com/elastic/beats/v7/libbeat/common"
)

func TestNewInputDone(t *testing.T) {
config := common.MapStr{
"paths": path.Join(os.TempDir(), "logs", "*.log"),
}
inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config)
}
34 changes: 34 additions & 0 deletions filebeat/input/docker/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.

// +build !integration

package docker

import (
"testing"

"github.com/elastic/beats/v7/filebeat/input/inputtest"
"github.com/elastic/beats/v7/libbeat/common"
)

func TestNewInputDone(t *testing.T) {
config := common.MapStr{
"containers.ids": "fad130edd3d2",
}
inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config)
}
65 changes: 65 additions & 0 deletions filebeat/input/inputtest/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 inputtest

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/filebeat/channel"
"github.com/elastic/beats/v7/filebeat/input"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/tests/resources"
)

// Outlet is an empty outlet for testing.
type Outlet struct{}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have some "support" in order to create more extensive tests. e.g.

type Outlet struct {
  DoneSig chan struct{} // optional channel to simulate shutdown
  Event func(beat.Event) bool // optional callback
}

e.g. the callback can be used to 'record' events in unit tests in order to validate them afterwards.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, but we wouldn't be using these features here by now, wdyt about adding them when there is a use case?


func (o Outlet) OnEvent(event beat.Event) bool { return true }
func (o Outlet) Close() error { return nil }
func (o Outlet) Done() <-chan struct{} { return nil }

// Connector is a connector to a test empty outlet.
var Connector = channel.ConnectorFunc(
func(_ *common.Config, _ beat.ClientConfig) (channel.Outleter, error) {
return Outlet{}, nil
},
)

// AssertNotStartedInputCanBeDone checks that the context of an input can be
// done before starting the input, and it doesn't leak goroutines. This is
// important to confirm that leaks don't happen with CheckConfig.
func AssertNotStartedInputCanBeDone(t *testing.T, factory input.Factory, configMap *common.MapStr) {
goroutines := resources.NewGoroutinesChecker()
defer goroutines.Check(t)

config, err := common.NewConfigFrom(configMap)
require.NoError(t, err)

context := input.Context{
Done: make(chan struct{}),
}

_, err = factory(config, Connector, context)
assert.NoError(t, err)

close(context.Done)
}
36 changes: 36 additions & 0 deletions filebeat/input/kafka/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

// +build !integration

package kafka

import (
"testing"

"github.com/elastic/beats/v7/filebeat/input/inputtest"
"github.com/elastic/beats/v7/libbeat/common"
)

func TestNewInputDone(t *testing.T) {
config := common.MapStr{
"hosts": "localhost:9092",
"topics": "messages",
"group_id": "filebeat",
}
inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config)
}
7 changes: 4 additions & 3 deletions filebeat/input/log/input_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ package log
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/filebeat/input/file"
"github.com/elastic/beats/v7/filebeat/input/inputtest"
"github.com/elastic/beats/v7/libbeat/common/match"

"github.com/stretchr/testify/assert"
)

var matchTests = []struct {
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestInit(t *testing.T) {
Paths: test.paths,
},
states: file.NewStates(),
outlet: TestOutlet{},
outlet: inputtest.Outlet{},
fileStateIdentifier: &file.MockIdentifier{},
}

Expand Down
29 changes: 4 additions & 25 deletions filebeat/input/log/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/elastic/beats/v7/filebeat/channel"
"github.com/elastic/beats/v7/filebeat/input"
"github.com/elastic/beats/v7/filebeat/input/file"
"github.com/elastic/beats/v7/filebeat/input/inputtest"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/match"
Expand Down Expand Up @@ -185,25 +186,10 @@ func testInputLifecycle(t *testing.T, context input.Context, closer func(input.C
}

func TestNewInputDone(t *testing.T) {
goroutines := resources.NewGoroutinesChecker()
defer goroutines.Check(t)

config, _ := common.NewConfigFrom(common.MapStr{
config := common.MapStr{
"paths": path.Join(os.TempDir(), "logs", "*.log"),
})

connector := channel.ConnectorFunc(func(_ *common.Config, _ beat.ClientConfig) (channel.Outleter, error) {
return TestOutlet{}, nil
})

context := input.Context{
Done: make(chan struct{}),
}

_, err := NewInput(config, connector, context)
assert.NoError(t, err)

close(context.Done)
inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config)
}

func TestNewInputError(t *testing.T) {
Expand All @@ -213,7 +199,7 @@ func TestNewInputError(t *testing.T) {
config := common.NewConfig()

connector := channel.ConnectorFunc(func(_ *common.Config, _ beat.ClientConfig) (channel.Outleter, error) {
return TestOutlet{}, nil
return inputtest.Outlet{}, nil
})

context := input.Context{}
Expand Down Expand Up @@ -318,10 +304,3 @@ func (o *eventCapturer) Close() error {
func (o *eventCapturer) Done() <-chan struct{} {
return o.c
}

// TestOutlet is an empty outlet for testing
type TestOutlet struct{}

func (o TestOutlet) OnEvent(event beat.Event) bool { return true }
func (o TestOutlet) Close() error { return nil }
func (o TestOutlet) Done() <-chan struct{} { return nil }
8 changes: 8 additions & 0 deletions filebeat/input/mqtt/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/stretchr/testify/require"

finput "github.com/elastic/beats/v7/filebeat/input"
"github.com/elastic/beats/v7/filebeat/input/inputtest"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/backoff"
Expand Down Expand Up @@ -322,6 +323,13 @@ func TestOnCreateHandler_SubscribeMultiple_BackoffSignalDone(t *testing.T) {
require.Equal(t, 1, mockedBackoff.resetCount)
}

func TestNewInputDone(t *testing.T) {
config := common.MapStr{
"hosts": "tcp://:0",
}
inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config)
}

func assertEventMatches(t *testing.T, expected mockedMessage, got beat.Event) {
topic, err := got.GetValue("mqtt.topic")
require.NoError(t, err)
Expand Down
34 changes: 34 additions & 0 deletions filebeat/input/redis/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.

// +build !integration

package redis

import (
"testing"

"github.com/elastic/beats/v7/filebeat/input/inputtest"
"github.com/elastic/beats/v7/libbeat/common"
)

func TestNewInputDone(t *testing.T) {
config := common.MapStr{
"hosts": "localhost:3679",
}
inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config)
}
Loading