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

Commit 686dcf3

Browse files
tigrannajaryanPaulo Janotti
authored andcommitted
Add global factory registry for receivers, exporters and processors (#556)
The registry allows to register and retrieve factories for receivers, exporters and processors by type. The factories will be used during configuration loading and during pipeline building (only configuration related code is currently present, the rest will be added gradually). Testing done: automated tests.
1 parent d210c89 commit 686dcf3

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed

internal/factories/factories.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package factories
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/census-instrumentation/opencensus-service/internal/configmodels"
21+
)
22+
23+
///////////////////////////////////////////////////////////////////////////////
24+
// Receiver factory and its registry.
25+
26+
// ReceiverFactory is factory interface for receivers. Note: only configuration-related
27+
// functionality exists for now. We will add more factory functionality in the future.
28+
type ReceiverFactory interface {
29+
// Type gets the type of the Receiver created by this factory.
30+
Type() string
31+
32+
// CreateDefaultConfig creates the default configuration for the Receiver.
33+
CreateDefaultConfig() configmodels.Receiver
34+
}
35+
36+
// List of registered receiver factories.
37+
var receiverFactories = make(map[string]ReceiverFactory)
38+
39+
// RegisterReceiverFactory registers a receiver factory.
40+
func RegisterReceiverFactory(factory ReceiverFactory) error {
41+
if receiverFactories[factory.Type()] != nil {
42+
panic(fmt.Sprintf("duplicate receiver factory %q", factory.Type()))
43+
}
44+
45+
receiverFactories[factory.Type()] = factory
46+
return nil
47+
}
48+
49+
// GetReceiverFactory gets a receiver factory by type string.
50+
func GetReceiverFactory(typeStr string) ReceiverFactory {
51+
return receiverFactories[typeStr]
52+
}
53+
54+
///////////////////////////////////////////////////////////////////////////////
55+
// Exporter factory and its registry.
56+
57+
// ExporterFactory is factory interface for exporters. Note: only configuration-related
58+
// functionality exists for now. We will add more factory functionality in the future.
59+
type ExporterFactory interface {
60+
// Type gets the type of the Exporter created by this factory.
61+
Type() string
62+
63+
// CreateDefaultConfig creates the default configuration for the Exporter.
64+
CreateDefaultConfig() configmodels.Exporter
65+
}
66+
67+
// List of registered exporter factories.
68+
var exporterFactories = make(map[string]ExporterFactory)
69+
70+
// RegisterExporterFactory registers a exporter factory.
71+
func RegisterExporterFactory(factory ExporterFactory) error {
72+
if exporterFactories[factory.Type()] != nil {
73+
panic(fmt.Sprintf("duplicate exporter factory %q", factory.Type()))
74+
}
75+
76+
exporterFactories[factory.Type()] = factory
77+
return nil
78+
}
79+
80+
// GetExporterFactory gets a exporter factory by type string.
81+
func GetExporterFactory(typeStr string) ExporterFactory {
82+
return exporterFactories[typeStr]
83+
}
84+
85+
///////////////////////////////////////////////////////////////////////////////
86+
// Option factory and its registry.
87+
88+
// OptionFactory is factory interface for options. Note: only configuration-related
89+
// functionality exists for now. We will add more factory functionality in the future.
90+
type OptionFactory interface {
91+
// Type gets the type of the Option created by this factory.
92+
Type() string
93+
94+
// CreateDefaultConfig creates the default configuration for the Option.
95+
CreateDefaultConfig() configmodels.Processor
96+
}
97+
98+
// List of registered option factories.
99+
var optionFactories = make(map[string]OptionFactory)
100+
101+
// RegisterProcessorFactory registers a option factory.
102+
func RegisterProcessorFactory(factory OptionFactory) error {
103+
if optionFactories[factory.Type()] != nil {
104+
panic(fmt.Sprintf("duplicate option factory %q", factory.Type()))
105+
}
106+
107+
optionFactories[factory.Type()] = factory
108+
return nil
109+
}
110+
111+
// GetProcessorFactory gets a option factory by type string.
112+
func GetProcessorFactory(typeStr string) OptionFactory {
113+
return optionFactories[typeStr]
114+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package factories
16+
17+
import (
18+
"testing"
19+
20+
"github.com/census-instrumentation/opencensus-service/internal/configmodels"
21+
)
22+
23+
type ExampleReceiverFactory struct {
24+
}
25+
26+
// Type gets the type of the Receiver config created by this factory.
27+
func (f *ExampleReceiverFactory) Type() string {
28+
return "examplereceiver"
29+
}
30+
31+
// CreateDefaultConfig creates the default configuration for the Receiver.
32+
func (f *ExampleReceiverFactory) CreateDefaultConfig() configmodels.Receiver {
33+
return nil
34+
}
35+
36+
func TestRegisterReceiverFactory(t *testing.T) {
37+
f := ExampleReceiverFactory{}
38+
err := RegisterReceiverFactory(&f)
39+
if err != nil {
40+
t.Fatalf("cannot register factory")
41+
}
42+
43+
if &f != GetReceiverFactory(f.Type()) {
44+
t.Fatalf("cannot find factory")
45+
}
46+
47+
// Verify that attempt to register a factory with duplicate name panics
48+
panicked := false
49+
func() {
50+
defer func() {
51+
if r := recover(); r != nil {
52+
panicked = true
53+
}
54+
}()
55+
56+
err = RegisterReceiverFactory(&f)
57+
}()
58+
59+
if !panicked {
60+
t.Fatalf("must panic on double registration")
61+
}
62+
}
63+
64+
type ExampleExporterFactory struct {
65+
}
66+
67+
// Type gets the type of the Exporter config created by this factory.
68+
func (f *ExampleExporterFactory) Type() string {
69+
return "exampleexporter"
70+
}
71+
72+
// CreateDefaultConfig creates the default configuration for the Exporter.
73+
func (f *ExampleExporterFactory) CreateDefaultConfig() configmodels.Exporter {
74+
return nil
75+
}
76+
77+
func TestRegisterExporterFactory(t *testing.T) {
78+
f := ExampleExporterFactory{}
79+
err := RegisterExporterFactory(&f)
80+
if err != nil {
81+
t.Fatalf("cannot register factory")
82+
}
83+
84+
if &f != GetExporterFactory(f.Type()) {
85+
t.Fatalf("cannot find factory")
86+
}
87+
88+
// Verify that attempt to register a factory with duplicate name panics
89+
paniced := false
90+
func() {
91+
defer func() {
92+
if r := recover(); r != nil {
93+
paniced = true
94+
}
95+
}()
96+
97+
err = RegisterExporterFactory(&f)
98+
}()
99+
100+
if !paniced {
101+
t.Fatalf("must panic on double registration")
102+
}
103+
}
104+
105+
type ExampleOptionFactory struct {
106+
}
107+
108+
// Type gets the type of the Option config created by this factory.
109+
func (f *ExampleOptionFactory) Type() string {
110+
return "exampleoption"
111+
}
112+
113+
// CreateDefaultConfig creates the default configuration for the Processor.
114+
func (f *ExampleOptionFactory) CreateDefaultConfig() configmodels.Processor {
115+
return nil
116+
}
117+
118+
func TestRegisterOptionFactory(t *testing.T) {
119+
f := ExampleOptionFactory{}
120+
err := RegisterProcessorFactory(&f)
121+
if err != nil {
122+
t.Fatalf("cannot register factory")
123+
}
124+
125+
if &f != GetProcessorFactory(f.Type()) {
126+
t.Fatalf("cannot find factory")
127+
}
128+
129+
// Verify that attempt to register a factory with duplicate name panics
130+
paniced := false
131+
func() {
132+
defer func() {
133+
if r := recover(); r != nil {
134+
paniced = true
135+
}
136+
}()
137+
138+
err = RegisterProcessorFactory(&f)
139+
}()
140+
141+
if !paniced {
142+
t.Fatalf("must panic on double registration")
143+
}
144+
}

0 commit comments

Comments
 (0)