Skip to content

Commit aee771d

Browse files
committed
[otelcol] Marshal the Collector's config from otelcol.Config
1 parent 650ac0b commit aee771d

File tree

6 files changed

+59
-144
lines changed

6 files changed

+59
-144
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: otelcol
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Obtain the Collector's effective config from otelcol.Config
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: []
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: "`otelcol.Collector` will now marshal `confmap.Conf` objects from `otelcol.Config` itself."
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: deprecation
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: otelcol
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Deprecate `otelcol.ConfmapProvider`
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: []
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: "`otelcol.Collector` will now marshal `confmap.Conf` objects from `otelcol.Config` itself."
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

otelcol/collector.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,6 @@ func (col *Collector) Shutdown() {
163163
func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
164164
col.setCollectorState(StateStarting)
165165

166-
var conf *confmap.Conf
167-
168-
if cp, ok := col.configProvider.(ConfmapProvider); ok {
169-
var err error
170-
conf, err = cp.GetConfmap(ctx)
171-
172-
if err != nil {
173-
return fmt.Errorf("failed to resolve config: %w", err)
174-
}
175-
}
176-
177166
factories, err := col.set.Factories()
178167
if err != nil {
179168
return fmt.Errorf("failed to initialize factories: %w", err)
@@ -189,6 +178,12 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
189178

190179
col.serviceConfig = &cfg.Service
191180

181+
conf := confmap.New()
182+
183+
if err := conf.Marshal(cfg); err != nil {
184+
return fmt.Errorf("could not marshal configuration: %w", err)
185+
}
186+
192187
col.service, err = service.New(ctx, service.Settings{
193188
BuildInfo: col.set.BuildInfo,
194189
CollectorConf: conf,

otelcol/collector_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -450,24 +450,6 @@ func TestCollectorDryRun(t *testing.T) {
450450
}
451451
}
452452

453-
func TestPassConfmapToServiceFailure(t *testing.T) {
454-
set := CollectorSettings{
455-
BuildInfo: component.NewDefaultBuildInfo(),
456-
Factories: nopFactories,
457-
ConfigProviderSettings: ConfigProviderSettings{
458-
ResolverSettings: confmap.ResolverSettings{
459-
URIs: []string{filepath.Join("testdata", "otelcol-invalid.yaml")},
460-
ProviderFactories: []confmap.ProviderFactory{confmap.NewProviderFactory(newFailureProvider)},
461-
},
462-
},
463-
}
464-
col, err := NewCollector(set)
465-
require.NoError(t, err)
466-
467-
err = col.Run(context.Background())
468-
require.Error(t, err)
469-
}
470-
471453
func startCollector(ctx context.Context, t *testing.T, col *Collector) *sync.WaitGroup {
472454
wg := &sync.WaitGroup{}
473455
wg.Add(1)

otelcol/configprovider.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type ConfigProvider interface {
5858
//
5959
// The purpose of this interface is that otelcol.ConfigProvider structs do not
6060
// necessarily need to use confmap.Conf as their underlying config structure.
61+
//
62+
// Deprecated: [v0.105.0] This interface is deprecated. otelcol.Collector will now obtain
63+
// a confmap.Conf object from the unmarshaled config itself.
6164
type ConfmapProvider interface {
6265
// GetConfmap resolves the Collector's configuration and provides it as a confmap.Conf object.
6366
//
@@ -70,7 +73,6 @@ type configProvider struct {
7073
}
7174

7275
var _ ConfigProvider = (*configProvider)(nil)
73-
var _ ConfmapProvider = (*configProvider)(nil)
7476

7577
// ConfigProviderSettings are the settings to configure the behavior of the ConfigProvider.
7678
type ConfigProviderSettings struct {
@@ -142,12 +144,3 @@ func (cm *configProvider) Watch() <-chan error {
142144
func (cm *configProvider) Shutdown(ctx context.Context) error {
143145
return cm.mapResolver.Shutdown(ctx)
144146
}
145-
146-
func (cm *configProvider) GetConfmap(ctx context.Context) (*confmap.Conf, error) {
147-
conf, err := cm.mapResolver.Resolve(ctx)
148-
if err != nil {
149-
return nil, fmt.Errorf("cannot resolve the configuration: %w", err)
150-
}
151-
152-
return conf, nil
153-
}

otelcol/configprovider_test.go

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"gopkg.in/yaml.v3"
1515

1616
"go.opentelemetry.io/collector/confmap"
17-
"go.opentelemetry.io/collector/featuregate"
18-
"go.opentelemetry.io/collector/internal/featuregates"
1917
)
2018

2119
func newConfig(yamlBytes []byte, factories Factories) (*Config, error) {
@@ -108,106 +106,3 @@ func TestConfigProviderFile(t *testing.T) {
108106

109107
assert.EqualValues(t, configNop, cfg)
110108
}
111-
112-
func TestGetConfmap(t *testing.T) {
113-
uriLocation := "file:" + filepath.Join("testdata", "otelcol-nop.yaml")
114-
fileProvider := newFakeProvider("file", func(_ context.Context, _ string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) {
115-
return confmap.NewRetrieved(newConfFromFile(t, uriLocation[5:]))
116-
})
117-
set := ConfigProviderSettings{
118-
ResolverSettings: confmap.ResolverSettings{
119-
URIs: []string{uriLocation},
120-
ProviderFactories: []confmap.ProviderFactory{fileProvider},
121-
},
122-
}
123-
124-
configBytes, err := os.ReadFile(filepath.Join("testdata", "otelcol-nop.yaml"))
125-
require.NoError(t, err)
126-
127-
yamlMap := map[string]any{}
128-
err = yaml.Unmarshal(configBytes, yamlMap)
129-
require.NoError(t, err)
130-
131-
cp, err := NewConfigProvider(set)
132-
require.NoError(t, err)
133-
134-
cmp, ok := cp.(ConfmapProvider)
135-
require.True(t, ok)
136-
137-
cmap, err := cmp.GetConfmap(context.Background())
138-
require.NoError(t, err)
139-
140-
assert.EqualValues(t, yamlMap, cmap.ToStringMap())
141-
}
142-
143-
func TestStrictlyTypedCoda(t *testing.T) {
144-
tests := []struct {
145-
basename string
146-
// isErrFromStrictTypes indicates whether the test should expect an error when the feature gate is
147-
// disabled. If so, we check that it errs both with and without the feature gate and that the coda is never
148-
// present.
149-
isErrFromStrictTypes bool
150-
}{
151-
{basename: "weak-implicit-bool-to-string.yaml"},
152-
{basename: "weak-implicit-int-to-string.yaml"},
153-
{basename: "weak-implicit-bool-to-int.yaml"},
154-
{basename: "weak-implicit-string-to-int.yaml"},
155-
{basename: "weak-implicit-int-to-bool.yaml"},
156-
{basename: "weak-implicit-string-to-bool.yaml"},
157-
{basename: "weak-empty-map-to-empty-array.yaml"},
158-
{basename: "weak-slice-of-maps-to-map.yaml"},
159-
{basename: "weak-single-element-to-slice.yaml"},
160-
{
161-
basename: "otelcol-invalid-components.yaml",
162-
isErrFromStrictTypes: true,
163-
},
164-
}
165-
166-
for _, tt := range tests {
167-
t.Run(tt.basename, func(t *testing.T) {
168-
filename := filepath.Join("testdata", tt.basename)
169-
uriLocation := "file:" + filename
170-
fileProvider := newFakeProvider("file", func(_ context.Context, _ string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) {
171-
return confmap.NewRetrieved(newConfFromFile(t, filename))
172-
})
173-
cp, err := NewConfigProvider(ConfigProviderSettings{
174-
ResolverSettings: confmap.ResolverSettings{
175-
URIs: []string{uriLocation},
176-
ProviderFactories: []confmap.ProviderFactory{fileProvider},
177-
},
178-
})
179-
require.NoError(t, err)
180-
factories, err := nopFactories()
181-
require.NoError(t, err)
182-
183-
// Save the previous value of the feature gate and restore it after the test.
184-
prev := featuregates.StrictlyTypedInputGate.IsEnabled()
185-
defer func() {
186-
require.NoError(t, featuregate.GlobalRegistry().Set(featuregates.StrictlyTypedInputID, prev))
187-
}()
188-
189-
// Ensure the error does not appear with the feature gate disabled.
190-
require.NoError(t, featuregate.GlobalRegistry().Set(featuregates.StrictlyTypedInputID, false))
191-
_, errWeakTypes := cp.Get(context.Background(), factories)
192-
if tt.isErrFromStrictTypes {
193-
require.Error(t, errWeakTypes)
194-
// Ensure coda is **NOT** present.
195-
assert.NotContains(t, errWeakTypes.Error(), strictlyTypedMessageCoda)
196-
} else {
197-
require.NoError(t, errWeakTypes)
198-
}
199-
200-
// Test with the feature gate enabled.
201-
require.NoError(t, featuregate.GlobalRegistry().Set(featuregates.StrictlyTypedInputID, true))
202-
_, errStrictTypes := cp.Get(context.Background(), factories)
203-
require.Error(t, errStrictTypes)
204-
if tt.isErrFromStrictTypes {
205-
// Ensure coda is **NOT** present.
206-
assert.NotContains(t, errStrictTypes.Error(), strictlyTypedMessageCoda)
207-
} else {
208-
// Ensure coda is present.
209-
assert.ErrorContains(t, errStrictTypes, strictlyTypedMessageCoda)
210-
}
211-
})
212-
}
213-
}

0 commit comments

Comments
 (0)