Skip to content

Commit

Permalink
[chore] Switch to component.MustNewID and `component.MustNewIDWithN…
Browse files Browse the repository at this point in the history
…ame` when passing literal strings (open-telemetry#31125)

**Description:** 

Runs the following commands:

- `rg "component.NewID\(\".*?\"\)" -l | xargs sd
'component.NewID\((".*?")\)' 'component.MustNewID($1)'`
- `rg "component.NewIDWithName\(\".*?\"" -l | xargs sd
'component.NewIDWithName\((".*?")' 'component. MustNewIDWithName($1'`

Additionally, makes some manual fixes

Needed for open-telemetry/opentelemetry-collector/pull/9472

**Link to tracking Issue:**
open-telemetry/opentelemetry-collector/issues/9208
  • Loading branch information
mx-psi authored and anthoai97 committed Feb 12, 2024
1 parent 088465c commit c395737
Show file tree
Hide file tree
Showing 50 changed files with 202 additions and 202 deletions.
4 changes: 2 additions & 2 deletions connector/servicegraphconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestStaleSeriesCleanup(t *testing.T) {

mHost := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeMetrics: {
component.NewID("mock"): mockMetricsExporter,
component.MustNewID("mock"): mockMetricsExporter,
},
})

Expand Down Expand Up @@ -365,7 +365,7 @@ func TestValidateOwnTelemetry(t *testing.T) {

mHost := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeMetrics: {
component.NewID("mock"): mockMetricsExporter,
component.MustNewID("mock"): mockMetricsExporter,
},
})

Expand Down
10 changes: 5 additions & 5 deletions exporter/awss3exporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cfg)

