Skip to content

Commit

Permalink
[chore] Use component.MustNewType everywhere (#31072)
Browse files Browse the repository at this point in the history
**Description:** Replaces usages of `component.Type(..)` by
`component.MustNewType`

**Link to tracking Issue:** Needed for
open-telemetry/opentelemetry-collector/pull/9472
  • Loading branch information
mx-psi authored Feb 6, 2024
1 parent 8ae1539 commit eb6e6f6
Show file tree
Hide file tree
Showing 24 changed files with 67 additions and 43 deletions.
5 changes: 4 additions & 1 deletion cmd/configschema/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func GetAllCfgInfos(components otelcol.Factories) []CfgInfo {
// config for the component specified by the passed-in componentType and
// componentName.
func GetCfgInfo(components otelcol.Factories, componentType, componentName string) (CfgInfo, error) {
t := component.Type(componentName)
t, err := component.NewType(componentName)
if err != nil {
return CfgInfo{}, fmt.Errorf("failed to build component.Type: %w", err)
}
switch componentType {
case receiver:
f := components.Receivers[t]
Expand Down
12 changes: 7 additions & 5 deletions cmd/otelcontribcol/distribution_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ func TestComponentsArePresent(t *testing.T) {
tt.Skip("component deprecated, skipping", metadataComponent)
return
}

cType := component.MustNewType(m.Type)
switch m.Status.Class {
case "connector":
assert.NotNil(tt, components.Connectors[component.Type(m.Type)], "missing connector: %s", m.Type)
assert.NotNil(tt, components.Connectors[cType], "missing connector: %s", m.Type)
case "exporter":
assert.NotNil(tt, components.Exporters[component.Type(m.Type)], "missing exporter: %s", m.Type)
assert.NotNil(tt, components.Exporters[cType], "missing exporter: %s", m.Type)
case "extension":
assert.NotNil(tt, components.Extensions[component.Type(m.Type)], "missing extension: %s", m.Type)
assert.NotNil(tt, components.Extensions[cType], "missing extension: %s", m.Type)
case "processor":
assert.NotNil(tt, components.Processors[component.Type(m.Type)], "missing processor: %s", m.Type)
assert.NotNil(tt, components.Processors[cType], "missing processor: %s", m.Type)
case "receiver":
assert.NotNil(tt, components.Receivers[component.Type(m.Type)], "missing receiver: %s", m.Type)
assert.NotNil(tt, components.Receivers[cType], "missing receiver: %s", m.Type)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/clickhouseexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {
defaultCfg := createDefaultConfig()
defaultCfg.(*Config).Endpoint = defaultEndpoint

storageID := component.NewIDWithName(component.Type("file_storage"), "clickhouse")
storageID := component.MustNewIDWithName("file_storage", "clickhouse")

tests := []struct {
id component.ID
Expand Down
5 changes: 3 additions & 2 deletions exporter/sumologicexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter/exporterhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sumologicexporter/internal/metadata"
)

func TestType(t *testing.T) {
factory := NewFactory()
pType := factory.Type()
assert.Equal(t, pType, component.Type("sumologic"))
assert.Equal(t, pType, metadata.Type)
}

func TestCreateDefaultConfig(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions exporter/syslogexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (

"github.com/cenkalti/backoff/v4"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter/exporterhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter/internal/metadata"
)

func TestType(t *testing.T) {
factory := NewFactory()
pType := factory.Type()
assert.Equal(t, pType, component.Type("syslog"))
assert.Equal(t, pType, metadata.Type)
}

func TestCreateDefaultConfig(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions extension/httpforwarder/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (

func TestFactory(t *testing.T) {
f := NewFactory()
expectType := "http_forwarder"
require.Equal(t, component.Type(expectType), f.Type())
expectType := component.MustNewType("http_forwarder")
require.Equal(t, expectType, f.Type())

cfg := f.CreateDefaultConfig().(*Config)
require.Equal(t, ":6060", cfg.Ingress.Endpoint)
Expand Down
4 changes: 2 additions & 2 deletions extension/storage/storagetest/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"go.opentelemetry.io/collector/extension/experimental/storage"
)

var testStorageType component.Type = "test_storage"
var testStorageType component.Type = component.MustNewType("test_storage")

// TestStorage is an in memory storage extension designed for testing
type TestStorage struct {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (s *TestStorage) GetClient(ctx context.Context, kind component.Kind, ent co
return client, setCreatorID(ctx, client, s.ID)
}

var nonStorageType component.Type = "non_storage"
var nonStorageType component.Type = component.MustNewType("non_storage")

// NonStorage is useful for testing expected behaviors that involve
// non-storage extensions
Expand Down
10 changes: 6 additions & 4 deletions pkg/stanza/adapter/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ func BenchmarkEndToEnd(b *testing.B) {
}
}

const benchType = "bench"
const benchTypeStr = "bench"

var benchType = component.MustNewType(benchTypeStr)

type BenchConfig struct {
BaseConfig
Expand Down Expand Up @@ -139,18 +141,18 @@ func (f BenchReceiverType) InputConfig(cfg component.Config) operator.Config {
}

func init() {
operator.Register(benchType, func() operator.Builder { return NewBenchOpConfig() })
operator.Register(benchTypeStr, func() operator.Builder { return NewBenchOpConfig() })
}

// NewBenchOpConfig creates a new benchmarking operator config with default values
func NewBenchOpConfig() *BenchOpConfig {
return NewBenchOpConfigWithID(benchType)
return NewBenchOpConfigWithID(benchTypeStr)
}

// NewBenchOpConfigWithID creates a new noop operator config with default values
func NewBenchOpConfigWithID(operatorID string) *BenchOpConfig {
return &BenchOpConfig{
InputConfig: helper.NewInputConfig(operatorID, benchType),
InputConfig: helper.NewInputConfig(operatorID, benchTypeStr),
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/stanza/adapter/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (o *UnstartableOperator) Process(_ context.Context, _ *entry.Entry) error {
return nil
}

const testType = "test"
const testTypeStr = "test"

var testType = component.MustNewType(testTypeStr)

type TestConfig struct {
BaseConfig `mapstructure:",squash"`
Expand Down
4 changes: 3 additions & 1 deletion processor/cumulativetodeltaprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor/processortest"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor/internal/metadata"
)

func TestType(t *testing.T) {
factory := NewFactory()
pType := factory.Type()
assert.Equal(t, pType, component.Type("cumulativetodelta"))
assert.Equal(t, pType, metadata.Type)
}

func TestCreateDefaultConfig(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion processor/deltatorateprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor/processortest"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatorateprocessor/internal/metadata"
)

func TestType(t *testing.T) {
factory := NewFactory()
pType := factory.Type()
assert.Equal(t, pType, component.Type("deltatorate"))
assert.Equal(t, pType, metadata.Type)
}

func TestCreateDefaultConfig(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion processor/filterprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (
"go.opentelemetry.io/collector/processor/processortest"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/filterprocessor/internal/metadata"
)

func TestType(t *testing.T) {
factory := NewFactory()
pType := factory.Type()

assert.Equal(t, pType, component.Type("filter"))
assert.Equal(t, pType, metadata.Type)
}

func TestCreateDefaultConfig(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion processor/metricsgenerationprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor/processortest"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricsgenerationprocessor/internal/metadata"
)

func TestType(t *testing.T) {
factory := NewFactory()
pType := factory.Type()
assert.Equal(t, pType, component.Type("experimental_metricsgeneration"))
assert.Equal(t, pType, metadata.Type)
}

func TestCreateDefaultConfig(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion processor/metricstransformprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor/processortest"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstransformprocessor/internal/metadata"
)

func TestType(t *testing.T) {
factory := NewFactory()
pType := factory.Type()
assert.Equal(t, pType, component.Type("metricstransform"))
assert.Equal(t, pType, metadata.Type)
}

func TestCreateDefaultConfig(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions receiver/azureeventhubreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver/internal/metadata"
)

func Test_NewFactory(t *testing.T) {
f := NewFactory()
assert.Equal(t, component.Type("azureeventhub"), f.Type())
assert.Equal(t, metadata.Type, f.Type())
}

func Test_NewLogsReceiver(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions receiver/chronyreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
Expand All @@ -22,7 +21,7 @@ func TestType(t *testing.T) {
t.Parallel()

factory := NewFactory()
assert.Equal(t, component.Type("chrony"), factory.Type(), "Must match the expected type")
assert.Equal(t, metadata.Type, factory.Type(), "Must match the expected type")
}

func TestValidConfig(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions receiver/fluentforwardreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/fluentforwardreceiver/internal/metadata"
)

func TestCreateDefaultConfig(t *testing.T) {
Expand All @@ -27,7 +28,7 @@ func TestCreateReceiver(t *testing.T) {
cfg := factory.CreateDefaultConfig().(*Config)
cfg.ListenAddress = "localhost:0" // Endpoint is required, not going to be used here.

require.Equal(t, component.Type("fluentforward"), factory.Type())
require.Equal(t, metadata.Type, factory.Type())

tReceiver, err := factory.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, consumertest.NewNop())
assert.Nil(t, err, "receiver creation failed")
Expand Down
2 changes: 1 addition & 1 deletion receiver/jmxreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestClassPathParse(t *testing.T) {

func TestWithInvalidConfig(t *testing.T) {
f := NewFactory()
assert.Equal(t, component.Type("jmx"), f.Type())
assert.Equal(t, metadata.Type, f.Type())

cfg := f.CreateDefaultConfig().(*Config)
require.NotNil(t, cfg)
Expand Down
5 changes: 3 additions & 2 deletions receiver/jmxreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jmxreceiver/internal/metadata"
)

func TestWithValidConfig(t *testing.T) {
mockJarVersions()
defer unmockJarVersions()

f := NewFactory()
assert.Equal(t, component.Type("jmx"), f.Type())
assert.Equal(t, metadata.Type, f.Type())

cfg := f.CreateDefaultConfig()
cfg.(*Config).Endpoint = "myendpoint:12345"
Expand Down
4 changes: 2 additions & 2 deletions receiver/k8sclusterreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func TestFactory(t *testing.T) {
f := NewFactory()
require.Equal(t, component.Type("k8s_cluster"), f.Type())
require.Equal(t, metadata.Type, f.Type())

cfg := f.CreateDefaultConfig()
rCfg, ok := cfg.(*Config)
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestFactory(t *testing.T) {

func TestFactoryDistributions(t *testing.T) {
f := NewFactory()
require.Equal(t, component.Type("k8s_cluster"), f.Type())
require.Equal(t, metadata.Type, f.Type())

cfg := f.CreateDefaultConfig()
rCfg, ok := cfg.(*Config)
Expand Down
4 changes: 2 additions & 2 deletions receiver/k8seventsreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8seventsreceiver/internal/metadata"
)

func TestDefaultConfig(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestDefaultConfig(t *testing.T) {
}

func TestFactoryType(t *testing.T) {
assert.Equal(t, component.Type("k8s_events"), NewFactory().Type())
assert.Equal(t, metadata.Type, NewFactory().Type())
}

func TestCreateReceiver(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions receiver/k8sobjectsreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sobjectsreceiver/internal/metadata"
)

func TestDefaultConfig(t *testing.T) {
Expand All @@ -31,7 +31,7 @@ func TestDefaultConfig(t *testing.T) {

func TestFactoryType(t *testing.T) {
t.Parallel()
assert.Equal(t, component.Type("k8sobjects"), NewFactory().Type())
assert.Equal(t, metadata.Type, NewFactory().Type())
}

func TestCreateReceiver(t *testing.T) {
Expand Down
Loading

0 comments on commit eb6e6f6

Please sign in to comment.