-
Notifications
You must be signed in to change notification settings - Fork 2k
/
config_yaml.go
187 lines (154 loc) · 4.98 KB
/
config_yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package jaeger
import (
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"gopkg.in/yaml.v2"
)
// Config - YAML configuration. For details see to https://github.com/jaegertracing/jaeger-client-go#environment-variables.
type Config struct {
ServiceName string `yaml:"service_name"`
Disabled bool `yaml:"disabled"`
RPCMetrics bool `yaml:"rpc_metrics"`
Tags string `yaml:"tags"`
SamplerType string `yaml:"sampler_type"`
SamplerParam float64 `yaml:"sampler_param"`
SamplerManagerHostPort string `yaml:"sampler_manager_host_port"`
SamplerMaxOperations int `yaml:"sampler_max_operations"`
SamplerRefreshInterval time.Duration `yaml:"sampler_refresh_interval"`
ReporterMaxQueueSize int `yaml:"reporter_max_queue_size"`
ReporterFlushInterval time.Duration `yaml:"reporter_flush_interval"`
ReporterLogSpans bool `yaml:"reporter_log_spans"`
Endpoint string `yaml:"endpoint"`
User string `yaml:"user"`
Password string `yaml:"password"`
AgentHost string `yaml:"agent_host"`
AgentPort int `yaml:"agent_port"`
Gen128Bit bool `yaml:"traceid_128bit"`
}
// ParseConfigFromYaml uses config YAML to set the tracer's Configuration.
func ParseConfigFromYaml(cfg []byte) (*config.Configuration, error) {
conf := &Config{}
if err := yaml.Unmarshal(cfg, &conf); err != nil {
return nil, err
}
c := &config.Configuration{}
if conf.ServiceName != "" {
c.ServiceName = conf.ServiceName
}
if conf.RPCMetrics {
c.RPCMetrics = conf.RPCMetrics
}
if conf.Gen128Bit {
c.Gen128Bit = conf.Gen128Bit
}
if conf.Disabled {
c.Disabled = conf.Disabled
}
if conf.Tags != "" {
c.Tags = parseTags(conf.Tags)
}
c.Sampler = samplerConfigFromConfig(*conf)
if r, err := reporterConfigFromConfig(*conf); err == nil {
c.Reporter = r
} else {
return nil, errors.Wrap(err, "cannot obtain reporter config from YAML")
}
return c, nil
}
// samplerConfigFromConfig creates a new SamplerConfig based on the YAML Config.
func samplerConfigFromConfig(cfg Config) *config.SamplerConfig {
sc := &config.SamplerConfig{}
if cfg.SamplerType != "" {
sc.Type = cfg.SamplerType
}
if cfg.SamplerParam != 0 {
sc.Param = cfg.SamplerParam
}
if cfg.SamplerManagerHostPort != "" {
sc.SamplingServerURL = cfg.SamplerManagerHostPort
}
if cfg.SamplerMaxOperations != 0 {
sc.MaxOperations = cfg.SamplerMaxOperations
}
if cfg.SamplerRefreshInterval != 0 {
sc.SamplingRefreshInterval = cfg.SamplerRefreshInterval
}
return sc
}
// reporterConfigFromConfig creates a new ReporterConfig based on the YAML Config.
func reporterConfigFromConfig(cfg Config) (*config.ReporterConfig, error) {
rc := &config.ReporterConfig{}
if cfg.ReporterMaxQueueSize != 0 {
rc.QueueSize = cfg.ReporterMaxQueueSize
}
if cfg.ReporterFlushInterval != 0 {
rc.BufferFlushInterval = cfg.ReporterFlushInterval
}
if cfg.ReporterLogSpans {
rc.LogSpans = cfg.ReporterLogSpans
}
if cfg.Endpoint != "" {
u, err := url.ParseRequestURI(cfg.Endpoint)
if err != nil {
return nil, errors.Wrapf(err, "cannot parse endpoint=%s", cfg.Endpoint)
}
rc.CollectorEndpoint = u.String()
user := cfg.User
pswd := cfg.Password
if user != "" && pswd == "" || user == "" && pswd != "" {
return nil, errors.Errorf("you must set %s and %s parameters together", cfg.User, cfg.Password)
}
rc.User = user
rc.Password = pswd
} else {
host := jaeger.DefaultUDPSpanServerHost
if cfg.AgentHost != "" {
host = cfg.AgentHost
}
port := jaeger.DefaultUDPSpanServerPort
if cfg.AgentPort != 0 {
port = cfg.AgentPort
}
rc.LocalAgentHostPort = net.JoinHostPort(host, strconv.Itoa(port))
}
return rc, nil
}
// parseTags parses the given string into a collection of Tags.
// Spec for this value:
// - comma separated list of key=value
// - value can be specified using the notation ${envVar:defaultValue}, where `envVar`
// is an environment variable and `defaultValue` is the value to use in case the env var is not set.
func parseTags(sTags string) []opentracing.Tag {
pairs := strings.Split(sTags, ",")
tags := make([]opentracing.Tag, 0)
for _, p := range pairs {
fmt.Printf("\n\n p is: %s", p)
kv := strings.SplitN(p, "=", 2)
if len(kv) < 2 {
continue // to avoid panic
}
k, v := strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1])
if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
ed := strings.SplitN(v[2:len(v)-1], ":", 2)
e, d := ed[0], ed[1]
v = os.Getenv(e)
if v == "" && d != "" {
v = d
}
}
tag := opentracing.Tag{Key: k, Value: v}
tags = append(tags, tag)
}
return tags
}