Skip to content

Commit

Permalink
Use environment variables as default options
Browse files Browse the repository at this point in the history
  • Loading branch information
XSAM committed Jun 6, 2020
1 parent 70523b5 commit a2e8905
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
54 changes: 52 additions & 2 deletions exporters/trace/jaeger/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestNewRawExporterWithEnv(t *testing.T) {
username = "user"
password = "password"
serviceName = "test-service"
disabled = "disable"
disabled = "false"
tags = "key=value"
)

Expand All @@ -145,14 +145,64 @@ func TestNewRawExporterWithEnv(t *testing.T) {
// Create Jaeger Exporter with environment variables
exp, err := NewRawExporter(
WithCollectorEndpoint(CollectorEndpointFromEnv(), WithCollectorEndpointOptionFromEnv()),
WithDisabled(true),
WithDisabledFromEnv(),
WithProcessFromEnv(),
)

assert.NoError(t, err)
assert.Equal(t, exp.o.Disabled, false)
assert.Equal(t, false, exp.o.Disabled)
assert.EqualValues(t, serviceName, exp.process.ServiceName)
assert.Len(t, exp.process.Tags, 1)

require.IsType(t, &collectorUploader{}, exp.uploader)
uploader := exp.uploader.(*collectorUploader)
assert.Equal(t, collectorEndpoint, uploader.endpoint)
assert.Equal(t, username, uploader.username)
assert.Equal(t, password, uploader.password)
}

func TestNewRawExporterWithEnvImplicitly(t *testing.T) {
const (
collectorEndpoint = "http://localhost"
username = "user"
password = "password"
serviceName = "test-service"
disabled = "false"
tags = "key=value"
)

require.NoError(t, os.Setenv(envEndpoint, collectorEndpoint))
require.NoError(t, os.Setenv(envUser, username))
require.NoError(t, os.Setenv(envPassword, password))
require.NoError(t, os.Setenv(envDisabled, disabled))
require.NoError(t, os.Setenv(envServiceName, serviceName))
require.NoError(t, os.Setenv(envTags, tags))
defer func() {
require.NoError(t, os.Unsetenv(envEndpoint))
require.NoError(t, os.Unsetenv(envUser))
require.NoError(t, os.Unsetenv(envPassword))
require.NoError(t, os.Unsetenv(envDisabled))
require.NoError(t, os.Unsetenv(envServiceName))
require.NoError(t, os.Unsetenv(envTags))
}()

// Create Jaeger Exporter with environment variables
exp, err := NewRawExporter(
WithCollectorEndpoint(""),
WithDisabled(true),
)

assert.NoError(t, err)
assert.Equal(t, false, exp.o.Disabled)
assert.EqualValues(t, serviceName, exp.process.ServiceName)
assert.Len(t, exp.process.Tags, 1)

require.IsType(t, &collectorUploader{}, exp.uploader)
uploader := exp.uploader.(*collectorUploader)
assert.Equal(t, collectorEndpoint, uploader.endpoint)
assert.Equal(t, username, uploader.username)
assert.Equal(t, password, uploader.password)
}

func TestCollectorEndpointFromEnv(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions exporters/trace/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func WithDisabled(disabled bool) Option {
// the collected spans to Jaeger.
func NewRawExporter(endpointOption EndpointOption, opts ...Option) (*Exporter, error) {
o := options{}
opts = append(opts, WithDisabledFromEnv(), WithProcessFromEnv())
for _, opt := range opts {
opt(&o)
}
Expand Down
7 changes: 7 additions & 0 deletions exporters/trace/jaeger/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ func WithAgentEndpoint(agentEndpoint string) EndpointOption {
// For example, http://localhost:14268/api/traces
func WithCollectorEndpoint(collectorEndpoint string, options ...CollectorEndpointOption) EndpointOption {
return func() (batchUploader, error) {
// Overwrite collector endpoint if environment variables are available.
if e := CollectorEndpointFromEnv(); e != "" {
collectorEndpoint = e
}

if collectorEndpoint == "" {
return nil, errors.New("collectorEndpoint must not be empty")
}

o := &CollectorEndpointOptions{
httpClient: http.DefaultClient,
}

options = append(options, WithCollectorEndpointOptionFromEnv())
for _, opt := range options {
opt(o)
}
Expand Down

0 comments on commit a2e8905

Please sign in to comment.