From a2e89054841d7176b41952bfabc0c21f48f413a6 Mon Sep 17 00:00:00 2001 From: Sam Xie Date: Sat, 6 Jun 2020 16:42:24 +0800 Subject: [PATCH] Use environment variables as default options --- exporters/trace/jaeger/env_test.go | 54 ++++++++++++++++++++++++++++-- exporters/trace/jaeger/jaeger.go | 1 + exporters/trace/jaeger/uploader.go | 7 ++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/exporters/trace/jaeger/env_test.go b/exporters/trace/jaeger/env_test.go index ee383298c21e..5af9c1a23fa3 100644 --- a/exporters/trace/jaeger/env_test.go +++ b/exporters/trace/jaeger/env_test.go @@ -123,7 +123,7 @@ func TestNewRawExporterWithEnv(t *testing.T) { username = "user" password = "password" serviceName = "test-service" - disabled = "disable" + disabled = "false" tags = "key=value" ) @@ -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) { diff --git a/exporters/trace/jaeger/jaeger.go b/exporters/trace/jaeger/jaeger.go index 7fe1ba43d33d..0932b3abc291 100644 --- a/exporters/trace/jaeger/jaeger.go +++ b/exporters/trace/jaeger/jaeger.go @@ -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) } diff --git a/exporters/trace/jaeger/uploader.go b/exporters/trace/jaeger/uploader.go index 158d02d477c9..3f3f848c55f5 100644 --- a/exporters/trace/jaeger/uploader.go +++ b/exporters/trace/jaeger/uploader.go @@ -55,6 +55,11 @@ 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") } @@ -62,6 +67,8 @@ func WithCollectorEndpoint(collectorEndpoint string, options ...CollectorEndpoin o := &CollectorEndpointOptions{ httpClient: http.DefaultClient, } + + options = append(options, WithCollectorEndpointOptionFromEnv()) for _, opt := range options { opt(o) }