-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Incorporate obsoleted overwritepropertiesconverter and strip --set ar…
…gs for otelcol service open-telemetry/opentelemetry-collector#6656 removed the overwrite properties config converter that this distribution uses. These changes migrate its functionality to our library and strip --set options to prevent further utilization by the otelcol service. They also add an integration tests to vet its usage.
- Loading branch information
1 parent
fde0a08
commit c313985
Showing
7 changed files
with
347 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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. | ||
|
||
// Taken from https://github.com/open-telemetry/opentelemetry-collector/blob/v0.66.0/confmap/converter/overwritepropertiesconverter/properties.go | ||
// to prevent breaking changes. | ||
// "Deprecated: [v0.63.0] this converter will not be supported anymore because of dot separation limitation. | ||
// See https://github.com/open-telemetry/opentelemetry-collector/issues/6294 for more details." | ||
package configconverter | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"strings" | ||
|
||
"github.com/knadh/koanf/maps" | ||
"github.com/magiconair/properties" | ||
|
||
"go.opentelemetry.io/collector/confmap" | ||
) | ||
|
||
type converter struct { | ||
properties []string | ||
} | ||
|
||
func NewOverwritePropertiesConverter(properties []string) confmap.Converter { | ||
return &converter{properties: properties} | ||
} | ||
|
||
func (c *converter) Convert(_ context.Context, conf *confmap.Conf) error { | ||
if len(c.properties) == 0 { | ||
return nil | ||
} | ||
|
||
b := &bytes.Buffer{} | ||
for _, property := range c.properties { | ||
property = strings.TrimSpace(property) | ||
b.WriteString(property) | ||
b.WriteString("\n") | ||
} | ||
|
||
var props *properties.Properties | ||
var err error | ||
if props, err = properties.Load(b.Bytes(), properties.UTF8); err != nil { | ||
return err | ||
} | ||
|
||
// Create a map manually instead of using properties.Map() to not expand the env vars. | ||
parsed := make(map[string]interface{}, props.Len()) | ||
for _, key := range props.Keys() { | ||
value, _ := props.Get(key) | ||
parsed[key] = value | ||
} | ||
prop := maps.Unflatten(parsed, ".") | ||
return conf.Merge(confmap.NewFromStringMap(prop)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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. | ||
|
||
// Taken from https://github.com/open-telemetry/opentelemetry-collector/blob/v0.66.0/confmap/converter/overwritepropertiesconverter/properties_test.go | ||
// to brevent breaking changes. | ||
package configconverter | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/confmap" | ||
) | ||
|
||
func TestOverwritePropertiesConverter_Empty(t *testing.T) { | ||
pmp := NewOverwritePropertiesConverter(nil) | ||
conf := confmap.NewFromStringMap(map[string]interface{}{"foo": "bar"}) | ||
assert.NoError(t, pmp.Convert(context.Background(), conf)) | ||
assert.Equal(t, map[string]interface{}{"foo": "bar"}, conf.ToStringMap()) | ||
} | ||
|
||
func TestOverwritePropertiesConverter(t *testing.T) { | ||
props := []string{ | ||
"processors.batch.timeout=2s", | ||
"processors.batch/foo.timeout=3s", | ||
"receivers.otlp.protocols.grpc.endpoint=localhost:1818", | ||
"exporters.kafka.brokers=foo:9200,foo2:9200", | ||
} | ||
|
||
pmp := NewOverwritePropertiesConverter(props) | ||
conf := confmap.New() | ||
require.NoError(t, pmp.Convert(context.Background(), conf)) | ||
keys := conf.AllKeys() | ||
assert.Len(t, keys, 4) | ||
assert.Equal(t, "2s", conf.Get("processors::batch::timeout")) | ||
assert.Equal(t, "3s", conf.Get("processors::batch/foo::timeout")) | ||
assert.Equal(t, "foo:9200,foo2:9200", conf.Get("exporters::kafka::brokers")) | ||
assert.Equal(t, "localhost:1818", conf.Get("receivers::otlp::protocols::grpc::endpoint")) | ||
} | ||
|
||
func TestOverwritePropertiesConverter_InvalidProperty(t *testing.T) { | ||
pmp := NewOverwritePropertiesConverter([]string{"=2s"}) | ||
conf := confmap.New() | ||
assert.Error(t, pmp.Convert(context.Background(), conf)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.