|
1 | 1 | package jaeger
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "io"
|
5 |
| - "log" |
| 6 | + "os" |
6 | 7 | "strings"
|
7 | 8 | "time"
|
8 | 9 |
|
9 | 10 | "flag"
|
10 | 11 |
|
11 | 12 | "github.com/grafana/globalconf"
|
12 | 13 | opentracing "github.com/opentracing/opentracing-go"
|
| 14 | + log "github.com/sirupsen/logrus" |
13 | 15 | jaegercfg "github.com/uber/jaeger-client-go/config"
|
14 | 16 | jaegerlog "github.com/uber/jaeger-client-go/log"
|
15 | 17 | )
|
@@ -52,18 +54,51 @@ func ConfigSetup() {
|
52 | 54 | }
|
53 | 55 |
|
54 | 56 | func ConfigProcess() {
|
55 |
| - addTagsRaw = strings.TrimSpace(addTagsRaw) |
56 |
| - if len(addTagsRaw) == 0 { |
57 |
| - return |
| 57 | + var err error |
| 58 | + addTagsParsed, err = parseTags(addTagsRaw) |
| 59 | + if err != nil { |
| 60 | + log.Fatalf("jaeger: Config validation error. %s", err) |
58 | 61 | }
|
59 |
| - tagSpecs := strings.Split(addTagsRaw, ",") |
60 |
| - for _, tagSpec := range tagSpecs { |
61 |
| - split := strings.Split(tagSpec, "=") |
62 |
| - if len(split) != 2 { |
63 |
| - log.Fatalf("cannot parse add-tags value %q", tagSpec) |
| 62 | +} |
| 63 | + |
| 64 | +// parseTags parses the given string into a slice of opentracing.Tag. |
| 65 | +// the string must be a comma separated list of key=value pairs, where value |
| 66 | +// can be specified as ${key:default}, where `key` is an environment |
| 67 | +// variable and `default` is the value to use in case the env var is not set |
| 68 | +func parseTags(input string) ([]opentracing.Tag, error) { |
| 69 | + pairs := strings.Split(input, ",") |
| 70 | + var tags []opentracing.Tag |
| 71 | + for _, pair := range pairs { |
| 72 | + if pair == "" { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + if !strings.Contains(pair, "=") { |
| 77 | + return nil, fmt.Errorf("invalid tag specifier %q", pair) |
64 | 78 | }
|
65 |
| - addTagsParsed = append(addTagsParsed, opentracing.Tag{Key: split[0], Value: split[1]}) |
| 79 | + kv := strings.SplitN(pair, "=", 2) |
| 80 | + key, val := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1]) |
| 81 | + if len(key) == 0 || len(val) == 0 { |
| 82 | + return nil, fmt.Errorf("invalid tag specifier %q", pair) |
| 83 | + } |
| 84 | + |
| 85 | + if strings.HasPrefix(val, "${") && strings.HasSuffix(val, "}") { |
| 86 | + spec := strings.SplitN(val[2:len(val)-1], ":", 2) |
| 87 | + envVar := spec[0] |
| 88 | + var envDefault string |
| 89 | + if len(spec) == 2 { |
| 90 | + envDefault = spec[1] |
| 91 | + } |
| 92 | + val = os.Getenv(envVar) |
| 93 | + if val == "" && envDefault != "" { |
| 94 | + val = envDefault |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + tags = append(tags, opentracing.Tag{Key: key, Value: val}) |
66 | 99 | }
|
| 100 | + |
| 101 | + return tags, nil |
67 | 102 | }
|
68 | 103 |
|
69 | 104 | // Get() returns a jaeger tracer
|
|
0 commit comments