Skip to content

Commit d9472a0

Browse files
authored
profiler: Disable agentless mode for WithAPIKey (#906)
This is a backwards incompatible change, but deemed neccessary by the profiling team. Since it's been analyzed to only impact a tiny minority of users, it's done without a major version change. UPGRADING: If you are currently using profiler.WithAPIKey() or DD_API_KEY to upload profiling data, please remove this configuration. dd-trace-go will now default to upload through the datadog agent running at localhost:8126. If your agent is running on another port, please use profiler.WithAgentAddr() to configure the correct agent address. The new profiler.WithAgentlessUpload() is currently not supported for anything other than debugging/testing when hitting integration issues. If you think you need it for your production environment, please contact our support. BACKGROUND: Historically the profiler only supported direct uploading to the backend without using an agent, aka "agentless mode". This integration method was succeeded by agent based uploading which should always be the default behavior by now. The old agentless mode is not officially supported anymore and only meant for testing and debugging at this point. It may make a comeback in the future, but for now it's a second-class citizen. until this patch setting an API Key via WithAPIKey or DD_API_KEY still allowed users to opt into the agentless mode, but this was problematic for two reasons: 1. A DD_API_KEY might be set in the environment for unrelated reasons or by accident. 2. The user may have forgotten switch their integration to agent uploading. Users were warned against agentless uploading via a banner that was shown from July 2020 to February 2021 and various email campaigns. It is now time to make sure nobody is uploading via agentless mode in production anymore.
1 parent 37a411d commit d9472a0

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

profiler/options.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919
"unicode"
2020

21+
"gopkg.in/DataDog/dd-trace-go.v1/internal"
2122
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
2223
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
2324
"gopkg.in/DataDog/dd-trace-go.v1/internal/version"
@@ -77,7 +78,8 @@ var defaultClient = &http.Client{
7778
var defaultProfileTypes = []ProfileType{MetricsProfile, CPUProfile, HeapProfile}
7879

7980
type config struct {
80-
apiKey string
81+
apiKey string
82+
agentless bool
8183
// targetURL is the upload destination URL. It will be set by the profiler on start to either apiURL or agentURL
8284
// based on the other options.
8385
targetURL string
@@ -158,6 +160,9 @@ func defaultConfig() (*config, error) {
158160
if v := os.Getenv("DD_API_KEY"); v != "" {
159161
WithAPIKey(v)(&c)
160162
}
163+
if internal.BoolEnv("DD_PROFILING_AGENTLESS", false) {
164+
WithAgentlessUpload()(&c)
165+
}
161166
if v := os.Getenv("DD_SITE"); v != "" {
162167
WithSite(v)(&c)
163168
}
@@ -209,15 +214,29 @@ func WithAgentAddr(hostport string) Option {
209214
}
210215
}
211216

212-
// WithAPIKey is deprecated and might be removed in future versions of this
213-
// package. It allows to skip the agent and talk to the Datadog API directly
214-
// using the provided API key.
217+
// WithAPIKey sets the Datadog API Key and takes precedence over the DD_API_KEY
218+
// env variable. Historically this option was used to enable agentless
219+
// uploading, but as of dd-trace-go v1.30.0 the behavior has changed to always
220+
// default to agent based uploading which doesn't require an API key. So if you
221+
// currently don't have an agent running on the default localhost:8126 hostport
222+
// you need to set it up, or use WithAgentAddr to specify the hostport location
223+
// of the agent. See WithAgentlessUpload for more information.
215224
func WithAPIKey(key string) Option {
216225
return func(cfg *config) {
217226
cfg.apiKey = key
218227
}
219228
}
220229

230+
// WithAgentlessUpload is currently for internal usage only and not officially
231+
// supported. You should not enable it unless somebody at Datadog instructed
232+
// you to do so. It allows to skip the agent and talk to the Datadog API
233+
// directly using the provided API key.
234+
func WithAgentlessUpload() Option {
235+
return func(cfg *config) {
236+
cfg.agentless = true
237+
}
238+
}
239+
221240
// WithURL specifies the HTTP URL for the Datadog Profiling API.
222241
func WithURL(url string) Option {
223242
return func(cfg *config) {

profiler/profiler.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,25 @@ func newProfiler(opts ...Option) (*profiler, error) {
7878
if os.Getenv("DD_PROFILING_WAIT_PROFILE") != "" {
7979
cfg.addProfileType(expGoroutineWaitProfile)
8080
}
81-
if cfg.apiKey != "" {
81+
// Agentless upload is disabled by default as of v1.30.0, but
82+
// WithAgentlessUpload can be used to enable it for testing and debugging.
83+
if cfg.agentless {
8284
if !isAPIKeyValid(cfg.apiKey) {
83-
return nil, errors.New("API key has incorrect format")
85+
return nil, errors.New("profiler.WithAgentlessUpload requires a valid API key. Use profiler.WithAPIKey or the DD_API_KEY env variable to set it")
8486
}
87+
// Always warn people against using this mode for now. All customers should
88+
// use agent based uploading at this point.
89+
log.Warn("profiler.WithAgentlessUpload is currently for internal usage only and not officially supported.")
8590
cfg.targetURL = cfg.apiURL
8691
} else {
92+
// Historically people could use an API Key to enable agentless uploading.
93+
// As of v1.30.0 customers the default behavior is to use agent based
94+
// uploading regardless of the presence of an API key. So if we see an API
95+
// key configured, we warn the customers that this is probably a
96+
// misconfiguration.
97+
if cfg.apiKey != "" {
98+
log.Warn("You are currently setting profiler.WithAPIKey or the DD_API_KEY env variable, but as of dd-trace-go v1.30.0 this value is getting ignored by the profiler. Please see the profiler.WithAPIKey go docs and verify that your integration is still working. If you can't remove DD_API_KEY from your environment, you can use WithAPIKey(\"\") to silence this warning.")
99+
}
87100
cfg.targetURL = cfg.agentURL
88101
}
89102
if cfg.hostname == "" {

profiler/profiler_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,25 @@ func TestStart(t *testing.T) {
5858
mu.Unlock()
5959
})
6060

61-
t.Run("options/GoodAPIKey", func(t *testing.T) {
61+
t.Run("options/GoodAPIKey/Agent", func(t *testing.T) {
6262
err := Start(WithAPIKey("12345678901234567890123456789012"))
6363
defer Stop()
6464
assert.Nil(t, err)
65+
assert.Equal(t, activeProfiler.cfg.agentURL, activeProfiler.cfg.targetURL)
66+
})
67+
68+
t.Run("options/GoodAPIKey/Agentless", func(t *testing.T) {
69+
err := Start(
70+
WithAPIKey("12345678901234567890123456789012"),
71+
WithAgentlessUpload(),
72+
)
73+
defer Stop()
74+
assert.Nil(t, err)
75+
assert.Equal(t, activeProfiler.cfg.apiURL, activeProfiler.cfg.targetURL)
6576
})
6677

6778
t.Run("options/BadAPIKey", func(t *testing.T) {
68-
err := Start(WithAPIKey("aaaa"))
79+
err := Start(WithAPIKey("aaaa"), WithAgentlessUpload())
6980
defer Stop()
7081
assert.NotNil(t, err)
7182

0 commit comments

Comments
 (0)