Skip to content
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

No condition required for autodiscover. Bool condition added #8897

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions heartbeat/heartbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@

############################# Heartbeat ######################################

# Configure monitors
heartbeat.monitors:
- type: http

# List or urls to query
urls: ["http://localhost:9200"]

# Configure task schedule
schedule: '@every 10s'

# Total test connection and data exchange timeout
#timeout: 16s
heartbeat.autodiscover:
providers:
- type: docker
templates:
- config:
- type: tcp
hosts: ["${data.host}:${data.port}"]
schedule: "@every 1s"
timeout: 1s

#==================== Elasticsearch template setting ==========================

Expand Down
19 changes: 12 additions & 7 deletions libbeat/autodiscover/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ type MapperSettings []*struct {
func NewConfigMapper(configs MapperSettings) (*Mapper, error) {
var mapper Mapper
for _, c := range configs {
condition, err := conditions.NewCondition(c.ConditionConfig)
if err != nil {
return nil, err
condMap := &ConditionMap{Configs: c.Configs}

if c.ConditionConfig == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could be doing this nil check here:

if mapping.Configs != nil && !mapping.Condition.Check(Event(event)) {
, then you don't need the overhead of adding a new bool condition to the conditions package

condMap.Condition = conditions.Bool(true)
} else {
var err error
condMap.Condition, err = conditions.NewCondition(c.ConditionConfig)
if err != nil {
return nil, err
}
}
mapper = append(mapper, &ConditionMap{
Condition: condition,
Configs: c.Configs,
})

mapper = append(mapper, condMap)
}

return &mapper, nil
Expand Down
10 changes: 10 additions & 0 deletions libbeat/autodiscover/template/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func TestConfigsMapping(t *testing.T) {
},
expected: []*common.Config{config},
},
// No condition
{
mapping: `
- config:
- correct: config`,
event: bus.Event{
"foo": 3,
},
expected: []*common.Config{config},
},
}

for _, test := range tests {
Expand Down
33 changes: 33 additions & 0 deletions libbeat/conditions/bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 conditions

import "fmt"

// Bool always returns true.
type Bool bool

// Check will always return true for a Bool condition.
func (c Bool) Check(event ValuesMap) bool {
return bool(c)
}

// String
func (c Bool) String() string {
return fmt.Sprintf("bool(%t)", c)
}
44 changes: 44 additions & 0 deletions libbeat/conditions/bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 conditions

import (
"testing"

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

func TestBoolTrue(t *testing.T) {
tval := true
config := Config{Bool: &tval}

c, err := NewCondition(&config)
require.NoError(t, err)

require.True(t, c.Check(nil))
}

func TestBoolFalse(t *testing.T) {
fval := false
config := Config{Bool: &fval}

c, err := NewCondition(&config)
require.NoError(t, err)

require.False(t, c.Check(nil))
}
3 changes: 3 additions & 0 deletions libbeat/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Config struct {
OR []Config `config:"or"`
AND []Config `config:"and"`
NOT *Config `config:"not"`
Bool *bool `config:"bool"`
}

// Condition is the interface for all defined conditions
Expand Down Expand Up @@ -85,6 +86,8 @@ func NewCondition(config *Config) (Condition, error) {
if err == nil {
condition, err = NewNotCondition(inner)
}
case config.Bool != nil:
condition = Bool(*config.Bool)
default:
err = errors.New("missing condition")
}
Expand Down
21 changes: 21 additions & 0 deletions libbeat/docs/processors-using.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ The supported conditions are:
* <<condition-or, `or`>>
* <<condition-and, `and`>>
* <<condition-not, `not`>>
* <<condition-bool, `bool`>>


[float]
Expand Down Expand Up @@ -400,6 +401,26 @@ not:
status: OK
------


[float]
[[condition-bool]]
===== `bool`

The `bool` operator returns a constant boolean value. Use it for debugging purposes where
it is more convenient to always have a given value than simply remove the config.

[source,yaml]
-------
bool: <true|false>
-------

For example, to configure the condition to always be true:

[source,yaml]
------
bool: true
------

[[add-cloud-metadata]]
=== Add cloud metadata

Expand Down