Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] simplify exporter code #3642

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions internal/components/exporters/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ import (
)

// registry holds a record of all known receiver parsers.
var registry = make(map[string]components.Parser)

// Register adds a new parser builder to the list of known builders.
func Register(name string, p components.Parser) {
registry[name] = p
}

// IsRegistered checks whether a parser is registered with the given name.
func IsRegistered(name string) bool {
_, ok := registry[name]
return ok
var registry = map[string]components.Parser{
"prometheus": components.NewSinglePortParserBuilder("prometheus", 8888).MustBuild(),
}

// ParserFor returns a parser builder for the given exporter name.
Expand All @@ -29,15 +20,3 @@ func ParserFor(name string) components.Parser {
// We want the default for exporters to fail silently.
return components.NewBuilder[any]().WithName(name).MustBuild()
}

var (
componentParsers = []components.Parser{
components.NewSinglePortParserBuilder("prometheus", 8888).MustBuild(),
}
)

func init() {
for _, parser := range componentParsers {
Register(parser.ParserType(), parser)
}
}
19 changes: 9 additions & 10 deletions internal/components/exporters/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package exporters_test
package exporters

import (
"testing"
Expand All @@ -10,13 +10,12 @@ import (
"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/internal/components"
"github.com/open-telemetry/opentelemetry-operator/internal/components/exporters"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

func TestParserForReturns(t *testing.T) {
const testComponentName = "test"
parser := exporters.ParserFor(testComponentName)
parser := ParserFor(testComponentName)
assert.Equal(t, "test", parser.ParserType())
assert.Equal(t, "__test", parser.ParserName())
ports, err := parser.Ports(logr.Discard(), testComponentName, map[string]interface{}{
Expand All @@ -28,9 +27,8 @@ func TestParserForReturns(t *testing.T) {

func TestCanRegister(t *testing.T) {
const testComponentName = "test"
exporters.Register(testComponentName, components.NewSinglePortParserBuilder(testComponentName, 9000).MustBuild())
assert.True(t, exporters.IsRegistered(testComponentName))
parser := exporters.ParserFor(testComponentName)
registry[testComponentName] = components.NewSinglePortParserBuilder(testComponentName, 9000).MustBuild()
parser := ParserFor(testComponentName)
assert.Equal(t, "test", parser.ParserType())
assert.Equal(t, "__test", parser.ParserName())
ports, err := parser.Ports(logr.Discard(), testComponentName, map[string]interface{}{})
Expand All @@ -49,11 +47,12 @@ func TestExporterComponentParsers(t *testing.T) {
} {
t.Run(tt.exporterName, func(t *testing.T) {
t.Run("is registered", func(t *testing.T) {
assert.True(t, exporters.IsRegistered(tt.exporterName))
_, ok := registry[tt.exporterName]
assert.True(t, ok)
})
t.Run("bad config errors", func(t *testing.T) {
// prepare
parser := exporters.ParserFor(tt.exporterName)
parser := ParserFor(tt.exporterName)

// test throwing in pure junk
_, err := parser.Ports(logr.Discard(), tt.exporterName, func() {})
Expand All @@ -64,7 +63,7 @@ func TestExporterComponentParsers(t *testing.T) {

t.Run("assigns the expected port", func(t *testing.T) {
// prepare
parser := exporters.ParserFor(tt.exporterName)
parser := ParserFor(tt.exporterName)

// test
ports, err := parser.Ports(logr.Discard(), tt.exporterName, map[string]interface{}{})
Expand All @@ -82,7 +81,7 @@ func TestExporterComponentParsers(t *testing.T) {

t.Run("allows port to be overridden", func(t *testing.T) {
// prepare
parser := exporters.ParserFor(tt.exporterName)
parser := ParserFor(tt.exporterName)

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