Skip to content

Commit

Permalink
Move service.[Collector|NewSvcHandler|CollectorSettings|State|NewComm…
Browse files Browse the repository at this point in the history
…and] to collector package

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Nov 23, 2022
1 parent 5b004ba commit a9d4c0b
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 50 deletions.
11 changes: 11 additions & 0 deletions .chloggen/depcollector.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: 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]
8 changes: 4 additions & 4 deletions cmd/builder/internal/builder/templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package main
import (
"log"

"go.opentelemetry.io/collector/collector"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/service"
)

func main() {
Expand All @@ -22,13 +22,13 @@ func main() {
Version: "{{ .Distribution.Version }}",
}

if err := run(service.CollectorSettings{BuildInfo: info, Factories: factories}); err != nil {
if err := run(collector.Settings{BuildInfo: info, Factories: factories}); err != nil {
log.Fatal(err)
}
}

func runInteractive(params service.CollectorSettings) error {
cmd := service.NewCommand(params)
func runInteractive(params collector.Settings) error {
cmd := collector.NewCommand(params)
if err := cmd.Execute(); err != nil {
log.Fatalf("collector server run finished with error: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/builder/internal/builder/templates/main_others.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package main

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

func run(params service.CollectorSettings) error {
func run(params collector.Settings) error {
return runInteractive(params)
}
8 changes: 4 additions & 4 deletions cmd/builder/internal/builder/templates/main_windows.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

"golang.org/x/sys/windows/svc"

"go.opentelemetry.io/collector/service"
"go.opentelemetry.io/collector/collector"
)

func run(params service.CollectorSettings) error {
func run(params collector.Settings) error {
if useInteractiveMode, err := checkUseInteractiveMode(); err != nil {
return err
} else if useInteractiveMode {
Expand All @@ -40,9 +40,9 @@ func checkUseInteractiveMode() (bool, error) {
return isInteractiveSession, nil
}

func runService(params service.CollectorSettings) error {
func runService(params collector.Settings) error {
// do not need to supply service name when startup is invoked through Service Control Manager directly
if err := svc.Run("", service.NewSvcHandler(params)); err != nil {
if err := svc.Run("", collector.NewSvcHandler(params)); err != nil {
return fmt.Errorf("failed to start collector server: %w", err)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/otelcorecol/main.go

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

4 changes: 2 additions & 2 deletions cmd/otelcorecol/main_others.go

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

8 changes: 4 additions & 4 deletions cmd/otelcorecol/main_windows.go

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

37 changes: 37 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions collector/collector_windows.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//go:build windows
// +build windows

package service
package collector

import (
"os"
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions collector/command.go
Original file line number Diff line number Diff line change
@@ -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
20 changes: 15 additions & 5 deletions service/command_test.go → collector/command_test.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
package collector

import (
"path/filepath"
Expand All @@ -23,28 +23,38 @@ 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)
}

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())
}

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())
}
42 changes: 42 additions & 0 deletions collector/config_provider.go
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions collector/settings.go
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion service/collector_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
Expand Down
Loading

0 comments on commit a9d4c0b

Please sign in to comment.