Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssi): properly parse lib versions from env var #34645

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,103 @@ func TestNewInstrumentationConfig(t *testing.T) {
}
}

func TestLibVersionsEnvVar(t *testing.T) {
tests := []struct {
name string
expected map[string]string
}{
{
name: "valid lib versions",
expected: map[string]string{
"python": "1",
"js": "2",
"java": "3",
},
},
{
name: "empty",
expected: map[string]string{},
},
{
name: "nil",
expected: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.expected)
require.NoError(t, err)
t.Setenv("DD_APM_INSTRUMENTATION_LIB_VERSIONS", string(data))
actual, err := NewInstrumentationConfig(configmock.New(t))
require.NoError(t, err)
require.Equal(t, tt.expected, actual.LibVersions)
})
}
}

func TestEnabledNamespacesEnvVar(t *testing.T) {
tests := []struct {
name string
expected []string
}{
{
name: "valid namespaces",
expected: []string{"foo", "bar"},
},
{
name: "empty",
expected: []string{},
},
{
name: "nil",
expected: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.expected)
require.NoError(t, err)
t.Setenv("DD_APM_INSTRUMENTATION_ENABLED_NAMESPACES", string(data))
actual, err := NewInstrumentationConfig(configmock.New(t))
require.NoError(t, err)
require.Equal(t, tt.expected, actual.EnabledNamespaces)
})
}
}

func TestDisabledNamespacesEnvVar(t *testing.T) {
tests := []struct {
name string
expected []string
}{
{
name: "valid namespaces",
expected: []string{"default", "kube-system"},
},
{
name: "empty",
expected: []string{},
},
{
name: "nil",
expected: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.expected)
require.NoError(t, err)
t.Setenv("DD_APM_INSTRUMENTATION_DISABLED_NAMESPACES", string(data))
actual, err := NewInstrumentationConfig(configmock.New(t))
require.NoError(t, err)
require.Equal(t, tt.expected, actual.DisabledNamespaces)
})
}
}

func TestTargetEnvVar(t *testing.T) {
tests := []struct {
name string
Expand Down
21 changes: 21 additions & 0 deletions pkg/config/setup/apm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,29 @@ func setupAPM(config pkgconfigmodel.Setup) {
config.BindEnvAndSetDefault("apm_config.compute_stats_by_span_kind", true, "DD_APM_COMPUTE_STATS_BY_SPAN_KIND") //nolint:errcheck
config.BindEnvAndSetDefault("apm_config.instrumentation.enabled", false, "DD_APM_INSTRUMENTATION_ENABLED")
config.BindEnvAndSetDefault("apm_config.instrumentation.enabled_namespaces", []string{}, "DD_APM_INSTRUMENTATION_ENABLED_NAMESPACES")
config.ParseEnvAsStringSlice("apm_config.instrumentation.enabled_namespaces", func(in string) []string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me. From the config's perspective, env vars always have string values, and since the value in the helm chart is converted to json, this handler will properly parse it to a slice of strings.

var mappings []string
if err := json.Unmarshal([]byte(in), &mappings); err != nil {
log.Errorf(`"apm_config.instrumentation.enabled_namespaces" can not be parsed: %v`, err)
}
return mappings
})
config.BindEnvAndSetDefault("apm_config.instrumentation.disabled_namespaces", []string{}, "DD_APM_INSTRUMENTATION_DISABLED_NAMESPACES")
config.ParseEnvAsStringSlice("apm_config.instrumentation.disabled_namespaces", func(in string) []string {
var mappings []string
if err := json.Unmarshal([]byte(in), &mappings); err != nil {
log.Errorf(`"apm_config.instrumentation.disabled_namespaces" can not be parsed: %v`, err)
}
return mappings
})
config.BindEnvAndSetDefault("apm_config.instrumentation.lib_versions", map[string]string{}, "DD_APM_INSTRUMENTATION_LIB_VERSIONS")
config.ParseEnvAsMapStringInterface("apm_config.instrumentation.lib_versions", func(in string) map[string]interface{} {
var mappings map[string]interface{}
if err := json.Unmarshal([]byte(in), &mappings); err != nil {
log.Errorf(`"apm_config.instrumentation.lib_versions" can not be parsed: %v`, err)
}
return mappings
})
// Note(stanistan): The flag "DD_APM_INSTRUMENTATION_VERSION"
// will remain undocumented for the duration of the beta.
// We intend to only switch back to v1 if beta customers have issues
Expand Down
Loading