Skip to content

Commit

Permalink
[extension] Add ModuleInfo to extension.Settings (#10888)
Browse files Browse the repository at this point in the history
#### Description
Paired with @djaglowski on adding a new struct, `ModuleInfo`, to
`extension.Settings`. The goal is to make the collector's component go
modules available to extensions that may want to use that information
(in particular the OpAMP extension).

We didn't want to modify the `extension` package but adding this
information to the Settings struct seems to be the most painless way to
make this information available. No function signatures need to be
updated and existing extensions can just ignore the new field on the
Settings struct unless they want the information.

#### Link to tracking issue
Fixes #10876 

#### Testing
Updated existing tests.

---------

Co-authored-by: Dan Jaglowski <[email protected]>
  • Loading branch information
dpaasman00 and djaglowski authored Aug 21, 2024
1 parent 98fb888 commit 6764622
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 9 deletions.
25 changes: 25 additions & 0 deletions .chloggen/module-info-extensions.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: enhancement

# 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: "Add ModuleInfo to extension.Settings to allow extensions to access component go module information."

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

# (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: []
12 changes: 12 additions & 0 deletions extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ type ConfigWatcher interface {
NotifyConfig(ctx context.Context, conf *confmap.Conf) error
}

// ModuleInfo describes the go module for each component.
type ModuleInfo struct {
Receiver map[component.Type]string
Processor map[component.Type]string
Exporter map[component.Type]string
Extension map[component.Type]string
Connector map[component.Type]string
}

// Settings is passed to Factory.Create(...) function.
type Settings struct {
// ID returns the ID of the component that will be created.
Expand All @@ -60,6 +69,9 @@ type Settings struct {

// BuildInfo can be used by components for informational purposes
BuildInfo component.BuildInfo

// ModuleInfo describes the go module for each component.
ModuleInfo ModuleInfo
}

// CreateFunc is the equivalent of Factory.Create(...) function.
Expand Down
13 changes: 12 additions & 1 deletion extension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ func TestBuilder(t *testing.T) {
cfgs := map[component.ID]component.Config{testID: defaultCfg, unknownID: defaultCfg}
b := NewBuilder(cfgs, factories)

e, err := b.Create(context.Background(), createSettings(testID))
testIDSettings := createSettings(testID)
testIDModuleInfo := ModuleInfo{
Extension: map[component.Type]string{
testType: "go.opentelemetry.io/collector/extension/extensiontest v1.2.3",
},
}
testIDSettings.ModuleInfo = testIDModuleInfo

e, err := b.Create(context.Background(), testIDSettings)
assert.NoError(t, err)
assert.NotNil(t, e)

Expand All @@ -107,6 +115,9 @@ func TestBuilder(t *testing.T) {
assert.True(t, ok)
assert.Equal(t, nop.Settings.Resource.Attributes().Len(), 0)

// Check that the extension has access to the module info.
assert.Equal(t, testIDModuleInfo, nop.ModuleInfo)

missingType, err := b.Create(context.Background(), createSettings(unknownID))
assert.EqualError(t, err, "extension factory not available for: \"unknown\"")
assert.Nil(t, missingType)
Expand Down
21 changes: 14 additions & 7 deletions otelcol/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,20 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
}

col.service, err = service.New(ctx, service.Settings{
BuildInfo: col.set.BuildInfo,
CollectorConf: conf,
Receivers: receiver.NewBuilder(cfg.Receivers, factories.Receivers),
Processors: processor.NewBuilder(cfg.Processors, factories.Processors),
Exporters: exporter.NewBuilder(cfg.Exporters, factories.Exporters),
Connectors: connector.NewBuilder(cfg.Connectors, factories.Connectors),
Extensions: extension.NewBuilder(cfg.Extensions, factories.Extensions),
BuildInfo: col.set.BuildInfo,
CollectorConf: conf,
Receivers: receiver.NewBuilder(cfg.Receivers, factories.Receivers),
Processors: processor.NewBuilder(cfg.Processors, factories.Processors),
Exporters: exporter.NewBuilder(cfg.Exporters, factories.Exporters),
Connectors: connector.NewBuilder(cfg.Connectors, factories.Connectors),
Extensions: extension.NewBuilder(cfg.Extensions, factories.Extensions),
ModuleInfo: extension.ModuleInfo{
Receiver: factories.ReceiverModules,
Processor: factories.ProcessorModules,
Exporter: factories.ExporterModules,
Extension: factories.ExtensionModules,
Connector: factories.ConnectorModules,
},
AsyncErrorChannel: col.asyncErrorChannel,
LoggingOptions: col.set.LoggingOptions,
}, cfg.Service)
Expand Down
2 changes: 2 additions & 0 deletions service/extensions/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ type Settings struct {

// Extensions builder for extensions.
Extensions *extension.Builder
ModuleInfo extension.ModuleInfo
}

type Option func(*Extensions)
Expand Down Expand Up @@ -203,6 +204,7 @@ func New(ctx context.Context, set Settings, cfg Config, options ...Option) (*Ext
ID: extID,
TelemetrySettings: set.Telemetry,
BuildInfo: set.BuildInfo,
ModuleInfo: set.ModuleInfo,
}
extSet.TelemetrySettings.Logger = components.ExtensionLogger(set.Telemetry.Logger, extID)

Expand Down
3 changes: 2 additions & 1 deletion service/internal/graph/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type Host struct {
Connectors *connector.Builder
Extensions *extension.Builder

BuildInfo component.BuildInfo
ModuleInfo extension.ModuleInfo
BuildInfo component.BuildInfo

Pipelines *Graph
ServiceExtensions *extensions.Extensions
Expand Down
5 changes: 5 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type Settings struct {
// Extensions builder for extensions.
Extensions *extension.Builder

// ModuleInfo describes the go module for each component.
ModuleInfo extension.ModuleInfo

// AsyncErrorChannel is the channel that is used to report fatal errors.
AsyncErrorChannel chan error

Expand Down Expand Up @@ -86,6 +89,7 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) {
Exporters: set.Exporters,
Connectors: set.Connectors,
Extensions: set.Extensions,
ModuleInfo: set.ModuleInfo,
BuildInfo: set.BuildInfo,
AsyncErrorChannel: set.AsyncErrorChannel,
},
Expand Down Expand Up @@ -288,6 +292,7 @@ func (srv *Service) initExtensions(ctx context.Context, cfg extensions.Config) e
Telemetry: srv.telemetrySettings,
BuildInfo: srv.buildInfo,
Extensions: srv.host.Extensions,
ModuleInfo: srv.host.ModuleInfo,
}
if srv.host.ServiceExtensions, err = extensions.New(ctx, extensionsSettings, cfg, extensions.WithReporter(srv.host.Reporter)); err != nil {
return fmt.Errorf("failed to build extensions: %w", err)
Expand Down

0 comments on commit 6764622

Please sign in to comment.