Skip to content

Commit

Permalink
ddtrace/tracer: updated DD_TAGS parsing to support space separation (#…
Browse files Browse the repository at this point in the history
…837)

DD_TAGS were split by commas only. Unless there is a comma in a string, tag list will be split by spaces. In either case, commas/spaces will be trimmed.
  • Loading branch information
dianashevchenko committed Feb 10, 2021
1 parent 7a521bb commit c1edb6e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
21 changes: 14 additions & 7 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,26 @@ func newConfig(opts ...StartOption) *config {
c.version = ver
}
if v := os.Getenv("DD_TAGS"); v != "" {
for _, tag := range strings.Split(v, ",") {
sep := " "
if strings.Index(v, ",") > -1 {
// falling back to comma as separator
sep = ","
}
for _, tag := range strings.Split(v, sep) {
tag = strings.TrimSpace(tag)
if tag == "" {
continue
}
kv := strings.SplitN(tag, ":", 2)
k := strings.TrimSpace(kv[0])
switch len(kv) {
case 1:
WithGlobalTag(k, "")(c)
case 2:
WithGlobalTag(k, strings.TrimSpace(kv[1]))(c)
key := strings.TrimSpace(kv[0])
if key == "" {
continue
}
var val string
if len(kv) == 2 {
val = strings.TrimSpace(kv[1])
}
WithGlobalTag(key, val)(c)
}
}
if _, ok := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); ok {
Expand Down
96 changes: 96 additions & 0 deletions ddtrace/tracer/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,102 @@ func TestServiceName(t *testing.T) {
})
}

func TestTagSeparators(t *testing.T) {
assert := assert.New(t)

for _, tag := range []struct {
in string
out map[string]string
}{{
in: "env:test aKey:aVal bKey:bVal cKey:",
out: map[string]string{
"env": "test",
"aKey": "aVal",
"bKey": "bVal",
"cKey": "",
},
},
{
in: "env:test,aKey:aVal,bKey:bVal,cKey:",
out: map[string]string{
"env": "test",
"aKey": "aVal",
"bKey": "bVal",
"cKey": "",
},
},
{
in: "env:test,aKey:aVal bKey:bVal cKey:",
out: map[string]string{
"env": "test",
"aKey": "aVal bKey:bVal cKey:",
},
},
{
in: "env:test bKey :bVal dKey: dVal cKey:",
out: map[string]string{
"env": "test",
"bKey": "",
"dKey": "",
"dVal": "",
"cKey": "",
},
},
{
in: "env :test, aKey : aVal bKey:bVal cKey:",
out: map[string]string{
"env": "test",
"aKey": "aVal bKey:bVal cKey:",
},
},
{
in: "env:keyWithA:Semicolon bKey:bVal cKey",
out: map[string]string{
"env": "keyWithA:Semicolon",
"bKey": "bVal",
"cKey": "",
},
},
{
in: "env:keyWith: , , Lots:Of:Semicolons ",
out: map[string]string{
"env": "keyWith:",
"Lots": "Of:Semicolons",
},
},
{
in: "a:b,c,d",
out: map[string]string{
"a": "b",
"c": "",
"d": "",
},
},
{
in: "a,1",
out: map[string]string{
"a": "",
"1": "",
},
},
{
in: "a:b:c:d",
out: map[string]string{"a": "b:c:d"},
},
} {
t.Run("", func(t *testing.T) {
os.Setenv("DD_TAGS", tag.in)
defer os.Unsetenv("DD_TAGS")
c := newConfig()
for key, expected := range tag.out {
got, ok := c.globalTags[key]
assert.True(ok, "tag not found")
assert.Equal(expected, got)
}
})
}
}

func TestVersionConfig(t *testing.T) {
t.Run("WithServiceVersion", func(t *testing.T) {
assert := assert.New(t)
Expand Down
7 changes: 6 additions & 1 deletion profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ func defaultConfig() *config {
WithVersion(v)(&c)
}
if v := os.Getenv("DD_TAGS"); v != "" {
for _, tag := range strings.Split(v, ",") {
sep := " "
if strings.Index(v, ",") > -1 {
// falling back to comma as separator
sep = ","
}
for _, tag := range strings.Split(v, sep) {
tag = strings.TrimSpace(tag)
if tag == "" {
continue
Expand Down

0 comments on commit c1edb6e

Please sign in to comment.