Skip to content

Commit

Permalink
Add env config map provider, read from environment variable (#4574)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Dec 17, 2021
1 parent 4d2fc94 commit f80527e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
56 changes: 56 additions & 0 deletions config/configmapprovider/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 configmapprovider // import "go.opentelemetry.io/collector/config/configmapprovider"

import (
"context"
"errors"
"fmt"
"os"

"gopkg.in/yaml.v2"

"go.opentelemetry.io/collector/config"
)

type envMapProvider struct {
envName string
}

// NewEnv returns a new Provider that reads the configuration from the given environment variable.
func NewEnv(envName string) Provider {
return &envMapProvider{
envName: envName,
}
}

func (emp *envMapProvider) Retrieve(_ context.Context, _ func(*ChangeEvent)) (Retrieved, error) {
if emp.envName == "" {
return nil, errors.New("config environment not specified")
}

content := os.Getenv(emp.envName)

var data map[string]interface{}
if err := yaml.Unmarshal([]byte(content), &data); err != nil {
return nil, fmt.Errorf("unable to parse yaml: %w", err)
}

return &simpleRetrieved{confMap: config.NewMapFromStringMap(data)}, nil
}

func (*envMapProvider) Shutdown(context.Context) error {
return nil
}
68 changes: 68 additions & 0 deletions config/configmapprovider/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 configmapprovider

import (
"context"
"os"
"path"
"testing"

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

"go.opentelemetry.io/collector/config"
)

func TestEnv_EmptyName(t *testing.T) {
exp := NewEnv("")
_, err := exp.Retrieve(context.Background(), nil)
require.Error(t, err)
}

func TestEnv_InvalidYaml(t *testing.T) {
bytes, err := os.ReadFile(path.Join("testdata", "invalid-yaml.yaml"))
require.NoError(t, err)
const envName = "invalid-yaml"
require.NoError(t, os.Setenv(envName, string(bytes)))
t.Cleanup(func() {
require.NoError(t, os.Unsetenv(envName))
})
env := NewEnv(envName)
_, err = env.Retrieve(context.Background(), nil)
assert.Error(t, err)
}

func TestEnv(t *testing.T) {
bytes, err := os.ReadFile(path.Join("testdata", "default-config.yaml"))
require.NoError(t, err)
const envName = "default-config"
require.NoError(t, os.Setenv(envName, string(bytes)))
t.Cleanup(func() {
require.NoError(t, os.Unsetenv(envName))
})
env := NewEnv(envName)
ret, err := env.Retrieve(context.Background(), nil)
assert.NoError(t, err)
cfg, err := ret.Get(context.Background())
assert.NoError(t, err)
expectedMap := config.NewMapFromStringMap(map[string]interface{}{
"processors::batch": nil,
"exporters::otlp::endpoint": "localhost:4317",
})
assert.Equal(t, expectedMap, cfg)
assert.NoError(t, ret.Close(context.Background()))
assert.NoError(t, env.Shutdown(context.Background()))
}
1 change: 1 addition & 0 deletions config/configmapprovider/testdata/invalid-yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processors

0 comments on commit f80527e

Please sign in to comment.