Skip to content

Commit ddc9c44

Browse files
authored
Move cfgwarn to logp/cfgwarn (#18)
1 parent 4316712 commit ddc9c44

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Provided packages:
88
* `github.com/elastic/elastic-agent-libs/config` the previous `config.go` file from `github.com/elastic/beats/v7/libbeat/common`. A minimal wrapper around `github.com/elastic/go-ucfg`. It contains helpers for merging and accessing configuration objects and flags.
99
* `github.com/elastic/elastic-agent-libs/file` is responsible for rotating and writing input and output files.
1010
* `github.com/elastic/elastic-agent-libs/logp` is the well known logger from libbeat.
11+
* `github.com/elastic/elastic-agent-libs/logp/cfgwarn` provides logging utilities for warning users about deprecated settings.
1112
* `github.com/elastic/elastic-agent-libs/mapstr` is the old `github.com/elastic/beats/v7/libbeat/common.MapStr`. It is an extra layer on top of `map[string]interface{}`.
1213
* `github.com/elastic/elastic-agent-libs/safemapstr` contains safe operations for `mapstr.M`.
1314
* `github.com/elastic/elastic-agent-libs/str` the previous `stringset.go` file from `github.com/elastic/beats/v7/libbeat/common`. It provides a string set implementation.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.17
55
require (
66
github.com/elastic/go-ucfg v0.8.4
77
github.com/hashicorp/go-multierror v1.1.1
8+
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
89
github.com/magefile/mage v1.12.1
910
github.com/spf13/cobra v1.3.0
1011
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
232232
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
233233
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
234234
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
235+
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4=
236+
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
235237
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
236238
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
237239
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=

logp/cfgwarn/cfgwarn.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
package cfgwarn
19+
20+
import (
21+
"fmt"
22+
23+
"go.uber.org/zap"
24+
25+
"github.com/elastic/elastic-agent-libs/logp"
26+
)
27+
28+
const selector = "cfgwarn"
29+
30+
// Beta logs the usage of an beta feature.
31+
func Beta(format string, v ...interface{}) {
32+
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("BETA: "+format, v...)
33+
}
34+
35+
// Deprecate logs a deprecation message.
36+
// The version string contains the version when the future will be removed.
37+
// If version is empty, the message will not mention the removal of the feature.
38+
func Deprecate(version string, format string, v ...interface{}) {
39+
var postfix string
40+
if version != "" {
41+
postfix = fmt.Sprintf(" Will be removed in version: %s", version)
42+
}
43+
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("DEPRECATED: "+format+postfix, v...)
44+
}
45+
46+
// Experimental logs the usage of an experimental feature.
47+
func Experimental(format string, v ...interface{}) {
48+
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("EXPERIMENTAL: "+format, v...)
49+
}

logp/cfgwarn/removed.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
package cfgwarn
19+
20+
import (
21+
"fmt"
22+
"strings"
23+
24+
"github.com/joeshaw/multierror"
25+
26+
"github.com/elastic/elastic-agent-libs/config"
27+
)
28+
29+
func checkRemovedSettings(cfg *config.C, settings ...string) error {
30+
var errs multierror.Errors
31+
for _, setting := range settings {
32+
if err := checkRemovedSetting(cfg, setting); err != nil {
33+
errs = append(errs, err)
34+
}
35+
}
36+
37+
return errs.Err()
38+
}
39+
40+
func checkRemovedSetting(cfg *config.C, setting string) error {
41+
segments := strings.Split(setting, ".")
42+
43+
L := len(segments)
44+
name := segments[L-1]
45+
path := segments[:L-1]
46+
47+
current := cfg
48+
49+
// we are looking for any key that match the name.
50+
for _, p := range path {
51+
current, _ = current.Child(p, -1)
52+
if current == nil {
53+
break
54+
}
55+
}
56+
57+
// full path to setting not available -> setting not found
58+
if current == nil {
59+
return nil
60+
}
61+
62+
if !current.HasField(name) {
63+
return nil
64+
}
65+
66+
return fmt.Errorf("setting '%v' has been removed", current.PathOf(name))
67+
}
68+
69+
// CheckRemoved6xSettings prints a warning if the obsolete setting is used.
70+
func CheckRemoved6xSettings(cfg *config.C, settings ...string) error {
71+
return checkRemovedSettings(cfg, settings...)
72+
}
73+
74+
// CheckRemoved6xSetting prints a warning if the obsolete setting is used.
75+
func CheckRemoved6xSetting(cfg *config.C, setting string) error {
76+
return checkRemovedSetting(cfg, setting)
77+
}

logp/cfgwarn/removed_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
package cfgwarn
19+
20+
import (
21+
"errors"
22+
"testing"
23+
24+
"github.com/joeshaw/multierror"
25+
"github.com/stretchr/testify/assert"
26+
27+
"github.com/elastic/elastic-agent-libs/config"
28+
)
29+
30+
func TestRemovedSetting(t *testing.T) {
31+
tests := []struct {
32+
name string
33+
cfg *config.C
34+
lookup string
35+
expected error
36+
}{
37+
{
38+
name: "no obsolete setting",
39+
lookup: "notfound",
40+
cfg: config.MustNewConfigFrom(map[string]interface{}{
41+
"hello.world": "ok",
42+
}),
43+
expected: nil,
44+
},
45+
{
46+
name: "obsolete setting found",
47+
lookup: "hello",
48+
cfg: config.MustNewConfigFrom(map[string]interface{}{
49+
"hello.world": "ok",
50+
}),
51+
expected: errors.New("setting 'hello' has been removed"),
52+
},
53+
}
54+
55+
functions := []struct {
56+
name string
57+
fn func(*config.C, string) error
58+
}{
59+
{name: "checkRemovedSetting", fn: checkRemovedSetting},
60+
{name: "checkRemoved6xSetting", fn: CheckRemoved6xSetting},
61+
}
62+
63+
for _, function := range functions {
64+
t.Run(function.name, func(t *testing.T) {
65+
for _, test := range tests {
66+
t.Run(test.name, func(t *testing.T) {
67+
err := function.fn(test.cfg, test.lookup)
68+
assert.Equal(t, test.expected, err)
69+
})
70+
}
71+
})
72+
}
73+
}
74+
75+
func TestRemovedSettings(t *testing.T) {
76+
tests := []struct {
77+
name string
78+
cfg *config.C
79+
lookup []string
80+
expected error
81+
}{
82+
{
83+
name: "no obsolete setting",
84+
lookup: []string{"notfound"},
85+
cfg: config.MustNewConfigFrom(map[string]interface{}{
86+
"hello.world": "ok",
87+
}),
88+
expected: nil,
89+
},
90+
{
91+
name: "obsolete setting found",
92+
lookup: []string{"hello"},
93+
cfg: config.MustNewConfigFrom(map[string]interface{}{
94+
"hello.world": "ok",
95+
}),
96+
expected: multierror.Errors{errors.New("setting 'hello' has been removed")}.Err(),
97+
},
98+
{
99+
name: "multiple obsolete settings",
100+
lookup: []string{"hello", "bad"},
101+
cfg: config.MustNewConfigFrom(map[string]interface{}{
102+
"hello.world": "ok",
103+
"bad": "true",
104+
}),
105+
expected: multierror.Errors{
106+
errors.New("setting 'hello' has been removed"),
107+
errors.New("setting 'bad' has been removed"),
108+
}.Err(),
109+
},
110+
{
111+
name: "multiple obsolete settings not on first level",
112+
lookup: []string{"filebeat.config.prospectors", "filebeat.prospectors"},
113+
cfg: config.MustNewConfigFrom(map[string]interface{}{
114+
"filebeat.prospectors": "ok",
115+
"filebeat.config.prospectors": map[string]interface{}{"ok": "ok1"},
116+
}),
117+
expected: multierror.Errors{
118+
errors.New("setting 'filebeat.config.prospectors' has been removed"),
119+
errors.New("setting 'filebeat.prospectors' has been removed"),
120+
}.Err(),
121+
},
122+
}
123+
124+
functions := []struct {
125+
name string
126+
fn func(*config.C, ...string) error
127+
}{
128+
{name: "checkRemovedSetting", fn: checkRemovedSettings},
129+
{name: "checkRemoved6xSetting", fn: CheckRemoved6xSettings},
130+
}
131+
132+
for _, function := range functions {
133+
t.Run(function.name, func(t *testing.T) {
134+
for _, test := range tests {
135+
t.Run(test.name, func(t *testing.T) {
136+
err := checkRemovedSettings(test.cfg, test.lookup...)
137+
assert.Equal(t, test.expected, err)
138+
})
139+
}
140+
})
141+
}
142+
}

0 commit comments

Comments
 (0)