Skip to content
This repository was archived by the owner on Aug 30, 2019. It is now read-only.

Commit e573587

Browse files
committed
all: merge APIEndpoint into Endpoints
1 parent e35def7 commit e573587

15 files changed

+132
-122
lines changed

api/api_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func newTestReceiverFromConfig(conf *config.AgentConfig) *HTTPReceiver {
4545

4646
func newTestReceiverConfig() *config.AgentConfig {
4747
conf := config.New()
48-
conf.APIKey = "test"
48+
conf.Endpoints[0].APIKey = "test"
4949

5050
return conf
5151
}

cmd/trace-agent/agent_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestWatchdog(t *testing.T) {
2727
}
2828

2929
conf := config.New()
30-
conf.APIKey = "apikey_2"
30+
conf.Endpoints[0].APIKey = "apikey_2"
3131
conf.MaxMemory = 1e7
3232
conf.WatchdogInterval = time.Millisecond
3333

@@ -118,7 +118,7 @@ func TestProcess(t *testing.T) {
118118
// • resulting resource is obfuscated with replacements applied
119119
// • resulting "sql.query" tag is obfuscated with no replacements applied
120120
cfg := config.New()
121-
cfg.APIKey = "test"
121+
cfg.Endpoints[0].APIKey = "test"
122122
cfg.ReplaceTags = []*config.ReplaceRule{{
123123
Name: "resource.name",
124124
Re: regexp.MustCompile("AND.*"),
@@ -144,7 +144,7 @@ func TestProcess(t *testing.T) {
144144

145145
t.Run("Blacklister", func(t *testing.T) {
146146
cfg := config.New()
147-
cfg.APIKey = "test"
147+
cfg.Endpoints[0].APIKey = "test"
148148
cfg.Ignore["resource"] = []string{"^INSERT.*"}
149149
ctx, cancel := context.WithCancel(context.Background())
150150
agent := NewAgent(ctx, cfg)
@@ -178,7 +178,7 @@ func TestProcess(t *testing.T) {
178178

179179
t.Run("Stats/Priority", func(t *testing.T) {
180180
cfg := config.New()
181-
cfg.APIKey = "test"
181+
cfg.Endpoints[0].APIKey = "test"
182182
ctx, cancel := context.WithCancel(context.Background())
183183
agent := NewAgent(ctx, cfg)
184184
defer cancel()
@@ -212,14 +212,14 @@ func TestProcess(t *testing.T) {
212212

213213
func BenchmarkAgentTraceProcessing(b *testing.B) {
214214
c := config.New()
215-
c.APIKey = "test"
215+
c.Endpoints[0].APIKey = "test"
216216

217217
runTraceProcessingBenchmark(b, c)
218218
}
219219

220220
func BenchmarkAgentTraceProcessingWithFiltering(b *testing.B) {
221221
c := config.New()
222-
c.APIKey = "test"
222+
c.Endpoints[0].APIKey = "test"
223223
c.Ignore["resource"] = []string{"[0-9]{3}", "foobar", "G.T [a-z]+", "[^123]+_baz"}
224224

225225
runTraceProcessingBenchmark(b, c)
@@ -229,7 +229,7 @@ func BenchmarkAgentTraceProcessingWithFiltering(b *testing.B) {
229229
// this means we won't compesate the overhead of filtering by dropping traces
230230
func BenchmarkAgentTraceProcessingWithWorstCaseFiltering(b *testing.B) {
231231
c := config.New()
232-
c.APIKey = "test"
232+
c.Endpoints[0].APIKey = "test"
233233
c.Ignore["resource"] = []string{"[0-9]{3}", "foobar", "aaaaa?aaaa", "[^123]+_baz"}
234234

235235
runTraceProcessingBenchmark(b, c)
@@ -250,7 +250,7 @@ func runTraceProcessingBenchmark(b *testing.B, c *config.AgentConfig) {
250250

251251
func BenchmarkWatchdog(b *testing.B) {
252252
conf := config.New()
253-
conf.APIKey = "apikey_2"
253+
conf.Endpoints[0].APIKey = "apikey_2"
254254
ctx, cancelFunc := context.WithCancel(context.Background())
255255
defer cancelFunc()
256256
agent := NewAgent(ctx, conf)

config/agent.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ var (
2626

2727
// Endpoint specifies an endpoint that the trace agent will write data (traces, stats & services) to.
2828
type Endpoint struct {
29-
APIKey string
30-
Host string
29+
APIKey string `json:"-"` // never marshal this
30+
Host string
31+
32+
// NoProxy will be set to true when the proxy setting for the trace API endpoint
33+
// needs to be ignored (e.g. it is part of the "no_proxy" list in the yaml settings).
3134
NoProxy bool
3235
}
3336

@@ -44,10 +47,11 @@ type AgentConfig struct {
4447
DefaultEnv string // the traces will default to this environment
4548
ConfigPath string // the source of this config, if any
4649

47-
// API
48-
APIEndpoint string
49-
AdditionalEndpoints []*Endpoint
50-
APIKey string `json:"-"` // never publish this
50+
// Endpoints specifies the set of hosts and API keys where traces and stats
51+
// will be uploaded to. The first endpoint is the main configuration endpoint;
52+
// any following ones are read from the 'additional_endpoints' parts of the
53+
// configuration file, if present.
54+
Endpoints []*Endpoint
5155

5256
// Concentrator
5357
BucketInterval time.Duration // the size of our pre-aggregation per bucket
@@ -87,10 +91,6 @@ type AgentConfig struct {
8791
ProxyURL *url.URL
8892
SkipSSLValidation bool
8993

90-
// NoProxy will be set to true when the proxy setting for the trace API endpoint
91-
// needs to be ignored (e.g. it is part of the "no_proxy" list in the yaml settings).
92-
NoProxy bool
93-
9494
// filtering
9595
Ignore map[string][]string
9696

@@ -112,10 +112,9 @@ type AgentConfig struct {
112112
// New returns a configuration with the default values.
113113
func New() *AgentConfig {
114114
return &AgentConfig{
115-
Enabled: true,
116-
DefaultEnv: "none",
117-
APIEndpoint: "https://trace.agent.datadoghq.com",
118-
APIKey: "",
115+
Enabled: true,
116+
DefaultEnv: "none",
117+
Endpoints: []*Endpoint{{Host: "https://trace.agent.datadoghq.com"}},
119118

120119
BucketInterval: time.Duration(10) * time.Second,
121120
ExtraAggregators: []string{"http.status_code"},
@@ -171,7 +170,7 @@ func (c *AgentConfig) LoadYaml(path string) error {
171170

172171
// Validate validates if the current configuration is good for the agent to start with.
173172
func (c *AgentConfig) validate() error {
174-
if c.APIKey == "" {
173+
if len(c.Endpoints) == 0 || c.Endpoints[0].APIKey == "" {
175174
return ErrMissingAPIKey
176175
}
177176
if c.Hostname == "" {

config/agent_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestSite(t *testing.T) {
7676
t.Run(name, func(t *testing.T) {
7777
cfg, err := Load(tt.file)
7878
assert.NoError(t, err)
79-
assert.Equal(t, tt.url, cfg.APIEndpoint)
79+
assert.Equal(t, tt.url, cfg.Endpoints[0].Host)
8080
})
8181
}
8282
}
@@ -103,7 +103,7 @@ func TestOnlyEnvConfig(t *testing.T) {
103103

104104
c := New()
105105
c.loadEnv()
106-
assert.Equal(t, "apikey_from_env", c.APIKey)
106+
assert.Equal(t, "apikey_from_env", c.Endpoints[0].APIKey)
107107

108108
os.Setenv("DD_API_KEY", "")
109109
}
@@ -115,7 +115,7 @@ func TestOnlyDDAgentConfig(t *testing.T) {
115115
assert.NoError(err)
116116

117117
assert.Equal("thing", c.Hostname)
118-
assert.Equal("apikey_12", c.APIKey)
118+
assert.Equal("apikey_12", c.Endpoints[0].APIKey)
119119
assert.Equal("0.0.0.0", c.ReceiverHost)
120120
assert.Equal(28125, c.StatsdPort)
121121
assert.Equal("DEBUG", c.LogLevel)
@@ -129,7 +129,7 @@ func TestDDAgentMultiAPIKeys(t *testing.T) {
129129
c, err := loadFile("./testdata/multi_api_keys.ini")
130130
assert.NoError(err)
131131

132-
assert.Equal("foo", c.APIKey)
132+
assert.Equal("foo", c.Endpoints[0].APIKey)
133133
}
134134

135135
func TestFullIniConfig(t *testing.T) {
@@ -138,10 +138,10 @@ func TestFullIniConfig(t *testing.T) {
138138
c, err := loadFile("./testdata/full.ini")
139139
assert.NoError(err)
140140

141-
assert.Equal("api_key_test", c.APIKey)
141+
assert.Equal("api_key_test", c.Endpoints[0].APIKey)
142142
assert.Equal("mymachine", c.Hostname)
143143
assert.Equal("https://user:password@proxy_for_https:1234", c.ProxyURL.String())
144-
assert.Equal("https://datadog.unittests", c.APIEndpoint)
144+
assert.Equal("https://datadog.unittests", c.Endpoints[0].Host)
145145
assert.Equal(false, c.Enabled)
146146
assert.Equal("test", c.DefaultEnv)
147147
assert.Equal(18126, c.ReceiverPort)
@@ -158,21 +158,20 @@ func TestFullYamlConfig(t *testing.T) {
158158
c, err := loadFile("./testdata/full.yaml")
159159
assert.NoError(err)
160160

161-
assert.Equal("api_key_test", c.APIKey)
162161
assert.Equal("mymachine", c.Hostname)
163162
assert.Equal("https://user:password@proxy_for_https:1234", c.ProxyURL.String())
164-
assert.Equal("https://datadog.unittests", c.APIEndpoint)
165163
assert.Equal(false, c.Enabled)
166164
assert.Equal("test", c.DefaultEnv)
167165
assert.Equal(18126, c.ReceiverPort)
168166
assert.Equal(0.5, c.ExtraSampleRate)
169167
assert.Equal(5.0, c.MaxTPS)
170168
assert.Equal("0.0.0.0", c.ReceiverHost)
171169
assert.EqualValues([]*Endpoint{
170+
{Host: "https://datadog.unittests", APIKey: "api_key_test"},
172171
{Host: "https://my1.endpoint.com", APIKey: "apikey1"},
173172
{Host: "https://my1.endpoint.com", APIKey: "apikey2"},
174173
{Host: "https://my2.endpoint.eu", APIKey: "apikey3"},
175-
}, c.AdditionalEndpoints)
174+
}, c.Endpoints)
176175

177176
assert.EqualValues([]string{"/health", "/500"}, c.Ignore["resource"])
178177

@@ -196,7 +195,7 @@ func TestUndocumentedYamlConfig(t *testing.T) {
196195
assert.NoError(err)
197196

198197
assert.Equal("thing", c.Hostname)
199-
assert.Equal("apikey_12", c.APIKey)
198+
assert.Equal("apikey_12", c.Endpoints[0].APIKey)
200199
assert.Equal(0.33, c.ExtraSampleRate)
201200
assert.Equal(100.0, c.MaxTPS)
202201
assert.Equal(25, c.ReceiverPort)

config/merge_env.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ func (c *AgentConfig) loadEnv() {
5454
c.Hostname = v
5555
}
5656

57+
if len(c.Endpoints) == 0 {
58+
c.Endpoints = make([]*Endpoint, 1)
59+
}
5760
site := os.Getenv(envSite)
5861
if site != "" {
59-
c.APIEndpoint = apiEndpointPrefix + site
62+
c.Endpoints[0].Host = apiEndpointPrefix + site
6063
}
6164
if v := os.Getenv(envURL); v != "" {
62-
c.APIEndpoint = v
65+
c.Endpoints[0].Host = v
6366
if site != "" {
6467
log.Infof("'DD_SITE' and 'DD_APM_DD_URL' are both set, using endpoint: %q", v)
6568
}
6669
}
6770

6871
if v := os.Getenv(envAPIKey); v != "" {
69-
vals := strings.Split(v, ",")
70-
for i := range vals {
71-
vals[i] = strings.TrimSpace(vals[i])
72-
}
73-
c.APIKey = vals[0]
72+
v := strings.Split(v, ",")[0]
73+
c.Endpoints[0].APIKey = strings.TrimSpace(v)
7474
}
7575

7676
if v := os.Getenv(envReceiverPort); v != "" {

config/merge_ini.go

+7-16
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
)
1616

1717
func (c *AgentConfig) loadIniConfig(conf *File) {
18-
if conf == nil {
19-
return
18+
if len(c.Endpoints) == 0 {
19+
c.Endpoints = make([]*Endpoint, 1)
2020
}
2121

2222
// [Main] section
@@ -37,7 +37,7 @@ func (c *AgentConfig) loadIniConfig(conf *File) {
3737
}
3838

3939
if v := m.Key("api_key").Strings(","); len(v) != 0 {
40-
c.APIKey = v[0]
40+
c.Endpoints[0].APIKey = v[0]
4141
} else {
4242
log.Error("Failed to parse api_key from dd-agent config")
4343
}
@@ -77,23 +77,14 @@ func (c *AgentConfig) loadIniConfig(conf *File) {
7777
}
7878

7979
// [trace.api] section
80-
// TODO: deprecate this old api_key location
8180
if v, _ := conf.Get("trace.api", "api_key"); v != "" {
82-
vals := strings.Split(v, ",")
83-
for i := range vals {
84-
vals[i] = strings.TrimSpace(vals[i])
85-
}
86-
c.APIKey = vals[0]
81+
v := strings.Split(v, ",")[0] // take the first
82+
c.Endpoints[0].APIKey = strings.TrimSpace(v)
8783
}
8884
// undocumented
8985
if v, _ := conf.Get("trace.api", "endpoint"); v != "" {
90-
vals := strings.Split(v, ",")
91-
for i := range vals {
92-
vals[i] = strings.TrimSpace(vals[i])
93-
}
94-
95-
// Takes the first endpoint
96-
c.APIEndpoint = vals[0]
86+
v := strings.Split(v, ",")[0]
87+
c.Endpoints[0].Host = strings.TrimSpace(v) // take the first
9788
}
9889

9990
// [trace.config] section

config/merge_yaml.go

+18-19
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ func NewYaml(configPath string) (*YamlAgentConfig, error) {
186186
}
187187

188188
func (c *AgentConfig) loadYamlConfig(yc *YamlAgentConfig) {
189-
if yc == nil {
190-
return
189+
if len(c.Endpoints) == 0 {
190+
c.Endpoints = make([]*Endpoint, 1)
191191
}
192192

193193
if yc.APIKey != "" {
194-
c.APIKey = yc.APIKey
194+
c.Endpoints[0].APIKey = yc.APIKey
195195
}
196196
if yc.HostName != "" {
197197
c.Hostname = yc.HostName
@@ -202,36 +202,35 @@ func (c *AgentConfig) loadYamlConfig(yc *YamlAgentConfig) {
202202
if yc.StatsdPort > 0 {
203203
c.StatsdPort = yc.StatsdPort
204204
}
205+
205206
if yc.Site != "" {
206-
c.APIEndpoint = apiEndpointPrefix + yc.Site
207+
c.Endpoints[0].Host = apiEndpointPrefix + yc.Site
207208
}
208-
if yc.TraceAgent.Endpoint != "" {
209-
c.APIEndpoint = yc.TraceAgent.Endpoint
209+
if host := yc.TraceAgent.Endpoint; host != "" {
210+
c.Endpoints[0].Host = host
210211
if yc.Site != "" {
211-
log.Infof("'site' and 'apm_dd_url' are both set, using endpoint: %q", c.APIEndpoint)
212+
log.Infof("'site' and 'apm_dd_url' are both set, using endpoint: %q", host)
212213
}
213214
}
214-
noProxy := make(map[string]bool, len(yc.Proxy.NoProxy))
215-
for _, host := range yc.Proxy.NoProxy {
216-
noProxy[host] = true
217-
}
218215
for url, keys := range yc.TraceAgent.AdditionalEndpoints {
219216
if len(keys) == 0 {
220217
log.Errorf("'additional_endpoints' entries must have at least one API key present")
221218
continue
222219
}
223220
for _, key := range keys {
224-
c.AdditionalEndpoints = append(c.AdditionalEndpoints, &Endpoint{
225-
Host: url,
226-
APIKey: key,
227-
NoProxy: noProxy[url],
228-
})
221+
c.Endpoints = append(c.Endpoints, &Endpoint{Host: url, APIKey: key})
229222
}
230223
}
231-
if host, ok := noProxy[c.APIEndpoint]; ok {
232-
log.Infof("Trace Agent main endpoint matches `proxy.no_proxy` list item %q: ignoring proxy", host)
233-
c.NoProxy = true
224+
225+
noProxy := make(map[string]bool, len(yc.Proxy.NoProxy))
226+
for _, host := range yc.Proxy.NoProxy {
227+
// map of hosts that need to be skipped by proxy
228+
noProxy[host] = true
234229
}
230+
for _, e := range c.Endpoints {
231+
e.NoProxy = noProxy[e.Host]
232+
}
233+
235234
if yc.Proxy.HTTPS != "" {
236235
url, err := url.Parse(yc.Proxy.HTTPS)
237236
if err == nil {

0 commit comments

Comments
 (0)