Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: Parse model from YAML #4826

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ package config // import "go.opentelemetry.io/contrib/config"
import (
"context"
"errors"
"os"
"regexp"

"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -113,8 +116,34 @@ func WithOpenTelemetryConfiguration(cfg OpenTelemetryConfiguration) Configuratio
})
}

// ParseYAML parses a YAML configuration file into an OpenTelemetryConfiguration.
func ParseYAML(file []byte) (*OpenTelemetryConfiguration, error) {
re := regexp.MustCompile(`\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}`)

replaceEnvVars := func(input []byte) []byte {
return re.ReplaceAllFunc(input, func(s []byte) []byte {
match := re.FindSubmatch(s)
if len(match) < 2 {
return s
}
envVarName := string(match[1])
envVarValue := os.Getenv(envVarName)
return []byte(envVarValue)
})
}

file = replaceEnvVars(file)
Copy link
Member

@pellared pellared Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to call out one important thing from https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/file-configuration.md#environment-variable-substitution:

It MUST NOT be possible to inject YAML structures by environment variables. For example, references to INVALID_MAP_VALUE environment variable below.

Maybe you will find some code that will help in https://github.com/open-telemetry/opentelemetry-collector.

@codeboten, are you able to provide some guidance?

Copy link
Contributor Author

@carrbs carrbs Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree @pellared, also this does not solve for blocking the key replacement (i.e.${SNEAKY}: "foo") in the spec. I will take a look at both of these cases to see if we can come up with something more useful.


var cfg OpenTelemetryConfiguration
err := yaml.Unmarshal(file, &cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}

// TODO: implement parsing functionality:
// - https://github.com/open-telemetry/opentelemetry-go-contrib/issues/4373
// - https://github.com/open-telemetry/opentelemetry-go-contrib/issues/4412

// TODO: create SDK from the model:
Expand Down
30 changes: 30 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package config

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -55,3 +56,32 @@ func TestNewSDK(t *testing.T) {
func ptr[T any](v T) *T {
return &v
}

func TestParseYAML(t *testing.T) {
tests := []struct {
name string
input string
wantErr error
wantType interface{}
}{
{
name: "valid YAML",
input: "file_format: yaml\ndisabled: false\n",
wantErr: nil,
wantType: &OpenTelemetryConfiguration{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := ([]byte)(tt.input)
got, err := ParseYAML(r)
if err != nil {
fmt.Println(err)
require.Equal(t, tt.wantErr.Error(), err.Error())
} else {
assert.IsType(t, tt.wantType, got)
}
})
}
}
Loading