Skip to content

Commit

Permalink
Add config marshaler.
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien committed Jun 21, 2022
1 parent 408f1e9 commit 9728812
Show file tree
Hide file tree
Showing 14 changed files with 787 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package config // import "go.opentelemetry.io/collector/config"
import (
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/service/configmarshaler/configencoder"
)

// Type is the component type as it is used in the config.
Expand All @@ -32,6 +33,9 @@ type Unmarshallable interface {
// Unmarshal is a function that unmarshalls a confmap.Conf into the unmarshable struct in a custom way.
// The confmap.Conf for this specific component may be nil or empty if no config available.
Unmarshal(component *confmap.Conf) error
// Marshal is a function that marshals the config into an interface to be encoded by
// the YAML encoder. Provides the configencoder.Encoder.
Marshal(encoder configencoder.Encoder) (interface{}, error)
}

// DataType is a special Type that represents the data types supported by the collector. We currently support
Expand Down
4 changes: 4 additions & 0 deletions config/configcompression/compressionType.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func IsCompressed(compressionType CompressionType) bool {
return compressionType != empty && compressionType != none
}

func (ct CompressionType) MarshalText() (out []byte, err error) {
return []byte(ct), nil
}

func (ct *CompressionType) UnmarshalText(in []byte) error {
switch typ := CompressionType(in); typ {
case Gzip,
Expand Down
6 changes: 6 additions & 0 deletions config/configtelemetry/configtelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
// that every component should generate.
type Level int32

var _ encoding.TextMarshaler = (*Level)(nil)
var _ encoding.TextUnmarshaler = (*Level)(nil)

func (l Level) String() string {
Expand All @@ -57,6 +58,11 @@ func (l Level) String() string {
return "unknown"
}

// MarshalText marshals Level to text.
func (l Level) MarshalText() (text []byte, err error) {
return []byte(l.String()), nil
}

// UnmarshalText unmarshalls text to a Level.
func (l *Level) UnmarshalText(text []byte) error {
if l == nil {
Expand Down
5 changes: 5 additions & 0 deletions config/identifiable.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func (id ComponentID) Name() string {
return id.nameVal
}

// MarshalText implements the encoding.TextMarshaler interface.
func (id ComponentID) MarshalText() (text []byte, err error) {
return []byte(id.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (id *ComponentID) UnmarshalText(text []byte) error {
idStr := string(text)
Expand Down
9 changes: 7 additions & 2 deletions receiver/otlpreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/service/configmarshaler/configencoder"
)

const (
Expand All @@ -32,8 +33,8 @@ const (

// Protocols is the configuration for the supported protocols.
type Protocols struct {
GRPC *configgrpc.GRPCServerSettings `mapstructure:"grpc"`
HTTP *confighttp.HTTPServerSettings `mapstructure:"http"`
GRPC *configgrpc.GRPCServerSettings `mapstructure:"grpc,omitempty"`
HTTP *confighttp.HTTPServerSettings `mapstructure:"http,omitempty"`
}

// Config defines configuration for OTLP receiver.
Expand Down Expand Up @@ -82,3 +83,7 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {

return nil
}

func (cfg *Config) Marshal(enc configencoder.Encoder) (interface{}, error) {
return enc.Encode(cfg)
}
20 changes: 20 additions & 0 deletions service/configmarshaler/configencoder/encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 configencoder // import "go.opentelemetry.io/collector/service/configmarshaler/configencoder"

type Encoder interface {
// Encode encodes the input into the output.
Encode(in interface{}) (out interface{}, err error)
}
40 changes: 40 additions & 0 deletions service/configmarshaler/configencoder/encodertest/encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 encodertest // import "go.opentelemetry.io/collector/service/configmarshaler/configencoder/encodertest"

import "go.opentelemetry.io/collector/service/configmarshaler/configencoder"

// nopEncoder mocks a configencoder.Encoder for test purposes.
type nopEncoder struct {
err error
}

var _ configencoder.Encoder = (*nopEncoder)(nil)

// NewNop returns a new instance of nopEncoder.
func NewNop() configencoder.Encoder {
return &nopEncoder{}
}

// NewNopWithErr returns a new instance of nopEncoder with
// an error set.
func NewNopWithErr(err error) configencoder.Encoder {
return &nopEncoder{err}
}

// Encode returns nil and the error if it is set.
func (ne nopEncoder) Encode(interface{}) (interface{}, error) {
return nil, ne.err
}
36 changes: 36 additions & 0 deletions service/configmarshaler/configencoder/encodertest/encoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 encodertest

import (
"errors"
"testing"

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

func TestNewNop(t *testing.T) {
encoder := NewNop()
got, err := encoder.Encode(nil)
require.NoError(t, err)
require.Nil(t, got)
}

func TestNewNopWithErr(t *testing.T) {
wantErr := errors.New("test")
encoder := NewNopWithErr(wantErr)
_, err := encoder.Encode(nil)
require.Equal(t, wantErr, err)
}
Loading

0 comments on commit 9728812

Please sign in to comment.