Skip to content

Commit

Permalink
Add exporterConfig validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Dec 21, 2021
1 parent 4ad0d98 commit b203790
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
21 changes: 16 additions & 5 deletions distro/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"google.golang.org/grpc"
)

// Environment variable keys that set values of the configuration.
Expand Down Expand Up @@ -62,7 +63,11 @@ type exporterConfig struct {
}

// Validate ensures c is valid, otherwise returning an appropriate error.
func (c exporterConfig) Validate() error {
func (c *exporterConfig) Validate() error {
if c == nil {
return nil
}

var errs []string

if c.Endpoint != "" {
Expand Down Expand Up @@ -91,6 +96,8 @@ func newConfig(opts ...Option) (*config, error) {
ExportConfig: &exporterConfig{
AccessToken: envOr(accessTokenKey, defaultAccessToken),
},
// FIXME: only load this after checking if the user passed
// WithTraceExporter or not.
TraceExporterFunc: loadTraceExporter(envOr(otelTracesExporterKey, defaultTraceExporter)),
}

Expand All @@ -115,7 +122,9 @@ func newConfig(opts ...Option) (*config, error) {
func (c *config) Validate() error {
var errs []string

errs = append(errs, c.ExportConfig.Validate().Error())
if ecErr := c.ExportConfig.Validate(); ecErr != nil {
errs = append(errs, ecErr.Error())
}

if len(errs) > 0 {
return fmt.Errorf("invalid config: %v", errs)
Expand Down Expand Up @@ -196,13 +205,15 @@ type traceExporterFunc func(*exporterConfig) (sdktrace.SpanExporter, error)
var exporters = map[string]traceExporterFunc{
// OTLP gRPC exporter.
"otlp": func(c *exporterConfig) (sdktrace.SpanExporter, error) {
var opts []otlptracegrpc.Option

endpoint := c.Endpoint
if endpoint == "" {
endpoint = defaultJaegerEndpoint
}
opts = append(opts, otlptracegrpc.WithEndpoint(endpoint))
opts := []otlptracegrpc.Option{
// FIXME: remove (maybe?)
otlptracegrpc.WithDialOption(grpc.WithBlock()),
otlptracegrpc.WithEndpoint(endpoint),
}

if c.AccessToken != "" {
opts = append(opts, otlptracegrpc.WithHeaders(map[string]string{
Expand Down
8 changes: 4 additions & 4 deletions distro/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var ConfigurationTests = []*ConfigFieldTest{
{
Name: "AccessToken",
ValueFunc: func(c *config) interface{} {
return c.AccessToken
return c.ExportConfig.AccessToken
},
DefaultValue: "",
EnvironmentTests: []KeyValue{
Expand All @@ -62,15 +62,15 @@ var ConfigurationTests = []*ConfigFieldTest{
},
AssertionFunc: func(t *testing.T, c *config, e error) {
assert.NoError(t, e)
assert.Equal(t, "secret", c.AccessToken)
assert.Equal(t, "secret", c.ExportConfig.AccessToken)
},
},
},
},
{
Name: "Endpoint",
ValueFunc: func(c *config) interface{} {
return c.Endpoint
return c.ExportConfig.Endpoint
},
DefaultValue: "",
OptionTests: []OptionTest{
Expand All @@ -81,7 +81,7 @@ var ConfigurationTests = []*ConfigFieldTest{
},
AssertionFunc: func(t *testing.T, c *config, e error) {
assert.NoError(t, e)
assert.Equal(t, "https://localhost/", c.Endpoint)
assert.Equal(t, "https://localhost/", c.ExportConfig.Endpoint)
},
},
{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.3.0
go.opentelemetry.io/otel/sdk v1.3.0
go.opentelemetry.io/otel/trace v1.3.0
google.golang.org/grpc v1.42.0
)

0 comments on commit b203790

Please sign in to comment.