From fcedaef7af47f9e228b9db07d14b069db0616b2a Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Wed, 23 Nov 2022 09:16:38 -0800 Subject: [PATCH] Move service.[Collector|NewSvcHandler|CollectorSettings|State|NewCommand] to collector package Signed-off-by: Bogdan Drutu --- .chloggen/depcollector.yaml | 11 +++++ collector/collector.go | 37 ++++++++++++++++ collector/collector_windows.go | 25 +++++++++++ .../collector_windows_test.go | 4 +- collector/command.go | 22 ++++++++++ {service => collector}/command_test.go | 20 ++++++--- collector/config_provider.go | 42 +++++++++++++++++++ collector/settings.go | 22 ++++++++++ service/collector.go | 8 +++- service/collector_windows.go | 2 +- service/command.go | 2 +- service/config_provider.go | 20 ++------- service/servicetest/configprovider.go | 3 +- service/settings.go | 2 +- 14 files changed, 190 insertions(+), 30 deletions(-) create mode 100755 .chloggen/depcollector.yaml create mode 100644 collector/collector.go create mode 100644 collector/collector_windows.go rename {service => collector}/collector_windows_test.go (93%) create mode 100644 collector/command.go rename {service => collector}/command_test.go (62%) create mode 100644 collector/config_provider.go create mode 100644 collector/settings.go diff --git a/.chloggen/depcollector.yaml b/.chloggen/depcollector.yaml new file mode 100755 index 00000000000..d5b78ae3759 --- /dev/null +++ b/.chloggen/depcollector.yaml @@ -0,0 +1,11 @@ +# 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: service + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Deprecate service.[Collector|NewSvcHandler|CollectorSettings|State|NewCommand] in favor of collector package" + +# One or more tracking issues or pull requests related to the change +issues: [6608] diff --git a/collector/collector.go b/collector/collector.go new file mode 100644 index 00000000000..88a3e6e1363 --- /dev/null +++ b/collector/collector.go @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package collector handles the command-line, configuration, and runs the +// OpenTelemetry Collector. +package collector // import "go.opentelemetry.io/collector/collector" + +import ( + "go.opentelemetry.io/collector/service" +) + +// State defines Collector's state. +type State = service.State //nolint:staticcheck + +const ( + StateStarting = service.StateStarting //nolint:staticcheck + StateRunning = service.StateRunning //nolint:staticcheck + StateClosing = service.StateClosing //nolint:staticcheck + StateClosed = service.StateClosed //nolint:staticcheck +) + +// Collector represents a server providing the OpenTelemetry Collector service. +type Collector = service.Collector //nolint:staticcheck + +// New creates and returns a new instance of Collector. +var New = service.New //nolint:staticcheck diff --git a/collector/collector_windows.go b/collector/collector_windows.go new file mode 100644 index 00000000000..85c654088a2 --- /dev/null +++ b/collector/collector_windows.go @@ -0,0 +1,25 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build windows +// +build windows + +package collector // import "go.opentelemetry.io/collector/collector" + +import ( + "go.opentelemetry.io/collector/service" +) + +// NewSvcHandler constructs a new svc.Handler using the given CollectorSettings. +var NewSvcHandler = service.NewSvcHandler //nolint:staticcheck diff --git a/service/collector_windows_test.go b/collector/collector_windows_test.go similarity index 93% rename from service/collector_windows_test.go rename to collector/collector_windows_test.go index 4929a04add5..0c95bdb3ec4 100644 --- a/service/collector_windows_test.go +++ b/collector/collector_windows_test.go @@ -15,7 +15,7 @@ //go:build windows // +build windows -package service +package collector import ( "os" @@ -38,7 +38,7 @@ func TestNewSvcHandler(t *testing.T) { factories, err := componenttest.NopFactories() require.NoError(t, err) - s := NewSvcHandler(CollectorSettings{BuildInfo: component.NewDefaultBuildInfo(), Factories: factories}) + s := NewSvcHandler(Settings{BuildInfo: component.NewDefaultBuildInfo(), Factories: factories}) colDone := make(chan struct{}) requests := make(chan svc.ChangeRequest) diff --git a/collector/command.go b/collector/command.go new file mode 100644 index 00000000000..a6e89157f3b --- /dev/null +++ b/collector/command.go @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package collector // import "go.opentelemetry.io/collector/collector" + +import ( + "go.opentelemetry.io/collector/service" +) + +// NewCommand constructs a new cobra.Command using the given CollectorSettings. +var NewCommand = service.NewCommand //nolint:staticcheck diff --git a/service/command_test.go b/collector/command_test.go similarity index 62% rename from service/command_test.go rename to collector/command_test.go index df5b45ae914..7732cd05d54 100644 --- a/service/command_test.go +++ b/collector/command_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package service +package collector import ( "path/filepath" @@ -23,10 +23,13 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" + "go.opentelemetry.io/collector/confmap" + "go.opentelemetry.io/collector/confmap/converter/expandconverter" + "go.opentelemetry.io/collector/confmap/provider/fileprovider" ) func TestNewCommandVersion(t *testing.T) { - cmd := NewCommand(CollectorSettings{BuildInfo: component.BuildInfo{Version: "test_version"}}) + cmd := NewCommand(Settings{BuildInfo: component.BuildInfo{Version: "test_version"}}) assert.Equal(t, "test_version", cmd.Version) } @@ -34,7 +37,7 @@ func TestNewCommandNoConfigURI(t *testing.T) { factories, err := componenttest.NopFactories() require.NoError(t, err) - cmd := NewCommand(CollectorSettings{Factories: factories}) + cmd := NewCommand(Settings{Factories: factories}) require.Error(t, cmd.Execute()) } @@ -42,9 +45,16 @@ func TestNewCommandInvalidComponent(t *testing.T) { factories, err := componenttest.NopFactories() require.NoError(t, err) - cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-invalid.yaml")})) + cfgProvider, err := NewConfigProvider( + ConfigProviderSettings{ + ResolverSettings: confmap.ResolverSettings{ + URIs: []string{filepath.Join("testdata", "otelcol-invalid.yaml")}, + Providers: map[string]confmap.Provider{"file": fileprovider.New()}, + Converters: []confmap.Converter{expandconverter.New()}, + }, + }) require.NoError(t, err) - cmd := NewCommand(CollectorSettings{Factories: factories, ConfigProvider: cfgProvider}) + cmd := NewCommand(Settings{Factories: factories, ConfigProvider: cfgProvider}) require.Error(t, cmd.Execute()) } diff --git a/collector/config_provider.go b/collector/config_provider.go new file mode 100644 index 00000000000..24370412aed --- /dev/null +++ b/collector/config_provider.go @@ -0,0 +1,42 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package collector // import "go.opentelemetry.io/collector/collector" + +import ( + "go.opentelemetry.io/collector/service" +) + +// ConfigProvider provides the service configuration. +// +// The typical usage is the following: +// +// cfgProvider.Get(...) +// cfgProvider.Watch() // wait for an event. +// cfgProvider.Get(...) +// cfgProvider.Watch() // wait for an event. +// // repeat Get/Watch cycle until it is time to shut down the Collector process. +// cfgProvider.Shutdown() +type ConfigProvider = service.ConfigProvider //nolint:staticcheck + +// ConfigProviderSettings are the settings to configure the behavior of the ConfigProvider. +type ConfigProviderSettings = service.ConfigProviderSettings //nolint:staticcheck + +// 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. +var NewConfigProvider = service.NewConfigProvider //nolint:staticcheck diff --git a/collector/settings.go b/collector/settings.go new file mode 100644 index 00000000000..34e80f4a4da --- /dev/null +++ b/collector/settings.go @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package collector // import "go.opentelemetry.io/collector/collector" + +import ( + "go.opentelemetry.io/collector/service" +) + +// Settings holds configuration for creating a new Collector. +type Settings = service.CollectorSettings //nolint:staticcheck diff --git a/service/collector.go b/service/collector.go index 7e460871b6f..ff99b0db625 100644 --- a/service/collector.go +++ b/service/collector.go @@ -33,13 +33,17 @@ import ( "go.opentelemetry.io/collector/service/internal/grpclog" ) -// State defines Collector's state. +// Deprecated: [v0.66.0] use collector.State type State int const ( + // Deprecated: [v0.66.0] use collector.StateStarting StateStarting State = iota + // Deprecated: [v0.66.0] use collector.StateRunning StateRunning + // Deprecated: [v0.66.0] use collector.StateClosing StateClosing + // Deprecated: [v0.66.0] use collector.StateClosed StateClosed ) @@ -68,7 +72,7 @@ func (s State) String() string { // - Upon shutdown, pipelines are notified, then pipelines and extensions are shut down. // - Users can call (*Collector).Shutdown anytime to shut down the collector. -// Collector represents a server providing the OpenTelemetry Collector service. +// Deprecated: [v0.66.0] use collector.Collector type Collector struct { set CollectorSettings diff --git a/service/collector_windows.go b/service/collector_windows.go index f429b6c29c0..a3330f0f3d1 100644 --- a/service/collector_windows.go +++ b/service/collector_windows.go @@ -39,7 +39,7 @@ type windowsService struct { flags *flag.FlagSet } -// NewSvcHandler constructs a new svc.Handler using the given CollectorSettings. +// Deprecated: [v0.66.0] use collector.NewSvcHandler func NewSvcHandler(set CollectorSettings) svc.Handler { return &windowsService{settings: set, flags: flags()} } diff --git a/service/command.go b/service/command.go index ac2e24fc194..696f19dd0b5 100644 --- a/service/command.go +++ b/service/command.go @@ -22,7 +22,7 @@ import ( "go.opentelemetry.io/collector/featuregate" ) -// NewCommand constructs a new cobra.Command using the given CollectorSettings. +// Deprecated: [v0.66.0] use collector.NewCommand func NewCommand(set CollectorSettings) *cobra.Command { flagSet := flags() rootCmd := &cobra.Command{ diff --git a/service/config_provider.go b/service/config_provider.go index 3feba9741fe..cec9b28a7eb 100644 --- a/service/config_provider.go +++ b/service/config_provider.go @@ -27,16 +27,7 @@ import ( "go.opentelemetry.io/collector/confmap/provider/yamlprovider" ) -// ConfigProvider provides the service configuration. -// -// The typical usage is the following: -// -// cfgProvider.Get(...) -// cfgProvider.Watch() // wait for an event. -// cfgProvider.Get(...) -// cfgProvider.Watch() // wait for an event. -// // repeat Get/Watch cycle until it is time to shut down the Collector process. -// cfgProvider.Shutdown() +// Deprecated: [v0.66.0] use collector.ConfigProvider type ConfigProvider interface { // Get returns the service configuration, or error otherwise. // @@ -65,7 +56,7 @@ type configProvider struct { mapResolver *confmap.Resolver } -// ConfigProviderSettings are the settings to configure the behavior of the ConfigProvider. +// Deprecated: [v0.66.0] use collector.ConfigProviderSettings type ConfigProviderSettings struct { // ResolverSettings are the settings to configure the behavior of the confmap.Resolver. ResolverSettings confmap.ResolverSettings @@ -81,12 +72,7 @@ func newDefaultConfigProviderSettings(uris []string) ConfigProviderSettings { } } -// 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.66.0] use collector.NewConfigProvider func NewConfigProvider(set ConfigProviderSettings) (ConfigProvider, error) { mr, err := confmap.NewResolver(set.ResolverSettings) if err != nil { diff --git a/service/servicetest/configprovider.go b/service/servicetest/configprovider.go index 227458ea93b..d88775c26c5 100644 --- a/service/servicetest/configprovider.go +++ b/service/servicetest/configprovider.go @@ -17,6 +17,7 @@ package servicetest // import "go.opentelemetry.io/collector/service/servicetest import ( "context" + "go.opentelemetry.io/collector/collector" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap" "go.opentelemetry.io/collector/confmap/converter/expandconverter" @@ -30,7 +31,7 @@ import ( // LoadConfig loads a config.Config from file, and does NOT validate the configuration. func LoadConfig(fileName string, factories component.Factories) (*service.Config, error) { // Read yaml config from file - provider, err := service.NewConfigProvider(service.ConfigProviderSettings{ + provider, err := collector.NewConfigProvider(collector.ConfigProviderSettings{ ResolverSettings: confmap.ResolverSettings{ URIs: []string{fileName}, Providers: makeMapProvidersMap(fileprovider.New(), envprovider.New(), yamlprovider.New(), httpprovider.New()), diff --git a/service/settings.go b/service/settings.go index 070d6911b0c..64f9194a8a4 100644 --- a/service/settings.go +++ b/service/settings.go @@ -41,7 +41,7 @@ type settings struct { telemetry *telemetryInitializer } -// CollectorSettings holds configuration for creating a new Collector. +// Deprecated: [v0.66.0] use collector.Settings type CollectorSettings struct { // Factories component factories. Factories component.Factories