Skip to content

Commit

Permalink
profiler: add WithSite option and DD_SITE environment variable. (Data…
Browse files Browse the repository at this point in the history
…Dog#644)

WithSite and DD_SITE will determine the apiURL that profiles are sent to.
  • Loading branch information
knusbaum authored and mingrammer committed Dec 22, 2020
1 parent 1b10bce commit 37ef903
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
27 changes: 26 additions & 1 deletion profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ package profiler

import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/internal/log"

"github.com/DataDog/datadog-go/statsd"
)

Expand Down Expand Up @@ -52,6 +55,12 @@ type config struct {
blockRate int
}

func urlForSite(site string) (string, error) {
u := fmt.Sprintf("https://intake.profile.%s/v1/input", site)
_, err := url.Parse(u)
return u, err
}

func (c *config) addProfileType(t ProfileType) {
if c.types == nil {
c.types = make(map[ProfileType]struct{})
Expand All @@ -61,8 +70,8 @@ func (c *config) addProfileType(t ProfileType) {

func defaultConfig() *config {
c := config{
apiURL: defaultAPIURL,
env: defaultEnv,
apiURL: defaultAPIURL,
service: filepath.Base(os.Args[0]),
statsd: &statsd.NoOpClient{},
period: DefaultPeriod,
Expand All @@ -75,6 +84,9 @@ func defaultConfig() *config {
c.addProfileType(t)
}

if v := os.Getenv("DD_SITE"); v != "" {
WithSite(v)(&c)
}
if v := os.Getenv("DD_ENV"); v != "" {
WithEnv(v)(&c)
}
Expand Down Expand Up @@ -174,3 +186,16 @@ func WithStatsd(client StatsdClient) Option {
cfg.statsd = client
}
}

// WithSite specifies the datadog site (datadoghq.com, datadoghq.eu, etc.)
// which profiles will be sent to.
func WithSite(site string) Option {
return func(cfg *config) {
u, err := urlForSite(site)
if err != nil {
log.Error("profiler: invalid site provided, using %s (%s)", defaultAPIURL, err)
return
}
cfg.apiURL = u
}
}
21 changes: 21 additions & 0 deletions profiler/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ func TestOptions(t *testing.T) {
assert.Equal(t, "serviceName", cfg.service)
})

t.Run("WithSite", func(t *testing.T) {
var cfg config
WithSite("datadog.eu")(&cfg)
assert.Equal(t, "https://intake.profile.datadog.eu/v1/input", cfg.apiURL)
})

t.Run("WithSite/override", func(t *testing.T) {
os.Setenv("DD_SITE", "wrong.site")
defer os.Unsetenv("DD_SITE")
cfg := defaultConfig()
WithSite("datadog.eu")(cfg)
assert.Equal(t, "https://intake.profile.datadog.eu/v1/input", cfg.apiURL)
})

t.Run("WithEnv", func(t *testing.T) {
var cfg config
WithEnv("envName")(&cfg)
Expand Down Expand Up @@ -112,6 +126,13 @@ func TestOptions(t *testing.T) {
}

func TestEnvVars(t *testing.T) {
t.Run("DD_SITE", func(t *testing.T) {
os.Setenv("DD_SITE", "datadog.eu")
defer os.Unsetenv("DD_SITE")
cfg := defaultConfig()
assert.Equal(t, "https://intake.profile.datadog.eu/v1/input", cfg.apiURL)
})

t.Run("DD_ENV", func(t *testing.T) {
os.Setenv("DD_ENV", "someEnv")
defer os.Unsetenv("DD_ENV")
Expand Down

0 comments on commit 37ef903

Please sign in to comment.