Skip to content

Commit

Permalink
fix(ssi): properly parse lib versions from env var (#34645)
Browse files Browse the repository at this point in the history
  • Loading branch information
betterengineering authored Mar 3, 2025
1 parent 78c8d30 commit 7197664
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
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 {
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

0 comments on commit 7197664

Please sign in to comment.