Skip to content

Commit

Permalink
Change config to use parsed/unmarshaled values
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Sep 14, 2021
1 parent 2d75cd8 commit 4bf1fde
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
14 changes: 4 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type Service struct {

// ServiceTelemetry defines the configurable settings for service telemetry.
type ServiceTelemetry struct {
Logs ServiceTelemetryLogs `mapstructure:"logs"`
Logs ServiceTelemetryLogs
}

func (srvT *ServiceTelemetry) validate() error {
Expand All @@ -173,24 +173,18 @@ func (srvT *ServiceTelemetry) validate() error {
// the collector uses mapstructure and not yaml tags.
type ServiceTelemetryLogs struct {
// Level is the minimum enabled logging level.
// Valid values are "DEBUG", "INFO", "WARN", "ERROR", "DPANIC", "PANIC", "FATAL".
Level string `mapstructure:"level"`
Level zapcore.Level

// Development puts the logger in development mode, which changes the
// behavior of DPanicLevel and takes stacktraces more liberally.
Development bool `mapstructure:"development"`
Development bool

// Encoding sets the logger's encoding.
// Valid values are "json" and "console".
Encoding string `mapstructure:"encoding"`
Encoding string
}

func (srvTL *ServiceTelemetryLogs) validate() error {
var lvl zapcore.Level
if err := lvl.UnmarshalText([]byte(srvTL.Level)); err != nil {
return fmt.Errorf(`service telemetry logs invalid level: %q, valid values are "DEBUG", "INFO", "WARN", "ERROR", "DPANIC", "PANIC", "FATAL"`, srvTL.Level)
}

if srvTL.Encoding != "json" && srvTL.Encoding != "console" {
return fmt.Errorf(`service telemetry logs invalid encoding: %q, valid values are "json" and "console"`, srvTL.Encoding)
}
Expand Down
12 changes: 2 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
)

var errInvalidRecvConfig = errors.New("invalid receiver config")
Expand Down Expand Up @@ -212,15 +213,6 @@ func TestConfigValidate(t *testing.T) {
},
expected: fmt.Errorf(`extension "nop" has invalid configuration: %w`, errInvalidExtConfig),
},
{
name: "invalid-service-telemetry-level",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Service.Telemetry.Logs.Level = "unknown"
return cfg
},
expected: errors.New(`service telemetry logs invalid level: "unknown", valid values are "DEBUG", "INFO", "WARN", "ERROR", "DPANIC", "PANIC", "FATAL"`),
},
{
name: "invalid-service-telemetry-encoding",
cfgFn: func() *Config {
Expand Down Expand Up @@ -263,7 +255,7 @@ func generateConfig() *Config {
},
},
Service: Service{
Telemetry: ServiceTelemetry{Logs: ServiceTelemetryLogs{Level: "DEBUG", Development: true, Encoding: "console"}},
Telemetry: ServiceTelemetry{Logs: ServiceTelemetryLogs{Level: zapcore.DebugLevel, Development: true, Encoding: "console"}},
Extensions: []ComponentID{NewID("nop")},
Pipelines: map[string]*Pipeline{
"traces": {
Expand Down
34 changes: 30 additions & 4 deletions config/configunmarshaler/defaultunmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
"reflect"

"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
Expand All @@ -33,6 +35,7 @@ const (
_ configErrorCode = iota

errInvalidTypeAndNameKey
errInvalidLogsLevel
errUnknownType
errDuplicateName
errUnmarshalTopLevelStructureError
Expand Down Expand Up @@ -77,11 +80,21 @@ type configSettings struct {
}

type serviceSettings struct {
Telemetry config.ServiceTelemetry `mapstructure:"telemetry"`
Telemetry serviceTelemetrySettings `mapstructure:"telemetry"`
Extensions []string `mapstructure:"extensions"`
Pipelines map[string]pipelineSettings `mapstructure:"pipelines"`
}

type serviceTelemetrySettings struct {
Logs serviceTelemetryLogsSettings `mapstructure:"logs"`
}

type serviceTelemetryLogsSettings struct {
Level string `mapstructure:"level"`
Development bool `mapstructure:"development"`
Encoding string `mapstructure:"encoding"`
}

type pipelineSettings struct {
Receivers []string `mapstructure:"receivers"`
Processors []string `mapstructure:"processors"`
Expand All @@ -108,8 +121,8 @@ func (*defaultUnmarshaler) Unmarshal(v *configparser.ConfigMap, factories compon
// Setup default telemetry values as in service/logger.go.
// TODO: Add a component.ServiceFactory to allow this to be defined by the Service.
Service: serviceSettings{
Telemetry: config.ServiceTelemetry{
Logs: config.ServiceTelemetryLogs{
Telemetry: serviceTelemetrySettings{
Logs: serviceTelemetryLogsSettings{
Level: "INFO",
Development: false,
Encoding: "console",
Expand Down Expand Up @@ -234,7 +247,20 @@ func unmarshalExtensions(exts map[string]map[string]interface{}, factories map[c

func unmarshalService(rawService serviceSettings) (config.Service, error) {
var ret config.Service
ret.Telemetry = rawService.Telemetry

var lvl zapcore.Level
if err := lvl.UnmarshalText([]byte(rawService.Telemetry.Logs.Level)); err != nil {
return ret, &configError{
msg: fmt.Sprintf(`service telemetry logs invalid level: %q, valid values are "DEBUG", "INFO", "WARN", "ERROR", "DPANIC", "PANIC", "FATAL"`, rawService.Telemetry.Logs.Level),
code: errInvalidLogsLevel,
}
}

ret.Telemetry.Logs = config.ServiceTelemetryLogs{
Level: lvl,
Development: rawService.Telemetry.Logs.Development,
Encoding: rawService.Telemetry.Logs.Encoding,
}

ret.Extensions = make([]config.ComponentID, 0, len(rawService.Extensions))
for _, extIDStr := range rawService.Extensions {
Expand Down
5 changes: 4 additions & 1 deletion config/configunmarshaler/defaultunmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
Expand Down Expand Up @@ -97,7 +98,7 @@ func TestDecodeConfig(t *testing.T) {
"Did not load processor config correctly")

// Verify Service Telemetry
assert.Equal(t, config.ServiceTelemetry{Logs: config.ServiceTelemetryLogs{Level: "DEBUG", Development: true, Encoding: "console"}}, cfg.Service.Telemetry)
assert.Equal(t, config.ServiceTelemetry{Logs: config.ServiceTelemetryLogs{Level: zapcore.DebugLevel, Development: true, Encoding: "console"}}, cfg.Service.Telemetry)

// Verify Service Extensions
assert.Equal(t, 2, len(cfg.Service.Extensions))
Expand Down Expand Up @@ -417,6 +418,8 @@ func TestDecodeConfig_Invalid(t *testing.T) {
{name: "invalid-processor-sub-config", expected: errUnmarshalTopLevelStructureError},
{name: "invalid-receiver-sub-config", expected: errUnmarshalTopLevelStructureError},
{name: "invalid-pipeline-sub-config", expected: errUnmarshalTopLevelStructureError},

{name: "invalid-logs-level", expected: errInvalidLogsLevel},
}

factories, err := testcomponents.ExampleComponents()
Expand Down
19 changes: 19 additions & 0 deletions config/configunmarshaler/testdata/invalid-logs-level.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
receivers:
examplereceiver:
processors:
exampleprocessor:
exporters:
exampleexporter:
extensions:
exampleextension:
service:
telemetry:
logs:
level: "UNKNOWN"
extensions: [exampleextension]
pipelines:
traces:
receivers: [examplereceiver]
processors: [exampleprocessor]
exporters: [exampleexporter]

0 comments on commit 4bf1fde

Please sign in to comment.