Skip to content

Commit

Permalink
Move otelcoltest to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Jun 17, 2024
1 parent f88ce7a commit a935651
Show file tree
Hide file tree
Showing 15 changed files with 858 additions and 65 deletions.
25 changes: 25 additions & 0 deletions .chloggen/move-otelcoltest-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: new_component

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: otelcoltest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: move go.opentelemetry.io/collector/otelcol/otelcoltest to its own package, go.opentelemetry.io/collector/otelcoltest

# One or more tracking issues or pull requests related to the change
issues: [10417]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
25 changes: 25 additions & 0 deletions .chloggen/move-otelcoltest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# 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: otelcol/otelcoltest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecated. Use go.opentelemetry.io/collector/otelcoltest instead

# One or more tracking issues or pull requests related to the change
issues: [10417]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
37 changes: 6 additions & 31 deletions otelcol/otelcoltest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@ import (
"go.opentelemetry.io/collector/otelcol"
)

// LoadConfigWithSettings loads a config.Config from the provider settings, and does NOT validate the configuration.
func LoadConfigWithSettings(factories otelcol.Factories, set otelcol.ConfigProviderSettings) (*otelcol.Config, error) {
// Read yaml config from file
provider, err := otelcol.NewConfigProvider(set)
if err != nil {
return nil, err
}
return provider.Get(context.Background(), factories)
}

// LoadConfig loads a config.Config from file, and does NOT validate the configuration.
//
// Deprecated: [v0.103.0] use LoadConfigWithSettings instead
// Deprecated: [v0.103.0] Use go.opentelemetry.io/collector/otelcoltest instead
func LoadConfig(fileName string, factories otelcol.Factories) (*otelcol.Config, error) {
return LoadConfigWithSettings(factories, otelcol.ConfigProviderSettings{
// Read yaml config from file
provider, err := otelcol.NewConfigProvider(otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{fileName},
ProviderFactories: []confmap.ProviderFactory{
Expand All @@ -41,33 +32,17 @@ func LoadConfig(fileName string, factories otelcol.Factories) (*otelcol.Config,
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
})
}

// LoadConfigAndValidateWithSettings loads a config from the provider settings, and validates the configuration.
func LoadConfigAndValidateWithSettings(factories otelcol.Factories, set otelcol.ConfigProviderSettings) (*otelcol.Config, error) {
cfg, err := LoadConfigWithSettings(factories, set)
if err != nil {
return nil, err
}
return cfg, cfg.Validate()
return provider.Get(context.Background(), factories)
}

// LoadConfigAndValidate loads a config from the file, and validates the configuration.
//
// Deprecated: [v0.103.0] Use LoadConfigAndValidateWithSettings instead
// Deprecated: [v0.103.0] Use go.opentelemetry.io/collector/otelcoltest instead
func LoadConfigAndValidate(fileName string, factories otelcol.Factories) (*otelcol.Config, error) {
cfg, err := LoadConfigWithSettings(factories, otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{fileName},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
})
cfg, err := LoadConfig(fileName, factories)
if err != nil {
return nil, err
}
Expand Down
37 changes: 3 additions & 34 deletions otelcol/otelcoltest/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,14 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/converter/expandconverter"
"go.opentelemetry.io/collector/confmap/provider/envprovider"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"go.opentelemetry.io/collector/confmap/provider/httpprovider"
"go.opentelemetry.io/collector/confmap/provider/yamlprovider"
"go.opentelemetry.io/collector/otelcol"
"go.opentelemetry.io/collector/service/pipelines"
)

func TestLoadConfig(t *testing.T) {
factories, err := NopFactories()
assert.NoError(t, err)

cfg, err := LoadConfigWithSettings(factories, otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", "config.yaml")},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
})
cfg, err := LoadConfig(filepath.Join("testdata", "config.yaml"), factories)
require.NoError(t, err)

// Verify extensions.
Expand Down Expand Up @@ -81,23 +63,10 @@ func TestLoadConfigAndValidate(t *testing.T) {
factories, err := NopFactories()
assert.NoError(t, err)

set := otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", "config.yaml")},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
}

cfgValidate, errValidate := LoadConfigAndValidateWithSettings(factories, set)
cfgValidate, errValidate := LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
require.NoError(t, errValidate)

cfg, errLoad := LoadConfigWithSettings(factories, set)
cfg, errLoad := LoadConfig(filepath.Join("testdata", "config.yaml"), factories)
require.NoError(t, errLoad)

assert.Equal(t, cfg, cfgValidate)
Expand Down
2 changes: 2 additions & 0 deletions otelcol/otelcoltest/nop_factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
)

// NopFactories returns a otelcol.Factories with all nop factories.
//
// Deprecated: [v0.103.0] Use go.opentelemetry.io/collector/otelcoltest instead
func NopFactories() (otelcol.Factories, error) {
var factories otelcol.Factories
var err error
Expand Down
1 change: 1 addition & 0 deletions otelcoltest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../Makefile.Common
75 changes: 75 additions & 0 deletions otelcoltest/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelcoltest // import "go.opentelemetry.io/collector/otelcoltest"

import (
"context"

"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/converter/expandconverter"
"go.opentelemetry.io/collector/confmap/provider/envprovider"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"go.opentelemetry.io/collector/confmap/provider/httpprovider"
"go.opentelemetry.io/collector/confmap/provider/yamlprovider"
"go.opentelemetry.io/collector/otelcol"
)

// LoadConfigWithSettings loads a config.Config from the provider settings, and does NOT validate the configuration.
func LoadConfigWithSettings(factories otelcol.Factories, set otelcol.ConfigProviderSettings) (*otelcol.Config, error) {
// Read yaml config from file
provider, err := otelcol.NewConfigProvider(set)
if err != nil {
return nil, err
}
return provider.Get(context.Background(), factories)
}

// LoadConfig loads a config.Config from file, and does NOT validate the configuration.
//
// Deprecated: [v0.103.0] use LoadConfigWithSettings instead
func LoadConfig(fileName string, factories otelcol.Factories) (*otelcol.Config, error) {
return LoadConfigWithSettings(factories, otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{fileName},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
})
}

// LoadConfigAndValidateWithSettings loads a config from the provider settings, and validates the configuration.
func LoadConfigAndValidateWithSettings(factories otelcol.Factories, set otelcol.ConfigProviderSettings) (*otelcol.Config, error) {
cfg, err := LoadConfigWithSettings(factories, set)
if err != nil {
return nil, err
}
return cfg, cfg.Validate()
}

// LoadConfigAndValidate loads a config from the file, and validates the configuration.
//
// Deprecated: [v0.103.0] Use LoadConfigAndValidateWithSettings instead
func LoadConfigAndValidate(fileName string, factories otelcol.Factories) (*otelcol.Config, error) {
cfg, err := LoadConfigWithSettings(factories, otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{fileName},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
})
if err != nil {
return nil, err
}
return cfg, cfg.Validate()
}
104 changes: 104 additions & 0 deletions otelcoltest/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelcoltest

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/converter/expandconverter"
"go.opentelemetry.io/collector/confmap/provider/envprovider"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"go.opentelemetry.io/collector/confmap/provider/httpprovider"
"go.opentelemetry.io/collector/confmap/provider/yamlprovider"
"go.opentelemetry.io/collector/otelcol"
"go.opentelemetry.io/collector/service/pipelines"
)

