Skip to content

Commit 631053d

Browse files
committed
[chore] simplify exporter code
1 parent a36367b commit 631053d

File tree

2 files changed

+11
-33
lines changed

2 files changed

+11
-33
lines changed

internal/components/exporters/helpers.go

+2-23
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,8 @@ import (
1919
)
2020

2121
// registry holds a record of all known receiver parsers.
22-
var registry = make(map[string]components.Parser)
23-
24-
// Register adds a new parser builder to the list of known builders.
25-
func Register(name string, p components.Parser) {
26-
registry[name] = p
27-
}
28-
29-
// IsRegistered checks whether a parser is registered with the given name.
30-
func IsRegistered(name string) bool {
31-
_, ok := registry[name]
32-
return ok
22+
var registry = map[string]components.Parser{
23+
"prometheus": components.NewSinglePortParserBuilder("prometheus", 8888).MustBuild(),
3324
}
3425

3526
// ParserFor returns a parser builder for the given exporter name.
@@ -40,15 +31,3 @@ func ParserFor(name string) components.Parser {
4031
// We want the default for exporters to fail silently.
4132
return components.NewBuilder[any]().WithName(name).MustBuild()
4233
}
43-
44-
var (
45-
componentParsers = []components.Parser{
46-
components.NewSinglePortParserBuilder("prometheus", 8888).MustBuild(),
47-
}
48-
)
49-
50-
func init() {
51-
for _, parser := range componentParsers {
52-
Register(parser.ParserType(), parser)
53-
}
54-
}

internal/components/exporters/helpers_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package exporters_test
15+
package exporters
1616

1717
import (
1818
"testing"
@@ -21,13 +21,12 @@ import (
2121
"github.com/stretchr/testify/assert"
2222

2323
"github.com/open-telemetry/opentelemetry-operator/internal/components"
24-
"github.com/open-telemetry/opentelemetry-operator/internal/components/exporters"
2524
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
2625
)
2726

2827
func TestParserForReturns(t *testing.T) {
2928
const testComponentName = "test"
30-
parser := exporters.ParserFor(testComponentName)
29+
parser := ParserFor(testComponentName)
3130
assert.Equal(t, "test", parser.ParserType())
3231
assert.Equal(t, "__test", parser.ParserName())
3332
ports, err := parser.Ports(logr.Discard(), testComponentName, map[string]interface{}{
@@ -39,9 +38,8 @@ func TestParserForReturns(t *testing.T) {
3938

4039
func TestCanRegister(t *testing.T) {
4140
const testComponentName = "test"
42-
exporters.Register(testComponentName, components.NewSinglePortParserBuilder(testComponentName, 9000).MustBuild())
43-
assert.True(t, exporters.IsRegistered(testComponentName))
44-
parser := exporters.ParserFor(testComponentName)
41+
registry[testComponentName] = components.NewSinglePortParserBuilder(testComponentName, 9000).MustBuild()
42+
parser := ParserFor(testComponentName)
4543
assert.Equal(t, "test", parser.ParserType())
4644
assert.Equal(t, "__test", parser.ParserName())
4745
ports, err := parser.Ports(logr.Discard(), testComponentName, map[string]interface{}{})
@@ -60,11 +58,12 @@ func TestExporterComponentParsers(t *testing.T) {
6058
} {
6159
t.Run(tt.exporterName, func(t *testing.T) {
6260
t.Run("is registered", func(t *testing.T) {
63-
assert.True(t, exporters.IsRegistered(tt.exporterName))
61+
_, ok := registry[tt.exporterName]
62+
assert.True(t, ok)
6463
})
6564
t.Run("bad config errors", func(t *testing.T) {
6665
// prepare
67-
parser := exporters.ParserFor(tt.exporterName)
66+
parser := ParserFor(tt.exporterName)
6867

6968
// test throwing in pure junk
7069
_, err := parser.Ports(logr.Discard(), tt.exporterName, func() {})
@@ -75,7 +74,7 @@ func TestExporterComponentParsers(t *testing.T) {
7574

7675
t.Run("assigns the expected port", func(t *testing.T) {
7776
// prepare
78-
parser := exporters.ParserFor(tt.exporterName)
77+
parser := ParserFor(tt.exporterName)
7978

8079
// test
8180
ports, err := parser.Ports(logr.Discard(), tt.exporterName, map[string]interface{}{})
@@ -93,7 +92,7 @@ func TestExporterComponentParsers(t *testing.T) {
9392

9493
t.Run("allows port to be overridden", func(t *testing.T) {
9594
// prepare
96-
parser := exporters.ParserFor(tt.exporterName)
95+
parser := ParserFor(tt.exporterName)
9796

9897
// test
9998
ports, err := parser.Ports(logr.Discard(), tt.exporterName, map[string]interface{}{

0 commit comments

Comments
 (0)