Skip to content

Commit

Permalink
Set an empty value when a default env var value is missing for agent …
Browse files Browse the repository at this point in the history
…tags pair (jaegertracing#1777)

* Set an empty value when a default env var value is missing

Signed-off-by: Juraci Paixão Kröhling <[email protected]>

* Skip value when default isn't specified

Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling authored and bookmoons committed Sep 10, 2019
1 parent c929ac0 commit f463471
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 13 additions & 0 deletions cmd/agent/app/reporter/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,25 @@ func parseAgentTags(agentTags string) map[string]string {
k, v := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1])

if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
skipWhenEmpty := false

ed := strings.SplitN(string(v[2:len(v)-1]), ":", 2)
if len(ed) == 1 {
// no default value specified, set to empty
skipWhenEmpty = true
ed = append(ed, "")
}

e, d := ed[0], ed[1]
v = os.Getenv(e)
if v == "" && d != "" {
v = d
}

// no value is set, skip this entry
if v == "" && skipWhenEmpty {
continue
}
}

tags[k] = v
Expand Down
20 changes: 18 additions & 2 deletions cmd/agent/app/reporter/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package reporter

import (
"flag"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -52,23 +53,38 @@ func TestBindFlags(t *testing.T) {
command.PersistentFlags().AddGoFlagSet(flags)
v.BindPFlags(command.PersistentFlags())

jaegerTags := fmt.Sprintf("%s,%s,%s,%s,%s,%s",
"key=value",
"envVar1=${envKey1:defaultVal1}",
"envVar2=${envKey2:defaultVal2}",
"envVar3=${envKey3}",
"envVar4=${envKey4}",
"envVar5=${envVar5:}",
)

err := command.ParseFlags([]string{
"--reporter.type=grpc",
"--jaeger.tags=key=value,envVar1=${envKey1:defaultVal1},envVar2=${envKey2:defaultVal2}",
"--jaeger.tags=" + jaegerTags,
})
require.NoError(t, err)

b := &Options{}
os.Setenv("envKey1", "envVal1")
defer os.Unsetenv("envKey1")

os.Setenv("envKey4", "envVal4")
defer os.Unsetenv("envKey4")

b.InitFromViper(v)

expectedTags := map[string]string{
"key": "value",
"envVar1": "envVal1",
"envVar2": "defaultVal2",
"envVar4": "envVal4",
"envVar5": "",
}

assert.Equal(t, Type("grpc"), b.ReporterType)
assert.Equal(t, expectedTags, b.AgentTags)
os.Unsetenv("envKey1")
}

0 comments on commit f463471

Please sign in to comment.