Skip to content

Commit

Permalink
fix: add basic validation in otlphttpexporter (#4860)
Browse files Browse the repository at this point in the history
* fix: add basic validation in otlphttpexporter

* chore: add to changelog

* fix: split testfile into good and bad config

Co-authored-by: Alex Boten <aboten@lightstep.com>
Co-authored-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
3 people committed Feb 16, 2022
1 parent 26e62ce commit ec3384d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
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]

0 comments on commit ec3384d

Please sign in to comment.