func TestLoadConfig(t *testing.T) {
factories, err := NopFactories()
assert.NoError(t, err)

cfg, err := LoadConfigWithSettings(factories, otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", "config.yaml")},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
})
require.NoError(t, err)

// Verify extensions.
require.Len(t, cfg.Extensions, 2)
assert.Contains(t, cfg.Extensions, component.MustNewID("nop"))
assert.Contains(t, cfg.Extensions, component.MustNewIDWithName("nop", "myextension"))

// Verify receivers
require.Len(t, cfg.Receivers, 2)
assert.Contains(t, cfg.Receivers, component.MustNewID("nop"))
assert.Contains(t, cfg.Receivers, component.MustNewIDWithName("nop", "myreceiver"))

// Verify exporters
assert.Len(t, cfg.Exporters, 2)
assert.Contains(t, cfg.Exporters, component.MustNewID("nop"))
assert.Contains(t, cfg.Exporters, component.MustNewIDWithName("nop", "myexporter"))

// Verify procs
assert.Len(t, cfg.Processors, 2)
assert.Contains(t, cfg.Processors, component.MustNewID("nop"))
assert.Contains(t, cfg.Processors, component.MustNewIDWithName("nop", "myprocessor"))

// Verify connectors
assert.Len(t, cfg.Connectors, 1)
assert.Contains(t, cfg.Connectors, component.MustNewIDWithName("nop", "myconnector"))

// Verify service.
require.Len(t, cfg.Service.Extensions, 1)
assert.Contains(t, cfg.Service.Extensions, component.MustNewID("nop"))
require.Len(t, cfg.Service.Pipelines, 1)
assert.Equal(t,
&pipelines.PipelineConfig{
Receivers: []component.ID{component.MustNewID("nop")},
Processors: []component.ID{component.MustNewID("nop")},
Exporters: []component.ID{component.MustNewID("nop")},
},
cfg.Service.Pipelines[component.MustNewID("traces")],
"Did not load pipeline config correctly")
}

func TestLoadConfigAndValidate(t *testing.T) {
factories, err := NopFactories()
assert.NoError(t, err)

set := otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{filepath.Join("testdata", "config.yaml")},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
yamlprovider.NewFactory(),
httpprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{expandconverter.NewFactory()},
},
}

cfgValidate, errValidate := LoadConfigAndValidateWithSettings(factories, set)
require.NoError(t, errValidate)

cfg, errLoad := LoadConfigWithSettings(factories, set)
require.NoError(t, errLoad)

assert.Equal(t, cfg, cfgValidate)
}
Loading

0 comments on commit a935651

Please sign in to comment.