Skip to content

Commit

Permalink
Allow passing confmap.Providers to the Collector
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-bradley committed Jan 18, 2024
1 parent 2370481 commit adae484
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 105 deletions.
25 changes: 25 additions & 0 deletions .chloggen/pass-confmap-providers.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: otelcol

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `ConfigProviderSettings` to `CollectorSettings`

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

# (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: This allows passing a custom list of `confmap.Provider`s to `otelcol.NewCommand`.

# 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: []
36 changes: 28 additions & 8 deletions otelcol/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package otelcol // import "go.opentelemetry.io/collector/otelcol"

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -66,10 +65,16 @@ type CollectorSettings struct {
// and manually handle the signals to shutdown the collector.
DisableGracefulShutdown bool

// Deprecated: [v0.93.0] Use ConfigProviderSettings instead.
// ConfigProvider provides the service configuration.
// If the provider watches for configuration change, collector may reload the new configuration upon changes.
ConfigProvider ConfigProvider

// ConfigProviderSettings allows configuring the way the Collector retrieves its configuration
// The Collector will reload based on configuration changes from the ConfigProvider if any
// confmap.Providers watch for configuration changes.
ConfigProviderSettings ConfigProviderSettings

// LoggingOptions provides a way to change behavior of zap logging.
LoggingOptions []zap.Option

Expand All @@ -92,6 +97,8 @@ type CollectorSettings struct {
type Collector struct {
set CollectorSettings

configProvider ConfigProvider

service *service.Service
state *atomic.Int32

Expand All @@ -105,8 +112,20 @@ type Collector struct {

// NewCollector creates and returns a new instance of Collector.
func NewCollector(set CollectorSettings) (*Collector, error) {
if set.ConfigProvider == nil {
return nil, errors.New("invalid nil config provider")
var err error
configProvider := set.ConfigProvider

if configProvider == nil {
resolverSet := set.ConfigProviderSettings.ResolverSettings
// Provide a default set of providers and converters if none have been specified.
if len(resolverSet.Providers) == 0 && len(resolverSet.Converters) == 0 {
set.ConfigProviderSettings = newDefaultConfigProviderSettings(resolverSet.URIs)
}

configProvider, err = NewConfigProvider(set.ConfigProviderSettings)
if err != nil {
return nil, err
}
}

state := &atomic.Int32{}
Expand All @@ -119,6 +138,7 @@ func NewCollector(set CollectorSettings) (*Collector, error) {
// the number of signals getting notified on is recommended.
signalsChannel: make(chan os.Signal, 3),
asyncErrorChannel: make(chan error),
configProvider: configProvider,
}, nil
}

Expand Down Expand Up @@ -146,7 +166,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {

var conf *confmap.Conf

if cp, ok := col.set.ConfigProvider.(ConfmapProvider); ok {
if cp, ok := col.configProvider.(ConfmapProvider); ok {
var err error
conf, err = cp.GetConfmap(ctx)

Expand All @@ -159,7 +179,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to initialize factories: %w", err)
}
cfg, err := col.set.ConfigProvider.Get(ctx, factories)
cfg, err := col.configProvider.Get(ctx, factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}
Expand Down Expand Up @@ -215,7 +235,7 @@ func (col *Collector) DryRun(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to initialize factories: %w", err)
}
cfg, err := col.set.ConfigProvider.Get(ctx, factories)
cfg, err := col.configProvider.Get(ctx, factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}
Expand Down Expand Up @@ -243,7 +263,7 @@ func (col *Collector) Run(ctx context.Context) error {
LOOP:
for {
select {
case err := <-col.set.ConfigProvider.Watch():
case err := <-col.configProvider.Watch():
if err != nil {
col.service.Logger().Error("Config watch failed", zap.Error(err))
break LOOP
Expand Down Expand Up @@ -280,7 +300,7 @@ func (col *Collector) shutdown(ctx context.Context) error {
// Accumulate errors and proceed with shutting down remaining components.
var errs error

if err := col.set.ConfigProvider.Shutdown(ctx); err != nil {
if err := col.configProvider.Shutdown(ctx); err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown config provider: %w", err))
}

Expand Down
Loading

0 comments on commit adae484

Please sign in to comment.