Skip to content

Commit

Permalink
Remove deprecated service.[Collector|New|CollectorSettings|ConfigProv…
Browse files Browse the repository at this point in the history
…ider]

Updates open-telemetry#5564

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Dec 13, 2022
1 parent 489e478 commit fb1ee9b
Show file tree
Hide file tree
Showing 28 changed files with 202 additions and 316 deletions.
11 changes: 11 additions & 0 deletions .chloggen/rmdepservice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove deprecated `service.[Collector|New|CollectorSettings|ConfigProvider]`

# One or more tracking issues or pull requests related to the change
issues: [5564]
60 changes: 40 additions & 20 deletions service/collector.go → otelcol/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Package service handles the command-line, configuration, and runs the
// OpenTelemetry Collector.
package service // import "go.opentelemetry.io/collector/service"
package otelcol // import "go.opentelemetry.io/collector/otelcol"

import (
"context"
Expand All @@ -28,21 +28,18 @@ import (
"go.uber.org/multierr"
"go.uber.org/zap"

"go.opentelemetry.io/collector/service/internal/grpclog"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/otelcol/internal/grpclog"
"go.opentelemetry.io/collector/service"
)

// State defines Collector's state.
// Deprecated: [v0.67.0] use otelcol.State
type State int

const (
// Deprecated: [v0.67.0] use otelcol.StateStarting
StateStarting State = iota
// Deprecated: [v0.67.0] use otelcol.StateRunning
StateRunning
// Deprecated: [v0.67.0] use otelcol.StateClosing
StateClosing
// Deprecated: [v0.67.0] use otelcol.StateClosed
StateClosed
)

Expand All @@ -60,6 +57,31 @@ func (s State) String() string {
return "UNKNOWN"
}

// CollectorSettings holds configuration for creating a new Collector.
type CollectorSettings struct {
// Factories component factories.
Factories component.Factories

// BuildInfo provides collector start information.
BuildInfo component.BuildInfo

// DisableGracefulShutdown disables the automatic graceful shutdown
// of the collector on SIGINT or SIGTERM.
// Users who want to handle signals themselves can disable this behavior
// and manually handle the signals to shutdown the collector.
DisableGracefulShutdown bool

// ConfigProvider provides the service configuration.
// If the provider watches for configuration change, collector may reload the new configuration upon changes.
ConfigProvider ConfigProvider

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

// SkipSettingGRPCLogger avoids setting the grpc logger
SkipSettingGRPCLogger bool
}

// (Internal note) Collector Lifecycle:
// - New constructs a new Collector.
// - Run starts the collector.
Expand All @@ -72,11 +94,10 @@ func (s State) String() string {
// - Users can call (*Collector).Shutdown anytime to shut down the collector.

// Collector represents a server providing the OpenTelemetry Collector service.
// Deprecated: [v0.67.0] use otelcol.Collector
type Collector struct {
set CollectorSettings

service *service
service *service.Service
state *atomic.Int32

// shutdownChan is used to terminate the collector.
Expand All @@ -87,9 +108,8 @@ type Collector struct {
asyncErrorChannel chan error
}

// New creates and returns a new instance of Collector.
// Deprecated: [v0.67.0] use otelcol.NewCollector
func New(set CollectorSettings) (*Collector, error) {
// 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")
}
Expand Down Expand Up @@ -136,7 +156,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
return fmt.Errorf("invalid configuration: %w", err)
}

col.service, err = newService(&settings{
col.service, err = service.New(service.Settings{
BuildInfo: col.set.BuildInfo,
Factories: col.set.Factories,
Config: cfg,
Expand All @@ -148,7 +168,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
}

if !col.set.SkipSettingGRPCLogger {
grpclog.SetLogger(col.service.telemetrySettings.Logger, cfg.Service.Telemetry.Logs.Level)
grpclog.SetLogger(col.service.Logger(), cfg.Service.Telemetry.Logs.Level)
}

if err = col.service.Start(ctx); err != nil {
Expand All @@ -159,7 +179,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
}

func (col *Collector) reloadConfiguration(ctx context.Context) error {
col.service.telemetrySettings.Logger.Warn("Config updated, restart service")
col.service.Logger().Warn("Config updated, restart service")
col.setCollectorState(StateClosing)

if err := col.service.Shutdown(ctx); err != nil {
Expand Down Expand Up @@ -195,28 +215,28 @@ LOOP:
select {
case err := <-col.set.ConfigProvider.Watch():
if err != nil {
col.service.telemetrySettings.Logger.Error("Config watch failed", zap.Error(err))
col.service.Logger().Error("Config watch failed", zap.Error(err))
break LOOP
}
if err = col.reloadConfiguration(ctx); err != nil {
return err
}
case err := <-col.asyncErrorChannel:
col.service.telemetrySettings.Logger.Error("Asynchronous error received, terminating process", zap.Error(err))
col.service.Logger().Error("Asynchronous error received, terminating process", zap.Error(err))
break LOOP
case s := <-col.signalsChannel:
col.service.telemetrySettings.Logger.Info("Received signal from OS", zap.String("signal", s.String()))
col.service.Logger().Info("Received signal from OS", zap.String("signal", s.String()))
if s != syscall.SIGHUP {
break LOOP
}
if err := col.reloadConfiguration(ctx); err != nil {
return err
}
case <-col.shutdownChan:
col.service.telemetrySettings.Logger.Info("Received shutdown request")
col.service.Logger().Info("Received shutdown request")
break LOOP
case <-ctx.Done():
col.service.telemetrySettings.Logger.Info("Context done, terminating process", zap.Error(ctx.Err()))
col.service.Logger().Info("Context done, terminating process", zap.Error(ctx.Err()))
// Call shutdown with background context as the passed in context has been canceled
return col.shutdown(context.Background())
}
Expand Down
26 changes: 13 additions & 13 deletions service/collector_test.go → otelcol/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Package collector handles the command-line, configuration, and runs the OC collector.
package service
package otelcol

import (
"context"
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestCollectorStartAsGoRoutine(t *testing.T) {
Factories: factories,
ConfigProvider: cfgProvider,
}
col, err := New(set)
col, err := NewCollector(set)
require.NoError(t, err)

wg := startCollector(context.Background(), t, col)
Expand All @@ -78,7 +78,7 @@ func TestCollectorCancelContext(t *testing.T) {
Factories: factories,
ConfigProvider: cfgProvider,
}
col, err := New(set)
col, err := NewCollector(set)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestCollectorStateAfterConfigChange(t *testing.T) {
require.NoError(t, err)

watcher := make(chan error, 1)
col, err := New(CollectorSettings{
col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: &mockCfgProvider{ConfigProvider: provider, watcher: watcher},
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestCollectorReportError(t *testing.T) {
cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

col, err := New(CollectorSettings{
col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: cfgProvider,
Expand All @@ -155,7 +155,7 @@ func TestCollectorReportError(t *testing.T) {
return StateRunning == col.GetState()
}, 2*time.Second, 200*time.Millisecond)

col.service.host.ReportFatalError(errors.New("err2"))
col.asyncErrorChannel <- errors.New("err2")

wg.Wait()
assert.Equal(t, StateClosed, col.GetState())
Expand All @@ -168,7 +168,7 @@ func TestCollectorSendSignal(t *testing.T) {
cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

col, err := New(CollectorSettings{
col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: cfgProvider,
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestCollectorFailedShutdown(t *testing.T) {
cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)

col, err := New(CollectorSettings{
col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: cfgProvider,
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestCollectorStartInvalidConfig(t *testing.T) {
cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-invalid.yaml")}))
require.NoError(t, err)

col, err := New(CollectorSettings{
col, err := NewCollector(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: cfgProvider,
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestCollectorStartWithTraceContextPropagation(t *testing.T) {
ConfigProvider: cfgProvider,
}

col, err := New(set)
col, err := NewCollector(set)
require.NoError(t, err)

if tt.errExpected {
Expand Down Expand Up @@ -302,7 +302,7 @@ func TestCollectorRun(t *testing.T) {
Factories: factories,
ConfigProvider: cfgProvider,
}
col, err := New(set)
col, err := NewCollector(set)
require.NoError(t, err)

wg := startCollector(context.Background(), t, col)
Expand All @@ -326,7 +326,7 @@ func TestCollectorShutdownBeforeRun(t *testing.T) {
Factories: factories,
ConfigProvider: cfgProvider,
}
col, err := New(set)
col, err := NewCollector(set)
require.NoError(t, err)

// Calling shutdown before collector is running should cause it to return quickly
Expand All @@ -352,7 +352,7 @@ func TestCollectorClosedStateOnStartUpError(t *testing.T) {
Factories: factories,
ConfigProvider: cfgProvider,
}
col, err := New(set)
col, err := NewCollector(set)
require.NoError(t, err)

// Expect run to error
Expand Down
26 changes: 11 additions & 15 deletions service/config_provider.go → otelcol/configprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package service // import "go.opentelemetry.io/collector/service"
package otelcol // import "go.opentelemetry.io/collector/otelcol"

import (
"context"
Expand All @@ -37,8 +37,6 @@ import (
// cfgProvider.Watch() // wait for an event.
// // repeat Get/Watch cycle until it is time to shut down the Collector process.
// cfgProvider.Shutdown()
//
// Deprecated: [v0.67.0] use otelcol.ConfigProvider
type ConfigProvider interface {
// Get returns the service configuration, or error otherwise.
//
Expand Down Expand Up @@ -68,29 +66,17 @@ type configProvider struct {
}

// ConfigProviderSettings are the settings to configure the behavior of the ConfigProvider.
// Deprecated: [v0.67.0] use otelcol.ConfigProviderSettings
type ConfigProviderSettings struct {
// ResolverSettings are the settings to configure the behavior of the confmap.Resolver.
ResolverSettings confmap.ResolverSettings
}

func newDefaultConfigProviderSettings(uris []string) ConfigProviderSettings {
return ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: uris,
Providers: makeMapProvidersMap(fileprovider.New(), envprovider.New(), yamlprovider.New(), httpprovider.New()),
Converters: []confmap.Converter{expandconverter.New()},
},
}
}

// NewConfigProvider returns a new ConfigProvider that provides the service configuration:
// * Initially it resolves the "configuration map":
// - Retrieve the confmap.Conf by merging all retrieved maps from the given `locations` in order.
// - Then applies all the confmap.Converter in the given order.
//
// * Then unmarshalls the confmap.Conf into the service Config.
// Deprecated: [v0.67.0] use otelcol.NewConfigProvider
func NewConfigProvider(set ConfigProviderSettings) (ConfigProvider, error) {
mr, err := confmap.NewResolver(set.ResolverSettings)
if err != nil {
Expand Down Expand Up @@ -130,6 +116,16 @@ func (cm *configProvider) Shutdown(ctx context.Context) error {
return cm.mapResolver.Shutdown(ctx)
}

func newDefaultConfigProviderSettings(uris []string) ConfigProviderSettings {
return ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: uris,
Providers: makeMapProvidersMap(fileprovider.New(), envprovider.New(), yamlprovider.New(), httpprovider.New()),
Converters: []confmap.Converter{expandconverter.New()},
},
}
}

func makeMapProvidersMap(providers ...confmap.Provider) map[string]confmap.Provider {
ret := make(map[string]confmap.Provider, len(providers))
for _, provider := range providers {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package configunmarshaler // import "go.opentelemetry.io/collector/service/internal/configunmarshaler"
package configunmarshaler // import "go.opentelemetry.io/collector/otelcol/internal/configunmarshaler"

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package configunmarshaler // import "go.opentelemetry.io/collector/service/internal/configunmarshaler"
package configunmarshaler // import "go.opentelemetry.io/collector/otelcol/internal/configunmarshaler"

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package configunmarshaler // import "go.opentelemetry.io/collector/service/internal/configunmarshaler"
package configunmarshaler // import "go.opentelemetry.io/collector/otelcol/internal/configunmarshaler"

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package configunmarshaler // import "go.opentelemetry.io/collector/service/internal/configunmarshaler"
package configunmarshaler // import "go.opentelemetry.io/collector/otelcol/internal/configunmarshaler"

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package configunmarshaler // import "go.opentelemetry.io/collector/service/internal/configunmarshaler"
package configunmarshaler // import "go.opentelemetry.io/collector/otelcol/internal/configunmarshaler"

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package grpclog // import "go.opentelemetry.io/collector/service/internal/grpclog"
package grpclog // import "go.opentelemetry.io/collector/otelcol/internal/grpclog"

import (
"go.uber.org/zap"
Expand Down
File renamed without changes.
Loading

0 comments on commit fb1ee9b

Please sign in to comment.