Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add basic validation in otlphttpexporter #4860

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### 💡 Enhancements 💡

- Add validation to check at least one endpoint is specified in otlphttpexporter's configuration (#4860)

## v0.45.0 Beta

### 🛑 Breaking changes 🛑
Expand Down
5 changes: 5 additions & 0 deletions exporter/otlphttpexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package otlphttpexporter // import "go.opentelemetry.io/collector/exporter/otlphttpexporter"

import (
"fmt"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/exporter/exporterhelper"
Expand All @@ -41,5 +43,8 @@ var _ config.Exporter = (*Config)(nil)

// Validate checks if the exporter configuration is valid
func (cfg *Config) Validate() error {
if cfg.Endpoint == "" && cfg.TracesEndpoint == "" && cfg.MetricsEndpoint == "" && cfg.LogsEndpoint == "" {
return fmt.Errorf("at least one endpoint must be specified")
}
return nil
}
13 changes: 10 additions & 3 deletions exporter/otlphttpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
// Bad config
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "bad_empty_config.yaml"), factories)

require.Error(t, err)

e0 := cfg.Exporters[config.NewComponentID(typeStr)]
assert.Equal(t, e0, factory.CreateDefaultConfig())

// Good config
cfg, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)

e1 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "2")]
assert.Equal(t, e1,
&Config{
Expand Down
15 changes: 15 additions & 0 deletions exporter/otlphttpexporter/testdata/bad_empty_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
receivers:
nop:

processors:
nop:

exporters:
otlphttp:

service:
pipelines:
traces:
receivers: [nop]
processors: [nop]
exporters: [otlphttp]
3 changes: 1 addition & 2 deletions exporter/otlphttpexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ processors:
nop:

exporters:
otlphttp:
otlphttp/2:
endpoint: "https://1.2.3.4:1234"
tls:
Expand Down Expand Up @@ -36,4 +35,4 @@ service:
traces:
receivers: [nop]
processors: [nop]
exporters: [otlphttp]
exporters: [otlphttp/2]