Skip to content

Commit 22bcedc

Browse files
Rework Settings structure to fit all requirements.
Split it into ServiceSettings and ApplicationSettings. Signed-off-by: Patryk Matyjasek <[email protected]>
1 parent 0f5ba54 commit 22bcedc

11 files changed

+119
-78
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Remove `tracetranslator.TagHTTPStatusCode`, use `conventions.AttributeHTTPStatusCode` (#3111)
1111
- Remove OpenCensus status constants and transformation (#3110)
1212
- Remove `tracetranslator.AttributeArrayToSlice`, not used in core or contrib (#3109)
13+
- Introduce `ServiceSettings` and `ApplicationSettings` instead of `Parameters` (#3163)
1314

1415
## v0.26.0 Beta
1516

cmd/otelcol/main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,21 @@ func main() {
3232
log.Fatalf("failed to build default components: %v", err)
3333
}
3434

35-
componentSettings := component.ComponentSettings{
35+
commonSettings := service.CommonSettings{
3636
BuildInfo: component.BuildInfo{
3737
Command: "otelcol",
3838
Description: "OpenTelemetry Collector",
3939
Version: version.Version,
4040
},
41+
Factories: factories,
4142
}
4243

43-
if err := run(service.Settings{ComponentSettings: componentSettings, Factories: factories}); err != nil {
44+
if err := run(service.ApplicationSettings{CommonSettings: commonSettings}); err != nil {
4445
log.Fatal(err)
4546
}
4647
}
4748

48-
func runInteractive(settings service.Settings) error {
49+
func runInteractive(settings service.ApplicationSettings) error {
4950
app, err := service.New(settings)
5051
if err != nil {
5152
return fmt.Errorf("failed to construct the application: %w", err)

cmd/otelcol/main_others.go

+1-1
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(settings service.Settings) error {
21+
func run(settings service.ApplicationSettings) error {
2222
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(settings service.Settings) error {
28+
func run(set service.ApplicationSettings) error {
2929
if useInteractiveMode, err := checkUseInteractiveMode(); err != nil {
3030
return err
3131
} else if useInteractiveMode {
32-
return runInteractive(settings)
32+
return runInteractive(set)
3333
} else {
34-
return runService(settings)
34+
return runService(set)
3535
}
3636
}
3737

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

54-
func runService(settings service.Settings) error {
54+
func runService(set service.ApplicationSettings) 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(settings)); 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

+12-11
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,20 @@ type Application struct {
8080
}
8181

8282
// New creates and returns a new instance of Application.
83-
func New(set Settings) (*Application, error) {
84-
if err := configcheck.ValidateConfigFromFactories(set.Factories); err != nil {
83+
func New(set ApplicationSettings) (*Application, error) {
84+
if err := configcheck.ValidateConfigFromFactories(set.CommonSettings.Factories); err != nil {
8585
return nil, err
8686
}
8787

8888
app := &Application{
89-
info: set.ComponentSettings.BuildInfo,
90-
factories: set.Factories,
89+
info: set.CommonSettings.BuildInfo,
90+
factories: set.CommonSettings.Factories,
9191
stateChannel: make(chan State, Closed+1),
9292
}
9393

9494
rootCmd := &cobra.Command{
95-
Use: set.ComponentSettings.BuildInfo.Command,
96-
Version: set.ComponentSettings.BuildInfo.Version,
95+
Use: set.CommonSettings.BuildInfo.Command,
96+
Version: set.CommonSettings.BuildInfo.Version,
9797
RunE: func(cmd *cobra.Command, args []string) error {
9898
var err error
9999
if app.logger, err = newLogger(set.LoggingOptions); err != nil {
@@ -223,15 +223,16 @@ func (app *Application) setupConfigurationComponents(ctx context.Context) error
223223
}
224224

225225
app.logger.Info("Applying configuration...")
226-
componentSettings := component.ComponentSettings{
226+
227+
commonSettings := CommonSettings{
227228
BuildInfo: app.info,
228-
Logger: app.logger,
229+
Factories: app.factories,
229230
}
230231

231-
service, err := newService(&Settings{
232-
Factories: app.factories,
233-
ComponentSettings: componentSettings,
232+
service, err := newService(&ServiceSettings{
233+
CommonSettings: commonSettings,
234234
Config: cfg,
235+
Logger: app.logger,
235236
AsyncErrorChannel: app.asyncErrorChannel,
236237
})
237238
if err != nil {

service/application_test.go

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

54-
componentSettings := component.ComponentSettings{
54+
commonSettings := CommonSettings{
5555
BuildInfo: component.DefaultBuildInfo(),
56+
Factories: factories,
5657
}
5758

58-
app, err := New(Settings{Factories: factories, ComponentSettings: componentSettings, LoggingOptions: []zap.Option{zap.Hooks(hook)}})
59+
app, err := New(ApplicationSettings{CommonSettings: commonSettings, LoggingOptions: []zap.Option{zap.Hooks(hook)}})
5960
require.NoError(t, err)
6061
assert.Equal(t, app.rootCmd, app.Command())
6162

@@ -123,11 +124,12 @@ func TestApplication_ReportError(t *testing.T) {
123124
factories, err := defaultcomponents.Components()
124125
require.NoError(t, err)
125126

126-
componentSettings := component.ComponentSettings{
127+
commonSettings := CommonSettings{
127128
BuildInfo: component.DefaultBuildInfo(),
129+
Factories: factories,
128130
}
129131

130-
app, err := New(Settings{Factories: factories, ComponentSettings: componentSettings})
132+
app, err := New(ApplicationSettings{CommonSettings: commonSettings})
131133
require.NoError(t, err)
132134

133135
app.rootCmd.SetArgs([]string{"--config=testdata/otelcol-config-minimal.yaml"})
@@ -150,14 +152,14 @@ func TestApplication_StartAsGoRoutine(t *testing.T) {
150152
factories, err := defaultcomponents.Components()
151153
require.NoError(t, err)
152154

153-
componentSettings := component.ComponentSettings{
155+
commonSettings := CommonSettings{
154156
BuildInfo: component.DefaultBuildInfo(),
157+
Factories: factories,
155158
}
156159

157-
params := Settings{
158-
ComponentSettings: componentSettings,
159-
ParserProvider: new(minimalParserLoader),
160-
Factories: factories,
160+
params := ApplicationSettings{
161+
CommonSettings: commonSettings,
162+
ParserProvider: new(minimalParserLoader),
161163
}
162164
app, err := New(params)
163165
require.NoError(t, err)

service/application_windows.go

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

2929
type WindowsService struct {
30-
settings Settings
30+
settings ApplicationSettings
3131
app *Application
3232
}
3333

34-
func NewWindowsService(settings Settings) *WindowsService {
35-
return &WindowsService{settings: settings}
34+
func NewWindowsService(set ApplicationSettings) *WindowsService {
35+
return &WindowsService{settings: set}
3636
}
3737

3838
// Execute implements https://godoc.org/golang.org/x/sys/windows/svc#Handler
@@ -120,9 +120,9 @@ func openEventLog(serviceName string) (*eventlog.Log, error) {
120120
return elog, nil
121121
}
122122

123-
func newWithEventViewerLoggingHook(settings Settings, elog *eventlog.Log) (*Application, error) {
124-
settings.LoggingOptions = append(
125-
settings.LoggingOptions,
123+
func newWithEventViewerLoggingHook(set ApplicationSettings, elog *eventlog.Log) (*Application, error) {
124+
set.LoggingOptions = append(
125+
set.LoggingOptions,
126126
zap.Hooks(func(entry zapcore.Entry) error {
127127
msg := fmt.Sprintf("%v\r\n\r\nStack Trace:\r\n%v", entry.Message, entry.Stack)
128128

@@ -143,5 +143,5 @@ func newWithEventViewerLoggingHook(settings Settings, elog *eventlog.Log) (*Appl
143143
}),
144144
)
145145

146-
return New(settings)
146+
return New(set)
147147
}

service/service.go

+6-31
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,8 @@ import (
2424
"go.opentelemetry.io/collector/config"
2525
"go.opentelemetry.io/collector/consumer/consumererror"
2626
"go.opentelemetry.io/collector/service/internal/builder"
27-
"go.opentelemetry.io/collector/service/parserprovider"
2827
)
2928

30-
// settings holds configuration for building a new service.
31-
type Settings struct {
32-
// Factories component factories.
33-
Factories component.Factories
34-
35-
// ComponentSettings contains logger and build info configuration
36-
ComponentSettings component.ComponentSettings
37-
38-
// Config represents the configuration of the service.
39-
Config *config.Config
40-
41-
// ParserProvider provides the configuration's Parser.
42-
// If it is not provided a default provider is used. The default provider loads the configuration
43-
// from a config file define by the --config command line flag and overrides component's configuration
44-
// properties supplied via --set command line flag.
45-
ParserProvider parserprovider.ParserProvider
46-
47-
// LoggingOptions provides a way to change behavior of zap logging.
48-
LoggingOptions []zap.Option
49-
50-
// AsyncErrorChannel is the channel that is used to report fatal errors.
51-
AsyncErrorChannel chan error
52-
}
53-
5429
// service represents the implementation of a component.Host.
5530
type service struct {
5631
factories component.Factories
@@ -65,13 +40,13 @@ type service struct {
6540
builtExtensions builder.Extensions
6641
}
6742

68-
func newService(settings *Settings) (*service, error) {
43+
func newService(set *ServiceSettings) (*service, error) {
6944
srv := &service{
70-
factories: settings.Factories,
71-
buildInfo: settings.ComponentSettings.BuildInfo,
72-
config: settings.Config,
73-
logger: settings.ComponentSettings.Logger,
74-
asyncErrorChannel: settings.AsyncErrorChannel,
45+
factories: set.CommonSettings.Factories,
46+
buildInfo: set.CommonSettings.BuildInfo,
47+
config: set.Config,
48+
logger: set.Logger,
49+
asyncErrorChannel: set.AsyncErrorChannel,
7550
}
7651

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

service/service_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ func createExampleService(t *testing.T) *service {
103103
require.NoError(t, err)
104104
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "otelcol-nop.yaml"), factories)
105105
require.NoError(t, err)
106-
componentSettings := component.ComponentSettings{
106+
commonSettings := CommonSettings{
107107
BuildInfo: component.DefaultBuildInfo(),
108-
Logger: zap.NewNop(),
108+
Factories: factories,
109109
}
110110

111-
srv, err := newService(&Settings{
112-
Factories: factories,
113-
ComponentSettings: componentSettings,
114-
Config: cfg,
111+
srv, err := newService(&ServiceSettings{
112+
CommonSettings: commonSettings,
113+
Logger: zap.NewNop(),
114+
Config: cfg,
115115
})
116116
require.NoError(t, err)
117117
return srv

service/settings.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.opentelemetry.io/collector/component"
19+
"go.opentelemetry.io/collector/config"
20+
"go.opentelemetry.io/collector/service/parserprovider"
21+
"go.uber.org/zap"
22+
)
23+
24+
// CommonSettings holds common settings for Service and Application
25+
type CommonSettings struct {
26+
// Factories component factories.
27+
Factories component.Factories
28+
29+
// BuildInfo provides application start information.
30+
BuildInfo component.BuildInfo
31+
}
32+
33+
// ServiceSettings holds configuration for building a new service.
34+
type ServiceSettings struct {
35+
// CommonSettings contains Factories and BuildInfo
36+
CommonSettings CommonSettings
37+
38+
// Config represents the configuration of the service.
39+
Config *config.Config
40+
41+
// Logger represents the logger used for all the components.
42+
Logger *zap.Logger
43+
44+
// AsyncErrorChannel is the channel that is used to report fatal errors.
45+
AsyncErrorChannel chan error
46+
}
47+
48+
// ApplicationSettings holds configuration for creating a new Application.
49+
type ApplicationSettings struct {
50+
// CommonSettings contains Factories and BuildInfo
51+
CommonSettings CommonSettings
52+
53+
// ParserProvider provides the configuration's Parser.
54+
// If it is not provided a default provider is used. The default provider loads the configuration
55+
// from a config file define by the --config command line flag and overrides component's configuration
56+
// properties supplied via --set command line flag.
57+
ParserProvider parserprovider.ParserProvider
58+
59+
// LoggingOptions provides a way to change behavior of zap logging.
60+
LoggingOptions []zap.Option
61+
}

testbed/testbed/otelcol_runner.go

+5-5
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-
componentSettings := component.ComponentSettings{
87+
commonSettings := service.CommonSettings{
8888
BuildInfo: component.BuildInfo{
8989
Command: "otelcol",
9090
Version: version.Version,
9191
},
92+
Factories: ipp.factories,
9293
}
93-
settings := service.Settings{
94-
ComponentSettings: componentSettings,
95-
ParserProvider: parserprovider.NewInMemory(strings.NewReader(ipp.configStr)),
96-
Factories: ipp.factories,
94+
settings := service.ApplicationSettings{
95+
CommonSettings: commonSettings,
96+
ParserProvider: parserprovider.NewInMemory(strings.NewReader(ipp.configStr)),
9797
}
9898
var err error
9999
ipp.svc, err = service.New(settings)

0 commit comments

Comments
 (0)