Skip to content

Commit 7031342

Browse files
committed
Make include_matches backwards compatible with 7.x config
To make transitioning from 7.x/8.0 to 8.1 easier allow the old format to continue working by translating it internally to the new format. This logs a deprecation warning when the old config format is detected. This will make it easier to Agent users to migrate from 7.x/8.0 to 8.1. There will be time window when integrations with the old format are running with the new Agent (or vice versa).
1 parent c2f2486 commit 7031342

3 files changed

Lines changed: 105 additions & 3 deletions

File tree

filebeat/input/journald/config.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,30 @@ package journald
2222

2323
import (
2424
"errors"
25+
"sync"
2526
"time"
2627

28+
"github.com/elastic/go-ucfg"
29+
2730
"github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalfield"
2831
"github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalread"
32+
"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
2933
"github.com/elastic/beats/v7/libbeat/reader/parser"
3034
)
3135

36+
var (
37+
// includeMatchesWarnOnce allow for a config deprecation warning to be
38+
// logged only once if an old config format is detected.
39+
includeMatchesWarnOnce sync.Once
40+
)
41+
3242
// Config stores the options of a journald input.
3343
type config struct {
3444
// Paths stores the paths to the journal files to be read.
3545
Paths []string `config:"paths"`
3646

3747
// Backoff is the current interval to wait before
38-
// attemting to read again from the journal.
48+
// attempting to read again from the journal.
3949
Backoff time.Duration `config:"backoff" validate:"min=0,nonzero"`
4050

4151
// MaxBackoff is the limit of the backoff time.
@@ -48,7 +58,7 @@ type config struct {
4858
CursorSeekFallback journalread.SeekMode `config:"cursor_seek_fallback"`
4959

5060
// Matches store the key value pairs to match entries.
51-
Matches journalfield.IncludeMatches `config:"include_matches"`
61+
Matches bwcIncludeMatches `config:"include_matches"`
5262

5363
// Units stores the units to monitor.
5464
Units []string `config:"units"`
@@ -66,6 +76,33 @@ type config struct {
6676
Parsers parser.Config `config:",inline"`
6777
}
6878

79+
// bwcIncludeMatches is a wrapper that accepts include_matches configuration
80+
// from 7.x to allow old config to remain compatible.
81+
type bwcIncludeMatches journalfield.IncludeMatches
82+
83+
func (im *bwcIncludeMatches) Unpack(c *ucfg.Config) error {
84+
// Handle 7.x config format in a backwards compatible manner. Old format:
85+
// include_matches: [_SYSTEMD_UNIT=foo.service, _SYSTEMD_UNIT=bar.service]
86+
if c.IsArray() {
87+
var matches []journalfield.Matcher
88+
if err := c.Unpack(&matches); err != nil {
89+
return err
90+
}
91+
for _, x := range matches {
92+
im.OR = append(im.OR, journalfield.IncludeMatches{
93+
Matches: []journalfield.Matcher{x},
94+
})
95+
}
96+
includeMatchesWarnOnce.Do(func() {
97+
cfgwarn.Deprecate("", "Please migrate your journald input's "+
98+
"include_matches config to the new more expressive format.")
99+
})
100+
return nil
101+
}
102+
103+
return c.Unpack((*journalfield.IncludeMatches)(im))
104+
}
105+
69106
var errInvalidSeekFallback = errors.New("invalid setting for cursor_seek_fallback")
70107

71108
func defaultConfig() config {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build linux && cgo && withjournald
19+
// +build linux,cgo,withjournald
20+
21+
package journald
22+
23+
import (
24+
"testing"
25+
26+
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
28+
29+
"github.com/elastic/beats/v7/libbeat/common"
30+
)
31+
32+
func TestConfigIncludeMatches(t *testing.T) {
33+
verify := func(t *testing.T, yml string) {
34+
t.Helper()
35+
36+
c, err := common.NewConfigWithYAML([]byte(yml), "source")
37+
require.NoError(t, err)
38+
39+
conf := defaultConfig()
40+
require.NoError(t, c.Unpack(&conf))
41+
42+
assert.EqualValues(t, "_SYSTEMD_UNIT=foo.service", conf.Matches.OR[0].Matches[0].String())
43+
assert.EqualValues(t, "_SYSTEMD_UNIT=bar.service", conf.Matches.OR[1].Matches[0].String())
44+
}
45+
46+
t.Run("normal", func(t *testing.T) {
47+
const yaml = `
48+
include_matches:
49+
or:
50+
- match: _SYSTEMD_UNIT=foo.service
51+
- match: _SYSTEMD_UNIT=bar.service
52+
`
53+
verify(t, yaml)
54+
})
55+
56+
t.Run("backwards-compatible", func(t *testing.T) {
57+
const yaml = `
58+
include_matches:
59+
- _SYSTEMD_UNIT=foo.service
60+
- _SYSTEMD_UNIT=bar.service
61+
`
62+
63+
verify(t, yaml)
64+
})
65+
}

filebeat/input/journald/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func configure(cfg *common.Config) ([]cursor.Source, cursor.Input, error) {
107107
MaxBackoff: config.MaxBackoff,
108108
Seek: config.Seek,
109109
CursorSeekFallback: config.CursorSeekFallback,
110-
Matches: config.Matches,
110+
Matches: journalfield.IncludeMatches(config.Matches),
111111
Units: config.Units,
112112
Transports: config.Transports,
113113
Identifiers: config.Identifiers,

0 commit comments

Comments
 (0)