Skip to content

Commit 41734c5

Browse files
pmatyjasek-sumoBogdan Drutu
and
Bogdan Drutu
authored
Standarize Settings, Params and Parameters (#3163)
* Standarize Settings, Params and Parameters Replace Parameters and settings structs with new struct Settings Replace dependencies in service and main Update tests Signed-off-by: Patryk Matyjasek <[email protected]> # Conflicts: # service/application_windows.go * Fix unnecessary file * Rework Settings structure to fit all requirements. Split it into ServiceSettings and ApplicationSettings. Signed-off-by: Patryk Matyjasek <[email protected]> # Conflicts: # CHANGELOG.md # service/application_windows.go * Fix linter issues: - Fix import orders - Rename stutter structs Signed-off-by: Patryk Matyjasek <[email protected]> # Conflicts: # CHANGELOG.md # service/application_windows.go * Remove CommonSettings struct, Make SvcSettings private Signed-off-by: Patryk Matyjasek <[email protected]> * Remove unused Settings struct. Fix Windows tests Signed-off-by: Patryk Matyjasek <[email protected]> * Update CHANGELOG.md Co-authored-by: Bogdan Drutu <[email protected]>
1 parent 51f8264 commit 41734c5

12 files changed

+111
-81
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 🛑 Breaking changes 🛑
66

77
- Remove unused logstest package (#3222)
8+
- Introduce `AppSettings` instead of `Parameters` (#3163)
89

910
## v0.27.0 Beta
1011

cmd/otelcol/main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,19 @@ func main() {
3131
if err != nil {
3232
log.Fatalf("failed to build default components: %v", err)
3333
}
34-
3534
info := component.BuildInfo{
3635
Command: "otelcol",
3736
Description: "OpenTelemetry Collector",
3837
Version: version.Version,
3938
}
4039

41-
if err := run(service.Parameters{BuildInfo: info, Factories: factories}); err != nil {
40+
if err := run(service.AppSettings{BuildInfo: info, Factories: factories}); err != nil {
4241
log.Fatal(err)
4342
}
4443
}
4544

46-
func runInteractive(params service.Parameters) error {
47-
app, err := service.New(params)
45+
func runInteractive(settings service.AppSettings) error {
46+
app, err := service.New(settings)
4847
if err != nil {
4948
return fmt.Errorf("failed to construct the application: %w", err)
5049
}

cmd/otelcol/main_others.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ package main
1818

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

21-
func run(params service.Parameters) error {
22-
return runInteractive(params)
21+
func run(settings service.AppSettings) error {
22+
return runInteractive(settings)
2323
}

cmd/otelcol/main_windows.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import (
2525
"go.opentelemetry.io/collector/service"
2626
)
2727

28-
func run(params service.Parameters) error {
28+
func run(set service.AppSettings) error {
2929
if useInteractiveMode, err := checkUseInteractiveMode(); err != nil {
3030
return err
3131
} else if useInteractiveMode {
32-
return runInteractive(params)
32+
return runInteractive(set)
3333
} else {
34-
return runService(params)
34+
return runService(set)
3535
}
3636
}
3737

@@ -51,9 +51,9 @@ func checkUseInteractiveMode() (bool, error) {
5151
}
5252
}
5353

54-
func runService(params service.Parameters) error {
54+
func runService(set service.AppSettings) error {
5555
// do not need to supply service name when startup is invoked through Service Control Manager directly
56-
if err := svc.Run("", service.NewWindowsService(params)); err != nil {
56+
if err := svc.Run("", service.NewWindowsService(set)); err != nil {
5757
return fmt.Errorf("failed to start service %w", err)
5858
}
5959

service/application.go

+10-25
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,24 @@ type Application struct {
7979
asyncErrorChannel chan error
8080
}
8181

82-
// Parameters holds configuration for creating a new Application.
83-
type Parameters struct {
84-
// Factories component factories.
85-
Factories component.Factories
86-
// BuildInfo provides application start information.
87-
BuildInfo component.BuildInfo
88-
// ParserProvider provides the configuration's Parser.
89-
// If it is not provided a default provider is used. The default provider loads the configuration
90-
// from a config file define by the --config command line flag and overrides component's configuration
91-
// properties supplied via --set command line flag.
92-
ParserProvider parserprovider.ParserProvider
93-
// LoggingOptions provides a way to change behavior of zap logging.
94-
LoggingOptions []zap.Option
95-
}
96-
9782
// New creates and returns a new instance of Application.
98-
func New(params Parameters) (*Application, error) {
99-
if err := configcheck.ValidateConfigFromFactories(params.Factories); err != nil {
83+
func New(set AppSettings) (*Application, error) {
84+
if err := configcheck.ValidateConfigFromFactories(set.Factories); err != nil {
10085
return nil, err
10186
}
10287

10388
app := &Application{
104-
info: params.BuildInfo,
105-
factories: params.Factories,
89+
info: set.BuildInfo,
90+
factories: set.Factories,
10691
stateChannel: make(chan State, Closed+1),
10792
}
10893

10994
rootCmd := &cobra.Command{
110-
Use: params.BuildInfo.Command,
111-
Version: params.BuildInfo.Version,
95+
Use: set.BuildInfo.Command,
96+
Version: set.BuildInfo.Version,
11297
RunE: func(cmd *cobra.Command, args []string) error {
11398
var err error
114-
if app.logger, err = newLogger(params.LoggingOptions); err != nil {
99+
if app.logger, err = newLogger(set.LoggingOptions); err != nil {
115100
return fmt.Errorf("failed to get logger: %w", err)
116101
}
117102

@@ -134,7 +119,7 @@ func New(params Parameters) (*Application, error) {
134119
rootCmd.Flags().AddGoFlagSet(flagSet)
135120
app.rootCmd = rootCmd
136121

137-
parserProvider := params.ParserProvider
122+
parserProvider := set.ParserProvider
138123
if parserProvider == nil {
139124
// use default provider.
140125
parserProvider = parserprovider.Default()
@@ -235,9 +220,9 @@ func (app *Application) setupConfigurationComponents(ctx context.Context) error
235220

236221
app.logger.Info("Applying configuration...")
237222

238-
service, err := newService(&settings{
239-
Factories: app.factories,
223+
service, err := newService(&svcSettings{
240224
BuildInfo: app.info,
225+
Factories: app.factories,
241226
Config: cfg,
242227
Logger: app.logger,
243228
AsyncErrorChannel: app.asyncErrorChannel,

service/application_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func TestApplication_Start(t *testing.T) {
5151
return nil
5252
}
5353

54-
app, err := New(Parameters{Factories: factories, BuildInfo: component.DefaultBuildInfo(), LoggingOptions: []zap.Option{zap.Hooks(hook)}})
54+
app, err := New(AppSettings{
55+
BuildInfo: component.DefaultBuildInfo(),
56+
Factories: factories,
57+
LoggingOptions: []zap.Option{zap.Hooks(hook)},
58+
})
5559
require.NoError(t, err)
5660
assert.Equal(t, app.rootCmd, app.Command())
5761

@@ -119,7 +123,7 @@ func TestApplication_ReportError(t *testing.T) {
119123
factories, err := defaultcomponents.Components()
120124
require.NoError(t, err)
121125

122-
app, err := New(Parameters{Factories: factories, BuildInfo: component.DefaultBuildInfo()})
126+
app, err := New(AppSettings{BuildInfo: component.DefaultBuildInfo(), Factories: factories})
123127
require.NoError(t, err)
124128

125129
app.rootCmd.SetArgs([]string{"--config=testdata/otelcol-config-minimal.yaml"})
@@ -142,12 +146,12 @@ func TestApplication_StartAsGoRoutine(t *testing.T) {
142146
factories, err := defaultcomponents.Components()
143147
require.NoError(t, err)
144148

145-
params := Parameters{
149+
set := AppSettings{
146150
BuildInfo: component.DefaultBuildInfo(),
147-
ParserProvider: new(minimalParserLoader),
148151
Factories: factories,
152+
ParserProvider: new(minimalParserLoader),
149153
}
150-
app, err := New(params)
154+
app, err := New(set)
151155
require.NoError(t, err)
152156

153157
appDone := make(chan struct{})

service/application_windows.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import (
2727
)
2828

2929
type WindowsService struct {
30-
params Parameters
31-
app *Application
30+
settings AppSettings
31+
app *Application
3232
}
3333

34-
func NewWindowsService(params Parameters) *WindowsService {
35-
return &WindowsService{params: params}
34+
func NewWindowsService(set AppSettings) *WindowsService {
35+
return &WindowsService{settings: set}
3636
}
3737

3838
// Execute implements https://godoc.org/golang.org/x/sys/windows/svc#Handler
@@ -81,7 +81,7 @@ func (s *WindowsService) Execute(args []string, requests <-chan svc.ChangeReques
8181

8282
func (s *WindowsService) start(elog *eventlog.Log, appErrorChannel chan error) error {
8383
var err error
84-
s.app, err = newWithWindowsEventLogCore(s.params, elog)
84+
s.app, err = newWithWindowsEventLogCore(s.settings, elog)
8585
if err != nil {
8686
return err
8787
}
@@ -120,12 +120,12 @@ func openEventLog(serviceName string) (*eventlog.Log, error) {
120120
return elog, nil
121121
}
122122

123-
func newWithWindowsEventLogCore(params Parameters, elog *eventlog.Log) (*Application, error) {
124-
params.LoggingOptions = append(
125-
params.LoggingOptions,
123+
func newWithWindowsEventLogCore(set AppSettings, elog *eventlog.Log) (*Application, error) {
124+
set.LoggingOptions = append(
125+
set.LoggingOptions,
126126
zap.WrapCore(withWindowsCore(elog)),
127127
)
128-
return New(params)
128+
return New(set)
129129
}
130130

131131
var _ zapcore.Core = (*windowsEventLogCore)(nil)

service/application_windows_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestWindowsService_Execute(t *testing.T) {
3434
factories, err := defaultcomponents.Components()
3535
require.NoError(t, err)
3636

37-
s := NewWindowsService(Parameters{Factories: factories, BuildInfo: component.DefaultBuildInfo()})
37+
s := NewWindowsService(AppSettings{BuildInfo: component.DefaultBuildInfo(), Factories: factories})
3838

3939
appDone := make(chan struct{})
4040
requests := make(chan svc.ChangeRequest)

service/service.go

+6-24
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@ import (
2626
"go.opentelemetry.io/collector/service/internal/builder"
2727
)
2828

29-
// settings holds configuration for building a new service.
30-
type settings struct {
31-
// Factories component factories.
32-
Factories component.Factories
33-
34-
// BuildInfo provides application start information.
35-
BuildInfo component.BuildInfo
36-
37-
// Config represents the configuration of the service.
38-
Config *config.Config
39-
40-
// Logger represents the logger used for all the components.
41-
Logger *zap.Logger
42-
43-
// AsyncErrorChannel is the channel that is used to report fatal errors.
44-
AsyncErrorChannel chan error
45-
}
46-
4729
// service represents the implementation of a component.Host.
4830
type service struct {
4931
factories component.Factories
@@ -58,13 +40,13 @@ type service struct {
5840
builtExtensions builder.Extensions
5941
}
6042

61-
func newService(settings *settings) (*service, error) {
43+
func newService(set *svcSettings) (*service, error) {
6244
srv := &service{
63-
factories: settings.Factories,
64-
buildInfo: settings.BuildInfo,
65-
config: settings.Config,
66-
logger: settings.Logger,
67-
asyncErrorChannel: settings.AsyncErrorChannel,
45+
factories: set.Factories,
46+
buildInfo: set.BuildInfo,
47+
config: set.Config,
48+
logger: set.Logger,
49+
asyncErrorChannel: set.AsyncErrorChannel,
6850
}
6951

7052
if err := srv.config.Validate(); err != nil {

service/service_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func createExampleService(t *testing.T) *service {
104104
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "otelcol-nop.yaml"), factories)
105105
require.NoError(t, err)
106106

107-
srv, err := newService(&settings{
108-
Factories: factories,
107+
srv, err := newService(&svcSettings{
109108
BuildInfo: component.DefaultBuildInfo(),
110-
Config: cfg,
109+
Factories: factories,
111110
Logger: zap.NewNop(),
111+
Config: cfg,
112112
})
113113
require.NoError(t, err)
114114
return srv

service/settings.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package service
16+
17+
import (
18+
"go.uber.org/zap"
19+
20+
"go.opentelemetry.io/collector/component"
21+
"go.opentelemetry.io/collector/config"
22+
"go.opentelemetry.io/collector/service/parserprovider"
23+
)
24+
25+
// svcSettings holds configuration for building a new service.
26+
type svcSettings struct {
27+
// Factories component factories.
28+
Factories component.Factories
29+
30+
// BuildInfo provides application start information.
31+
BuildInfo component.BuildInfo
32+
33+
// Config represents the configuration of the service.
34+
Config *config.Config
35+
36+
// Logger represents the logger used for all the components.
37+
Logger *zap.Logger
38+
39+
// AsyncErrorChannel is the channel that is used to report fatal errors.
40+
AsyncErrorChannel chan error
41+
}
42+
43+
// AppSettings holds configuration for creating a new Application.
44+
type AppSettings struct {
45+
// Factories component factories.
46+
Factories component.Factories
47+
48+
// BuildInfo provides application start information.
49+
BuildInfo component.BuildInfo
50+
51+
// ParserProvider provides the configuration's Parser.
52+
// If it is not provided a default provider is used. The default provider loads the configuration
53+
// from a config file define by the --config command line flag and overrides component's configuration
54+
// properties supplied via --set command line flag.
55+
ParserProvider parserprovider.ParserProvider
56+
57+
// LoggingOptions provides a way to change behavior of zap logging.
58+
LoggingOptions []zap.Option
59+
}

testbed/testbed/otelcol_runner.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ func (ipp *InProcessCollector) PrepareConfig(configStr string) (configCleanup fu
8484
}
8585

8686
func (ipp *InProcessCollector) Start(args StartParams) error {
87-
params := service.Parameters{
87+
settings := service.AppSettings{
8888
BuildInfo: component.BuildInfo{
8989
Command: "otelcol",
9090
Version: version.Version,
9191
},
92-
ParserProvider: parserprovider.NewInMemory(strings.NewReader(ipp.configStr)),
9392
Factories: ipp.factories,
93+
ParserProvider: parserprovider.NewInMemory(strings.NewReader(ipp.configStr)),
9494
}
9595
var err error
96-
ipp.svc, err = service.New(params)
96+
ipp.svc, err = service.New(settings)
9797
if err != nil {
9898
return err
9999
}

0 commit comments

Comments
 (0)