Skip to content

Commit

Permalink
Remove memory_limiter ballast_size_mib key from config if it exists.
Browse files Browse the repository at this point in the history
Also remove adding ballast extension from the PR.
  • Loading branch information
pmcollins committed Sep 21, 2021
1 parent 03e53ae commit c73c660
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 209 deletions.
13 changes: 6 additions & 7 deletions cmd/otelcol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,28 @@ func main() {
Version: version.Version,
}

ballastSizeMib, _ := strconv.Atoi(os.Getenv(ballastEnvVarName))
serviceParams := service.CollectorSettings{
BuildInfo: info,
Factories: factories,
ParserProvider: ballastParserProvider(info, ballastSizeMib),
ParserProvider: memLimitRemoverParserProvider(info),
}

if err := run(serviceParams); err != nil {
log.Fatal(err)
}
}

func ballastParserProvider(info component.BuildInfo, ballastSizeMib int) parserprovider.ParserProvider {
// memLimitRemoverParserProvider removes a ballast_size_mib key from a memory
// limiter processor, if it exists. Support for this key will be (or has been)
// removed and will cause the Collector to not start.
func memLimitRemoverParserProvider(info component.BuildInfo) parserprovider.ParserProvider {
cfgSourcePP := configprovider.NewConfigSourceParserProvider(
newBaseParserProvider(),
zap.NewNop(), // The service logger is not available yet, setting it to NoP.
info,
configsources.Get()...,
)
if ballastSizeMib == 0 {
return cfgSourcePP
}
return configprovider.BallastParserProvider(cfgSourcePP, ballastSizeMib)
return configprovider.NewMemLimitRemoverParserProvider(cfgSourcePP)
}

// Check whether a string exists in an array of CLI arguments
Expand Down
93 changes: 0 additions & 93 deletions internal/configprovider/ballast_parser_provider.go

This file was deleted.

69 changes: 0 additions & 69 deletions internal/configprovider/ballast_parser_provider_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.

package configprovider

import (
"fmt"
"log"
"regexp"

"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/service/parserprovider"
)

var ballastKeyRegexp *regexp.Regexp

func init() {
const expr = "processors::memory_limiter(/\\w+)?::ballast_size_mib"
ballastKeyRegexp, _ = regexp.Compile(expr)
}

// memLimitBallastRemoverParserProvider implements ParserProvider, wraps a
// ParserProvider, and removes any ballast_size_mib key from the memory_limiter
// processor, if one exists, from the wrapped config. This is to ensure that the
// Collector will still start when support for this key gets removed.
type memLimitBallastRemoverParserProvider struct {
pp parserprovider.ParserProvider
}

var _ parserprovider.ParserProvider = (*memLimitBallastRemoverParserProvider)(nil)

func NewMemLimitRemoverParserProvider(pp parserprovider.ParserProvider) parserprovider.ParserProvider {
return &memLimitBallastRemoverParserProvider{pp: pp}
}

func (mpp memLimitBallastRemoverParserProvider) Get() (*configparser.ConfigMap, error) {
cfgMap, err := mpp.pp.Get()
if err != nil {
return nil, fmt.Errorf("memLimitBallastRemoverParserProvider.Get(): %w", err)
}
out := configparser.NewParser()
for _, k := range cfgMap.AllKeys() {
if isMemLimitBallastKey(k) {
log.Println("Deprecated memory_limiter processor `ballast_size_mib` key found. Removing from config.")
} else {
out.Set(k, cfgMap.Get(k))
}
}
return out, nil
}

func isMemLimitBallastKey(k string) bool {
return ballastKeyRegexp.MatchString(k)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.

package configprovider

import (
"testing"

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

func TestIsMemLimitBallastKey(t *testing.T) {
assert.False(t, isMemLimitBallastKey("processors::ballast_size_mib"))
assert.True(t, isMemLimitBallastKey("processors::memory_limiter::ballast_size_mib"))
assert.True(t, isMemLimitBallastKey("processors::memory_limiter/foo::ballast_size_mib"))
}

func TestMemLimitBallastRemoverPP(t *testing.T) {
tests := []struct {
name string
fname string
key string
}{
{
name: "default",
fname: "testdata/ballast_mem_limiter.yaml",
key: "processors::memory_limiter::ballast_size_mib",
},
{
name: "custom",
fname: "testdata/ballast_mem_limiter_custom.yaml",
key: "processors::memory_limiter/foo::ballast_size_mib",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pp := &memLimitBallastRemoverParserProvider{
&fileParserProvider{FileName: test.fname},
}
cfgMap, err := pp.Get()
require.NoError(t, err)
b := cfgMap.IsSet(test.key)
assert.False(t, b)
})
}
}
22 changes: 0 additions & 22 deletions internal/configprovider/testdata/ballast_custom.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ receivers:
collection_interval: 1s
scrapers:
cpu:
processors:
memory_limiter:
check_interval: 1s
limit_mib: 4000
spike_limit_mib: 800
ballast_size_mib: 64
exporters:
logging:
loglevel: info
Expand All @@ -16,6 +22,8 @@ service:
metrics:
receivers:
- hostmetrics
processors:
- memory_limiter
exporters:
- logging
extensions:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
extensions:
memory_ballast:
size_in_percentage: 20
size_mib: 64
receivers:
hostmetrics:
collection_interval: 1s
scrapers:
cpu:
processors:
memory_limiter/foo:
check_interval: 1s
limit_mib: 4000
spike_limit_mib: 800
ballast_size_mib: 64
exporters:
logging:
loglevel: info
Expand All @@ -16,6 +22,8 @@ service:
metrics:
receivers:
- hostmetrics
processors:
- memory_limiter/foo
exporters:
- logging
extensions:
Expand Down
Loading

0 comments on commit c73c660

Please sign in to comment.