From 92f0cc3586d9bd6070c50328bd623c9985db1ba5 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Thu, 4 Nov 2021 08:54:38 -0700 Subject: [PATCH] [datadogexporter]: Do not rely on collector to resolve envvar when possible to resolve them (#6122) Signed-off-by: Bogdan Drutu --- exporter/datadogexporter/factory.go | 19 ++++++------ exporter/datadogexporter/factory_test.go | 38 ++++++++++++++++++------ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/exporter/datadogexporter/factory.go b/exporter/datadogexporter/factory.go index 716483e2bbfd..1ae5f1a41767 100644 --- a/exporter/datadogexporter/factory.go +++ b/exporter/datadogexporter/factory.go @@ -16,6 +16,7 @@ package datadogexporter import ( "context" + "os" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" @@ -53,21 +54,21 @@ func createDefaultConfig() config.Exporter { QueueSettings: exporterhelper.DefaultQueueSettings(), API: ddconfig.APIConfig{ - Key: "$DD_API_KEY", // Must be set if using API - Site: "$DD_SITE", // If not provided, set during config sanitization + Key: os.Getenv("DD_API_KEY"), // Must be set if using API + Site: os.Getenv("DD_SITE"), // If not provided, set during config sanitization }, TagsConfig: ddconfig.TagsConfig{ - Hostname: "$DD_HOST", - Env: "$DD_ENV", - Service: "$DD_SERVICE", - Version: "$DD_VERSION", - EnvVarTags: "$DD_TAGS", // Only taken into account if Tags is not set + Hostname: os.Getenv("DD_HOST"), + Env: os.Getenv("DD_ENV"), + Service: os.Getenv("DD_SERVICE"), + Version: os.Getenv("DD_VERSION"), + EnvVarTags: os.Getenv("DD_TAGS"), // Only taken into account if Tags is not set }, Metrics: ddconfig.MetricsConfig{ TCPAddr: confignet.TCPAddr{ - Endpoint: "$DD_URL", // If not provided, set during config sanitization + Endpoint: os.Getenv("DD_URL"), // If not provided, set during config sanitization }, SendMonotonic: true, DeltaTTL: 3600, @@ -85,7 +86,7 @@ func createDefaultConfig() config.Exporter { Traces: ddconfig.TracesConfig{ SampleRate: 1, TCPAddr: confignet.TCPAddr{ - Endpoint: "$DD_APM_URL", // If not provided, set during config sanitization + Endpoint: os.Getenv("DD_APM_URL"), // If not provided, set during config sanitization }, IgnoreResources: []string{}, }, diff --git a/exporter/datadogexporter/factory_test.go b/exporter/datadogexporter/factory_test.go index 08c24d5e2993..f20700d956e6 100644 --- a/exporter/datadogexporter/factory_test.go +++ b/exporter/datadogexporter/factory_test.go @@ -37,6 +37,26 @@ import ( // Test that the factory creates the default configuration func TestCreateDefaultConfig(t *testing.T) { + assert.NoError(t, os.Setenv("DD_API_KEY", "API_KEY")) + assert.NoError(t, os.Setenv("DD_SITE", "SITE")) + assert.NoError(t, os.Setenv("DD_URL", "URL")) + assert.NoError(t, os.Setenv("DD_APM_URL", "APM_URL")) + assert.NoError(t, os.Setenv("DD_HOST", "HOST")) + assert.NoError(t, os.Setenv("DD_ENV", "ENV")) + assert.NoError(t, os.Setenv("DD_SERVICE", "SERVICE")) + assert.NoError(t, os.Setenv("DD_VERSION", "VERSION")) + assert.NoError(t, os.Setenv("DD_TAGS", "TAGS")) + defer func() { + assert.NoError(t, os.Unsetenv("DD_API_KEY")) + assert.NoError(t, os.Unsetenv("DD_SITE")) + assert.NoError(t, os.Unsetenv("DD_URL")) + assert.NoError(t, os.Unsetenv("DD_APM_URL")) + assert.NoError(t, os.Unsetenv("DD_HOST")) + assert.NoError(t, os.Unsetenv("DD_ENV")) + assert.NoError(t, os.Unsetenv("DD_SERVICE")) + assert.NoError(t, os.Unsetenv("DD_VERSION")) + assert.NoError(t, os.Unsetenv("DD_TAGS")) + }() factory := NewFactory() cfg := factory.CreateDefaultConfig() @@ -49,13 +69,13 @@ func TestCreateDefaultConfig(t *testing.T) { QueueSettings: exporterhelper.DefaultQueueSettings(), API: ddconfig.APIConfig{ - Key: "$DD_API_KEY", - Site: "$DD_SITE", + Key: "API_KEY", + Site: "SITE", }, Metrics: ddconfig.MetricsConfig{ TCPAddr: confignet.TCPAddr{ - Endpoint: "$DD_URL", + Endpoint: "URL", }, DeltaTTL: 3600, SendMonotonic: true, @@ -69,17 +89,17 @@ func TestCreateDefaultConfig(t *testing.T) { Traces: ddconfig.TracesConfig{ SampleRate: 1, TCPAddr: confignet.TCPAddr{ - Endpoint: "$DD_APM_URL", + Endpoint: "APM_URL", }, IgnoreResources: []string{}, }, TagsConfig: ddconfig.TagsConfig{ - Hostname: "$DD_HOST", - Env: "$DD_ENV", - Service: "$DD_SERVICE", - Version: "$DD_VERSION", - EnvVarTags: "$DD_TAGS", + Hostname: "HOST", + Env: "ENV", + Service: "SERVICE", + Version: "VERSION", + EnvVarTags: "TAGS", }, SendMetadata: true,