Skip to content

Commit

Permalink
Move cfgwarn to logp/cfgwarn (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch authored Mar 2, 2022
1 parent 4316712 commit ddc9c44
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Provided packages:
* `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.
* `github.com/elastic/elastic-agent-libs/file` is responsible for rotating and writing input and output files.
* `github.com/elastic/elastic-agent-libs/logp` is the well known logger from libbeat.
* `github.com/elastic/elastic-agent-libs/logp/cfgwarn` provides logging utilities for warning users about deprecated settings.
* `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{}`.
* `github.com/elastic/elastic-agent-libs/safemapstr` contains safe operations for `mapstr.M`.
* `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.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/elastic/go-ucfg v0.8.4
github.com/hashicorp/go-multierror v1.1.1
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
github.com/magefile/mage v1.12.1
github.com/spf13/cobra v1.3.0
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4=
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
49 changes: 49 additions & 0 deletions logp/cfgwarn/cfgwarn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 cfgwarn

import (
"fmt"

"go.uber.org/zap"

"github.com/elastic/elastic-agent-libs/logp"
)

const selector = "cfgwarn"

// Beta logs the usage of an beta feature.
func Beta(format string, v ...interface{}) {
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("BETA: "+format, v...)
}

// Deprecate logs a deprecation message.
// The version string contains the version when the future will be removed.
// If version is empty, the message will not mention the removal of the feature.
func Deprecate(version string, format string, v ...interface{}) {
var postfix string
if version != "" {
postfix = fmt.Sprintf(" Will be removed in version: %s", version)
}
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("DEPRECATED: "+format+postfix, v...)
}

// Experimental logs the usage of an experimental feature.
func Experimental(format string, v ...interface{}) {
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("EXPERIMENTAL: "+format, v...)
}
77 changes: 77 additions & 0 deletions logp/cfgwarn/removed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 cfgwarn

import (
"fmt"
"strings"

"github.com/joeshaw/multierror"

"github.com/elastic/elastic-agent-libs/config"
)

func checkRemovedSettings(cfg *config.C, settings ...string) error {
var errs multierror.Errors
for _, setting := range settings {
if err := checkRemovedSetting(cfg, setting); err != nil {
errs = append(errs, err)
}
}

return errs.Err()
}

func checkRemovedSetting(cfg *config.C, setting string) error {
segments := strings.Split(setting, ".")

L := len(segments)
name := segments[L-1]
path := segments[:L-1]

current := cfg

// we are looking for any key that match the name.
for _, p := range path {
current, _ = current.Child(p, -1)
if current == nil {
break
}
}

// full path to setting not available -> setting not found
if current == nil {
return nil
}

if !current.HasField(name) {
return nil
}

return fmt.Errorf("setting '%v' has been removed", current.PathOf(name))
}

// CheckRemoved6xSettings prints a warning if the obsolete setting is used.
func CheckRemoved6xSettings(cfg *config.C, settings ...string) error {
return checkRemovedSettings(cfg, settings...)
}

// CheckRemoved6xSetting prints a warning if the obsolete setting is used.
func CheckRemoved6xSetting(cfg *config.C, setting string) error {
return checkRemovedSetting(cfg, setting)
}
142 changes: 142 additions & 0 deletions logp/cfgwarn/removed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// 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 cfgwarn

import (
"errors"
"testing"

"github.com/joeshaw/multierror"
"github.com/stretchr/testify/assert"

"github.com/elastic/elastic-agent-libs/config"
)

func TestRemovedSetting(t *testing.T) {
tests := []struct {
name string
cfg *config.C
lookup string
expected error
}{
{
name: "no obsolete setting",
lookup: "notfound",
cfg: config.MustNewConfigFrom(map[string]interface{}{
"hello.world": "ok",
}),
expected: nil,
},
{
name: "obsolete setting found",
lookup: "hello",
cfg: config.MustNewConfigFrom(map[string]interface{}{
"hello.world": "ok",
}),
expected: errors.New("setting 'hello' has been removed"),
},
}

functions := []struct {
name string
fn func(*config.C, string) error
}{
{name: "checkRemovedSetting", fn: checkRemovedSetting},
{name: "checkRemoved6xSetting", fn: CheckRemoved6xSetting},
}

for _, function := range functions {
t.Run(function.name, func(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := function.fn(test.cfg, test.lookup)
assert.Equal(t, test.expected, err)
})
}
})
}
}

func TestRemovedSettings(t *testing.T) {
tests := []struct {
name string
cfg *config.C
lookup []string
expected error
}{
{
name: "no obsolete setting",
lookup: []string{"notfound"},
cfg: config.MustNewConfigFrom(map[string]interface{}{
"hello.world": "ok",
}),
expected: nil,
},
{
name: "obsolete setting found",
lookup: []string{"hello"},
cfg: config.MustNewConfigFrom(map[string]interface{}{
"hello.world": "ok",
}),
expected: multierror.Errors{errors.New("setting 'hello' has been removed")}.Err(),
},
{
name: "multiple obsolete settings",
lookup: []string{"hello", "bad"},
cfg: config.MustNewConfigFrom(map[string]interface{}{
"hello.world": "ok",
"bad": "true",
}),
expected: multierror.Errors{
errors.New("setting 'hello' has been removed"),
errors.New("setting 'bad' has been removed"),
}.Err(),
},
{
name: "multiple obsolete settings not on first level",
lookup: []string{"filebeat.config.prospectors", "filebeat.prospectors"},
cfg: config.MustNewConfigFrom(map[string]interface{}{
"filebeat.prospectors": "ok",
"filebeat.config.prospectors": map[string]interface{}{"ok": "ok1"},
}),
expected: multierror.Errors{
errors.New("setting 'filebeat.config.prospectors' has been removed"),
errors.New("setting 'filebeat.prospectors' has been removed"),
}.Err(),
},
}

functions := []struct {
name string
fn func(*config.C, ...string) error
}{
{name: "checkRemovedSetting", fn: checkRemovedSettings},
{name: "checkRemoved6xSetting", fn: CheckRemoved6xSettings},
}

for _, function := range functions {
t.Run(function.name, func(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := checkRemovedSettings(test.cfg, test.lookup...)
assert.Equal(t, test.expected, err)
})
}
})
}
}

0 comments on commit ddc9c44

Please sign in to comment.