e := cfg.Exporters[component.NewID("awss3")].(*Config)
e := cfg.Exporters[component.MustNewID("awss3")].(*Config)
assert.Equal(t, e,
&Config{
S3Uploader: S3UploaderConfig{
Expand All @@ -51,7 +51,7 @@ func TestConfig(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cfg)

e := cfg.Exporters[component.NewID("awss3")].(*Config)
e := cfg.Exporters[component.MustNewID("awss3")].(*Config)

assert.Equal(t, e,
&Config{
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestConfigForS3CompatibleSystems(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cfg)

e := cfg.Exporters[component.NewID("awss3")].(*Config)
e := cfg.Exporters[component.MustNewID("awss3")].(*Config)

assert.Equal(t, e,
&Config{
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestMarshallerName(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cfg)

e := cfg.Exporters[component.NewID("awss3")].(*Config)
e := cfg.Exporters[component.MustNewID("awss3")].(*Config)

assert.Equal(t, e,
&Config{
Expand All @@ -180,7 +180,7 @@ func TestMarshallerName(t *testing.T) {
},
)

e = cfg.Exporters[component.NewIDWithName("awss3", "proto")].(*Config)
e = cfg.Exporters[component.MustNewIDWithName("awss3", "proto")].(*Config)

assert.Equal(t, e,
&Config{
Expand Down
2 changes: 1 addition & 1 deletion exporter/azuremonitorexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestLoadConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

disk := component.NewIDWithName("disk", "")
disk := component.MustNewIDWithName("disk", "")

tests := []struct {
id component.ID
Expand Down
2 changes: 1 addition & 1 deletion exporter/kineticaexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestLoadConfig(t *testing.T) {
}{

{
id: component.NewIDWithName("kinetica", ""),
id: component.MustNewIDWithName("kinetica", ""),
expected: defaultCfg,
},
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/loadbalancingexporter/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestRemoveExtraExporters(t *testing.T) {
func TestAddMissingExporters(t *testing.T) {
// prepare
cfg := simpleConfig()
exporterFactory := exporter.NewFactory("otlp", func() component.Config {
exporterFactory := exporter.NewFactory(component.MustNewType("otlp"), func() component.Config {
return &otlpexporter.Config{}
}, exporter.WithTraces(func(
_ context.Context,
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestFailedToAddMissingExporters(t *testing.T) {
// prepare
cfg := simpleConfig()
expectedErr := errors.New("some expected error")
exporterFactory := exporter.NewFactory("otlp", func() component.Config {
exporterFactory := exporter.NewFactory(component.MustNewType("otlp"), func() component.Config {
return &otlpexporter.Config{}
}, exporter.WithTraces(func(
_ context.Context,
Expand Down
2 changes: 1 addition & 1 deletion exporter/opensearchexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestLoadConfig(t *testing.T) {
},
MaxIdleConns: &maxIdleConns,
IdleConnTimeout: &idleConnTimeout,
Auth: &configauth.Authentication{AuthenticatorID: component.NewID("sample_basic_auth")},
Auth: &configauth.Authentication{AuthenticatorID: component.MustNewID("sample_basic_auth")},
},
BackOffConfig: configretry.BackOffConfig{
Enabled: true,
Expand Down
2 changes: 1 addition & 1 deletion exporter/otelarrowexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestUnmarshalConfig(t *testing.T) {
},
WriteBufferSize: 512 * 1024,
BalancerName: "experimental",
Auth: &configauth.Authentication{AuthenticatorID: component.NewID("nop")},
Auth: &configauth.Authentication{AuthenticatorID: component.MustNewID("nop")},
},
Arrow: ArrowSettings{
NumStreams: 2,
Expand Down
12 changes: 6 additions & 6 deletions extension/pprofextension/pprofextension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestPerformanceProfilerExtensionUsage(t *testing.T) {
BlockProfileFraction: 3,
MutexProfileFraction: 5,
}
tt, err := componenttest.SetupTelemetry(component.NewID("TestPprofExtension"))
tt, err := componenttest.SetupTelemetry(component.MustNewID("TestPprofExtension"))
require.NoError(t, err, "SetupTelemetry should succeed")

pprofExt := newServer(config, tt.TelemetrySettings())
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestPerformanceProfilerExtensionPortAlreadyInUse(t *testing.T) {
Endpoint: endpoint,
},
}
tt, err := componenttest.SetupTelemetry(component.NewID("TestPprofExtension"))
tt, err := componenttest.SetupTelemetry(component.MustNewID("TestPprofExtension"))
require.NoError(t, err, "SetupTelemetry should succeed")
pprofExt := newServer(config, tt.TelemetrySettings())
require.NotNil(t, pprofExt)
Expand All @@ -76,7 +76,7 @@ func TestPerformanceProfilerMultipleStarts(t *testing.T) {
},
}

tt, err := componenttest.SetupTelemetry(component.NewID("TestPprofExtension"))
tt, err := componenttest.SetupTelemetry(component.MustNewID("TestPprofExtension"))
require.NoError(t, err, "SetupTelemetry should succeed")
pprofExt := newServer(config, tt.TelemetrySettings())
require.NotNil(t, pprofExt)
Expand All @@ -95,7 +95,7 @@ func TestPerformanceProfilerMultipleShutdowns(t *testing.T) {
},
}

tt, err := componenttest.SetupTelemetry(component.NewID("TestPprofExtension"))
tt, err := componenttest.SetupTelemetry(component.MustNewID("TestPprofExtension"))
require.NoError(t, err, "SetupTelemetry should succeed")
pprofExt := newServer(config, tt.TelemetrySettings())
require.NotNil(t, pprofExt)
Expand All @@ -111,7 +111,7 @@ func TestPerformanceProfilerShutdownWithoutStart(t *testing.T) {
Endpoint: testutil.GetAvailableLocalAddress(t),
},
}
tt, err := componenttest.SetupTelemetry(component.NewID("TestPprofExtension"))
tt, err := componenttest.SetupTelemetry(component.MustNewID("TestPprofExtension"))
require.NoError(t, err, "SetupTelemetry should succeed")
pprofExt := newServer(config, tt.TelemetrySettings())
require.NotNil(t, pprofExt)
Expand All @@ -133,7 +133,7 @@ func TestPerformanceProfilerLifecycleWithFile(t *testing.T) {
},
SaveToFile: tmpFile.Name(),
}
tt, err := componenttest.SetupTelemetry(component.NewID("TestPprofExtension"))
tt, err := componenttest.SetupTelemetry(component.MustNewID("TestPprofExtension"))
require.NoError(t, err, "SetupTelemetry should succeed")
pprofExt := newServer(config, tt.TelemetrySettings())
require.NotNil(t, pprofExt)
Expand Down
2 changes: 1 addition & 1 deletion extension/storage/dbstorage/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ func newTestExtension(t *testing.T) storage.Extension {
}

func newTestEntity(name string) component.ID {
return component.NewIDWithName("nop", name)
return component.MustNewIDWithName("nop", name)
}
2 changes: 1 addition & 1 deletion extension/storage/filestorage/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func newTestExtension(t *testing.T) storage.Extension {
}

func newTestEntity(name string) component.ID {
return component.NewIDWithName("nop", name)
return component.MustNewIDWithName("nop", name)
}

func TestCompaction(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions extension/storage/storagetest/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func runExtensionLifecycle(t *testing.T, ext *TestStorage, expectPersistence boo
ctx := context.Background()
require.NoError(t, ext.Start(ctx, componenttest.NewNopHost()))

clientOne, err := ext.GetClient(ctx, component.KindProcessor, component.NewID("foo"), "client_one")
clientOne, err := ext.GetClient(ctx, component.KindProcessor, component.MustNewID("foo"), "client_one")
require.NoError(t, err)

creatorID, err := CreatorID(ctx, clientOne)
Expand Down Expand Up @@ -65,7 +65,7 @@ func runExtensionLifecycle(t *testing.T, ext *TestStorage, expectPersistence boo
require.NoError(t, clientOne.Close(ctx))

// Create new client to test persistence
clientTwo, err := ext.GetClient(ctx, component.KindProcessor, component.NewID("foo"), "client_one")
clientTwo, err := ext.GetClient(ctx, component.KindProcessor, component.MustNewID("foo"), "client_one")
require.NoError(t, err)

creatorID, err = CreatorID(ctx, clientTwo)
Expand Down
6 changes: 3 additions & 3 deletions internal/aws/xray/telemetry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

func TestRegistry(t *testing.T) {
r := NewRegistry()
newID := component.NewID("new")
contribID := component.NewID("contrib")
notCreatedID := component.NewID("not-created")
newID := component.MustNewID("new")
contribID := component.MustNewID("contrib")
notCreatedID := component.MustNewID("not_created")
original := r.Register(
newID,
Config{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestNopRegistry(t *testing.T) {
assert.Same(t, nopRegistryInstance, NewNopRegistry())
r := NewNopRegistry()
assert.NotPanics(t, func() {
recorder := r.Register(component.NewID("a"), telemetry.Config{}, nil)
assert.Same(t, recorder, r.Load(component.NewID("b")))
r.LoadOrStore(component.NewID("c"), recorder)
r.LoadOrNop(component.NewID("d"))
recorder := r.Register(component.MustNewID("a"), telemetry.Config{}, nil)
assert.Same(t, recorder, r.Load(component.MustNewID("b")))
r.LoadOrStore(component.MustNewID("c"), recorder)
r.LoadOrNop(component.MustNewID("d"))
})
}
2 changes: 1 addition & 1 deletion internal/sharedcomponent/sharedcomponent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
)

var id = component.NewID("test")
var id = component.MustNewID("test")

func TestNewSharedComponents(t *testing.T) {
comps := NewSharedComponents()
Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/adapter/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func createNoopReceiver(nextConsumer consumer.Logs) (*receiver, error) {
return nil, err
}

receiverID := component.NewID("test")
receiverID := component.MustNewID("test")
obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: receiverID,
ReceiverCreateSettings: receivertest.NewNopCreateSettings(),
Expand All @@ -46,7 +46,7 @@ func createNoopReceiver(nextConsumer consumer.Logs) (*receiver, error) {
}

return &receiver{
id: component.NewID("testReceiver"),
id: component.MustNewID("testReceiver"),
pipe: pipe,
emitter: emitter,
consumer: nextConsumer,
Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/adapter/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func BenchmarkReadLine(b *testing.B) {

storageClient := storagetest.NewInMemoryClient(
component.KindReceiver,
component.NewID("foolog"),
component.MustNewID("foolog"),
"test",
)

Expand Down Expand Up @@ -216,7 +216,7 @@ func BenchmarkParseAndMap(b *testing.B) {

storageClient := storagetest.NewInMemoryClient(
component.KindReceiver,
component.NewID("foolog"),
component.MustNewID("foolog"),
"test",
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/adapter/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestFindCorrectStorageExtension(t *testing.T) {
}

func TestFailOnMissingStorageExtension(t *testing.T) {
id := component.NewIDWithName("test", "missing")
id := component.MustNewIDWithName("test", "missing")
r := createReceiver(t, id)
err := r.Start(context.Background(), storagetest.NewStorageHost())
require.Error(t, err)
Expand Down

0 comments on commit c395737

Please sign in to comment.