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

[extension] Error out if passed extension.Settings has incorrect type #12305

Merged
merged 8 commits into from
Feb 7, 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: 25 additions & 0 deletions .chloggen/mx-psi_extensiontest-withtype.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: extensiontest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate `extensiontest.NewNopSettings` in favor of `extensiontest.NewNopSettingsWithType`

# One or more tracking issues or pull requests related to the change
issues: [12305]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_type-mismatch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: extension

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Explicitly error out at extension creation time if there is a type mismatch.

# One or more tracking issues or pull requests related to the change
issues: [12305]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions cmd/mdatagen/internal/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ import (
{{- end }}
)

var typ = component.MustNewType("{{ .Type }}")

func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "{{ .Type }}", NewFactory().Type().String())
require.Equal(t, typ, NewFactory().Type())
}

func TestComponentConfigStruct(t *testing.T) {
Expand Down Expand Up @@ -412,7 +414,7 @@ func TestComponentLifecycle(t *testing.T) {

{{- if not .Tests.SkipShutdown }}
t.Run("shutdown", func(t *testing.T) {
e, err := factory.Create(context.Background(), extensiontest.NewNopSettings(), cfg)
e, err := factory.Create(context.Background(), extensiontest.NewNopSettingsWithType(typ), cfg)
require.NoError(t, err)
err = e.Shutdown(context.Background())
require.NoError(t, err)
Expand All @@ -421,12 +423,12 @@ func TestComponentLifecycle(t *testing.T) {

{{- if not .Tests.SkipLifecycle }}
t.Run("lifecycle", func(t *testing.T) {
firstExt, err := factory.Create(context.Background(), extensiontest.NewNopSettings(), cfg)
firstExt, err := factory.Create(context.Background(), extensiontest.NewNopSettingsWithType(typ), cfg)
require.NoError(t, err)
require.NoError(t, firstExt.Start(context.Background(), {{ .Tests.Host }}))
require.NoError(t, firstExt.Shutdown(context.Background()))

secondExt, err := factory.Create(context.Background(), extensiontest.NewNopSettings(), cfg)
secondExt, err := factory.Create(context.Background(), extensiontest.NewNopSettingsWithType(typ), cfg)
require.NoError(t, err)
require.NoError(t, secondExt.Start(context.Background(), {{ .Tests.Host }}))
require.NoError(t, secondExt.Shutdown(context.Background()))
Expand Down
4 changes: 3 additions & 1 deletion connector/forwardconnector/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion exporter/debugexporter/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion exporter/nopexporter/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion exporter/otlpexporter/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion exporter/otlphttpexporter/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Settings struct {
type CreateFunc func(context.Context, Settings, component.Config) (Extension, error)

// Create implements Factory.Create.
// Deprecated: [v0.120.0] No longer used, will be removed.
func (f CreateFunc) Create(ctx context.Context, set Settings, cfg component.Config) (Extension, error) {
return f(ctx, set, cfg)
}
Expand All @@ -51,7 +52,7 @@ type Factory interface {
type factory struct {
cfgType component.Type
component.CreateDefaultConfigFunc
CreateFunc
createFunc CreateFunc
extensionStability component.StabilityLevel
}

Expand All @@ -65,6 +66,14 @@ func (f *factory) Stability() component.StabilityLevel {
return f.extensionStability
}

func (f *factory) Create(ctx context.Context, set Settings, cfg component.Config) (Extension, error) {
if set.ID.Type() != f.cfgType {
return nil, fmt.Errorf("component type mismatch: component ID %q does not have type %q", set.ID, f.cfgType)
}

return f.createFunc(ctx, set, cfg)
}

// NewFactory returns a new Factory based on this configuration.
func NewFactory(
cfgType component.Type,
Expand All @@ -75,7 +84,7 @@ func NewFactory(
return &factory{
cfgType: cfgType,
CreateDefaultConfigFunc: createDefaultConfig,
CreateFunc: createServiceExtension,
createFunc: createServiceExtension,
extensionStability: sl,
}
}
Expand Down
5 changes: 4 additions & 1 deletion extension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func TestNewFactory(t *testing.T) {
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())

assert.Equal(t, component.StabilityLevelDevelopment, factory.Stability())
ext, err := factory.Create(context.Background(), Settings{}, &defaultCfg)
ext, err := factory.Create(context.Background(), Settings{ID: component.NewID(testType)}, &defaultCfg)
require.NoError(t, err)
assert.Same(t, nopExtensionInstance, ext)

_, err = factory.Create(context.Background(), Settings{ID: component.NewID(component.MustNewType("mismatch"))}, &defaultCfg)
require.Error(t, err)
}

func TestMakeFactoryMap(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions extension/extensiontest/nop_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ import (
"go.opentelemetry.io/collector/extension"
)

var nopType = component.MustNewType("nop")
// NopType is the type of the nop extension.
var NopType = component.MustNewType("nop")

// NewNopSettings returns a new nop settings for extension.Factory Create* functions.
// Deprecated: [v0.120.0] Use NewNopSettingsWithType(NopType) instead.
func NewNopSettings() extension.Settings {
return NewNopSettingsWithType(NopType)
}

// NewNopSettings returns a new nop settings for extension.Factory Create* functions with the given type.
func NewNopSettingsWithType(ty component.Type) extension.Settings {
return extension.Settings{
ID: component.NewIDWithName(nopType, uuid.NewString()),
ID: component.NewIDWithName(ty, uuid.NewString()),
TelemetrySettings: componenttest.NewNopTelemetrySettings(),
BuildInfo: component.NewDefaultBuildInfo(),
}
Expand All @@ -27,7 +34,7 @@ func NewNopSettings() extension.Settings {
// NewNopFactory returns an extension.Factory that constructs nop extensions.
func NewNopFactory() extension.Factory {
return extension.NewFactory(
nopType,
NopType,
func() component.Config {
return &nopConfig{}
},
Expand Down
6 changes: 5 additions & 1 deletion extension/memorylimiterextension/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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/extension/extensiontest"
"go.opentelemetry.io/collector/internal/memorylimiter"
Expand All @@ -37,7 +38,10 @@ func TestCreate(t *testing.T) {
pCfg.MemorySpikeLimitMiB = 1907
pCfg.CheckInterval = 100 * time.Millisecond

tp, err := factory.Create(context.Background(), extensiontest.NewNopSettings(), cfg)
set := extensiontest.NewNopSettings()
set.ID = component.NewID(factory.Type())
Comment on lines +41 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we switch extensiontest.NewNopSettings() to accept a type? Also we can generate a helper like metadatatest.NewNopSettings for components that calls the extensiontest but passes the type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added extensiontest.NewNopSettingsWithType, PTAL!


tp, err := factory.Create(context.Background(), set, cfg)
require.NoError(t, err)
assert.NotNil(t, tp)
// test if we can shutdown a monitoring routine that has not started
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion extension/zpagesextension/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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/config/confighttp"
"go.opentelemetry.io/collector/extension/extensiontest"
Expand All @@ -35,7 +36,9 @@ func TestFactoryCreate(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.ServerConfig.Endpoint = testutil.GetAvailableLocalAddress(t)

ext, err := create(context.Background(), extensiontest.NewNopSettings(), cfg)
set := extensiontest.NewNopSettings()
set.ID = component.NewID(NewFactory().Type())
ext, err := create(context.Background(), set, cfg)
require.NoError(t, err)
require.NotNil(t, ext)
}
11 changes: 7 additions & 4 deletions extension/zpagesextension/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion processor/batchprocessor/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading