Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit 7a7c903

Browse files
tigrannajaryanPaulo Janotti
authored andcommitted
Add implementation of OpenCensus receiver config (#562)
* Add implementation of OpenCensus receiver config - Added factory and config loading for OpenCensus receiver. - Added support for custom unmarshaling of receiver configuration. - Added ability for factory to create trace and metric receivers based on configuration. This will be later required when building the pipeline based on the config. - Moved example factories used in tests to example_factories.go to make it easier to reuse them in other tests. Testing done: automated tests * Fix PR comments.
1 parent 65c89d8 commit 7a7c903

File tree

11 files changed

+567
-149
lines changed

11 files changed

+567
-149
lines changed

internal/configv2/configv2.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,16 @@ func loadReceivers(v *viper.Viper) (configmodels.Receivers, error) {
200200

201201
// Now that the default config struct is created we can Unmarshal into it
202202
// and it will apply user-defined config on top of the default.
203-
if err := subViper.UnmarshalKey(key, receiverCfg); err != nil {
203+
customUnmarshaler := factory.CustomUnmarshaler()
204+
if customUnmarshaler != nil {
205+
// This configuration requires a custom unmarshaler, use it.
206+
err = customUnmarshaler(subViper, key, receiverCfg)
207+
} else {
208+
// Standard viper unmarshaler is fine.
209+
err = subViper.UnmarshalKey(key, receiverCfg)
210+
}
211+
212+
if err != nil {
204213
return nil, &configError{
205214
code: errUnmarshalError,
206215
msg: fmt.Sprintf("error reading settings for receiver type %q: %v", typeStr, err),

internal/configv2/configv2_test.go

Lines changed: 4 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -15,164 +15,20 @@
1515
package configv2
1616

1717
import (
18-
"os"
1918
"path"
2019
"testing"
2120

22-
"github.com/spf13/viper"
2321
"github.com/stretchr/testify/assert"
2422

2523
"github.com/census-instrumentation/opencensus-service/internal/configmodels"
26-
"github.com/census-instrumentation/opencensus-service/internal/factories"
2724
)
2825

29-
// ExampleReceiver: for testing purposes we are defining an example config and factory
30-
// for "examplereceiver" receiver type.
31-
type ExampleReceiver struct {
32-
configmodels.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
33-
ExtraSetting string `mapstructure:"extra"`
34-
}
35-
36-
type ExampleReceiverFactory struct {
37-
}
38-
39-
// Type gets the type of the Receiver config created by this factory.
40-
func (f *ExampleReceiverFactory) Type() string {
41-
return "examplereceiver"
42-
}
43-
44-
// CreateDefaultConfig creates the default configuration for the Receiver.
45-
func (f *ExampleReceiverFactory) CreateDefaultConfig() configmodels.Receiver {
46-
return &ExampleReceiver{
47-
ReceiverSettings: configmodels.ReceiverSettings{
48-
Endpoint: "localhost:1000",
49-
Enabled: false,
50-
},
51-
ExtraSetting: "some string",
52-
}
53-
}
54-
55-
// MultiProtoReceiver: for testing purposes we are defining an example multi protocol
56-
// config and factory for "multireceiver" receiver type.
57-
type MultiProtoReceiver struct {
58-
Protocols map[string]MultiProtoReceiverOneCfg `mapstructure:"protocols"`
59-
}
60-
61-
var _ configmodels.Receiver = (*MultiProtoReceiver)(nil)
62-
63-
type MultiProtoReceiverOneCfg struct {
64-
Enabled bool `mapstructure:"enabled"`
65-
Endpoint string `mapstructure:"endpoint"`
66-
ExtraSetting string `mapstructure:"extra"`
67-
}
68-
69-
type MultiProtoReceiverFactory struct {
70-
}
71-
72-
// Type gets the type of the Receiver config created by this factory.
73-
func (f *MultiProtoReceiverFactory) Type() string {
74-
return "multireceiver"
75-
}
76-
77-
// CreateDefaultConfig creates the default configuration for the Receiver.
78-
func (f *MultiProtoReceiverFactory) CreateDefaultConfig() configmodels.Receiver {
79-
return &MultiProtoReceiver{
80-
Protocols: map[string]MultiProtoReceiverOneCfg{
81-
"http": {
82-
Enabled: false,
83-
Endpoint: "example.com:8888",
84-
ExtraSetting: "extra string 1",
85-
},
86-
"tcp": {
87-
Enabled: false,
88-
Endpoint: "omnition.com:9999",
89-
ExtraSetting: "extra string 2",
90-
},
91-
},
92-
}
93-
}
94-
95-
// ExampleExporter: for testing purposes we are defining an example config and factory
96-
// for "exampleexporter" exporter type.
97-
type ExampleExporter struct {
98-
configmodels.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
99-
ExtraSetting string `mapstructure:"extra"`
100-
}
101-
102-
type ExampleExporterFactory struct {
103-
}
104-
105-
// Type gets the type of the Exporter config created by this factory.
106-
func (f *ExampleExporterFactory) Type() string {
107-
return "exampleexporter"
108-
}
109-
110-
// CreateDefaultConfig creates the default configuration for the Exporter.
111-
func (f *ExampleExporterFactory) CreateDefaultConfig() configmodels.Exporter {
112-
return &ExampleExporter{
113-
ExporterSettings: configmodels.ExporterSettings{
114-
Enabled: false,
115-
},
116-
ExtraSetting: "some export string",
117-
}
118-
}
119-
120-
// ExampleProcessor: for testing purposes we are defining an example config and factory
121-
// for "exampleprocessor" processor type.
122-
type ExampleProcessor struct {
123-
configmodels.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
124-
ExtraSetting string `mapstructure:"extra"`
125-
}
126-
127-
type ExampleProcessorFactory struct {
128-
}
129-
130-
// Type gets the type of the Processor config created by this factory.
131-
func (f *ExampleProcessorFactory) Type() string {
132-
return "exampleprocessor"
133-
}
134-
135-
// CreateDefaultConfig creates the default configuration for the Processor.
136-
func (f *ExampleProcessorFactory) CreateDefaultConfig() configmodels.Processor {
137-
return &ExampleProcessor{
138-
ProcessorSettings: configmodels.ProcessorSettings{
139-
Enabled: false,
140-
},
141-
ExtraSetting: "some export string",
142-
}
143-
}
144-
145-
// Register all factories
146-
var _ = factories.RegisterReceiverFactory(&ExampleReceiverFactory{})
147-
var _ = factories.RegisterReceiverFactory(&MultiProtoReceiverFactory{})
148-
var _ = factories.RegisterExporterFactory(&ExampleExporterFactory{})
149-
var _ = factories.RegisterProcessorFactory(&ExampleProcessorFactory{})
150-
151-
func loadConfigFile(t *testing.T, fileName string) (*configmodels.ConfigV2, error) {
152-
// Open the file for reading.
153-
file, err := os.Open(fileName)
154-
if err != nil {
155-
t.Error(err)
156-
return nil, err
157-
}
158-
159-
// Read yaml config from file
160-
v := viper.New()
161-
v.SetConfigType("yaml")
162-
err = v.ReadConfig(file)
163-
if err != nil {
164-
t.Errorf("unable to read yaml, %v", err)
165-
return nil, err
166-
}
167-
168-
// Load the config from viper
169-
return Load(v)
170-
}
26+
var _ = RegisterTestFactories()
17127

17228
func TestDecodeConfig(t *testing.T) {
17329

17430
// Load the config
175-
config, err := loadConfigFile(t, path.Join(".", "testdata", "valid-config.yaml"))
31+
config, err := LoadConfigFile(t, path.Join(".", "testdata", "valid-config.yaml"))
17632
if err != nil {
17733
t.Fatalf("unable to load config, %v", err)
17834
}
@@ -244,7 +100,7 @@ func TestDecodeConfig(t *testing.T) {
244100
func TestDecodeConfig_MultiProto(t *testing.T) {
245101

246102
// Load the config
247-
config, err := loadConfigFile(t, path.Join(".", "testdata", "multiproto-config.yaml"))
103+
config, err := LoadConfigFile(t, path.Join(".", "testdata", "multiproto-config.yaml"))
248104
if err != nil {
249105
t.Fatalf("unable to load config, %v", err)
250106
}
@@ -325,7 +181,7 @@ func TestDecodeConfig_Invalid(t *testing.T) {
325181
}
326182

327183
for _, test := range testCases {
328-
_, err := loadConfigFile(t, path.Join(".", "testdata", test.name+".yaml"))
184+
_, err := LoadConfigFile(t, path.Join(".", "testdata", test.name+".yaml"))
329185
if err == nil {
330186
t.Errorf("expected error but succedded on invalid config case: %s", test.name)
331187
} else if test.expected != 0 {

0 commit comments

Comments
 (0)