Skip to content

Commit

Permalink
profiler: remove all environment variables
Browse files Browse the repository at this point in the history
This change temporarily removes environment variables until we decide
for a standardized naming.
  • Loading branch information
gbbr committed Feb 21, 2020
1 parent 57a0fd9 commit 24c1643
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 87 deletions.
4 changes: 2 additions & 2 deletions profiler/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
// Copyright 2016-2020 Datadog, Inc.

// Package profiler periodically collects and sends profiles to the Datadog API. Use
// Start to start the profiler. An API key needs to be specified by means of an option
// or the environment variable DD_API_KEY.
// Start to start the profiler. An API key needs to be specified by means of the WithAPIKey
// option.
package profiler
23 changes: 2 additions & 21 deletions profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)

Expand Down Expand Up @@ -72,26 +71,8 @@ func defaultConfig() *config {
for _, t := range defaultProfileTypes {
c.addProfileType(t)
}
if v := os.Getenv("DD_API_KEY"); v != "" {
c.apiKey = v
}
if v := os.Getenv("DD_HOSTNAME"); v != "" {
c.hostname = v
}
if v := os.Getenv("DD_ENV"); v != "" {
c.env = v
}
if v := os.Getenv("DD_SERVICE_NAME"); v != "" {
c.service = v
}
if v := os.Getenv("DD_PROFILE_URL"); v != "" {
c.apiURL = v
}
if v := os.Getenv("DD_PROFILE_TAGS"); v != "" {
for _, tag := range strings.Split(v, ",") {
c.tags = append(c.tags, tag)
}
}
// TODO(x): add support for environment variables once we figure out
// the naming standards.
return &c
}

Expand Down
61 changes: 1 addition & 60 deletions profiler/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package profiler

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -60,11 +59,7 @@ func TestDefaultConfig(t *testing.T) {
cfg := defaultConfig()
assert := assert.New(t)
assert.Equal(defaultAPIURL, cfg.apiURL)
if v := os.Getenv("DD_ENV"); v != "" {
assert.Equal(v, cfg.env)
} else {
assert.Equal(defaultEnv, cfg.env)
}
assert.Equal(defaultEnv, cfg.env)
assert.Equal(filepath.Base(os.Args[0]), cfg.service)
assert.Equal(len(defaultProfileTypes), len(cfg.types))
for _, pt := range defaultProfileTypes {
Expand All @@ -78,60 +73,6 @@ func TestDefaultConfig(t *testing.T) {
assert.Equal(DefaultMutexFraction, cfg.mutexFraction)
assert.Equal(DefaultBlockRate, cfg.blockRate)
})

t.Run("env", func(t *testing.T) {
env, val := "DD_API_KEY", "123"
t.Run(env, func(t *testing.T) {
os.Setenv(env, val)
cfg := defaultConfig()
assert.Equal(t, val, cfg.apiKey)
os.Unsetenv(env)
})

env, val = "DD_HOSTNAME", "my-hostname"
t.Run(env, func(t *testing.T) {
os.Setenv(env, val)
cfg := defaultConfig()
assert.Equal(t, val, cfg.hostname)
os.Unsetenv(env)
})

env, val = "DD_ENV", "my-env"
t.Run(env, func(t *testing.T) {
os.Setenv(env, val)
cfg := defaultConfig()
assert.Equal(t, val, cfg.env)
os.Unsetenv(env)
})

env, val = "DD_SERVICE_NAME", "my-service"
t.Run(env, func(t *testing.T) {
os.Setenv(env, val)
cfg := defaultConfig()
assert.Equal(t, val, cfg.service)
os.Unsetenv(env)
})

env, val = "DD_PROFILE_URL", "http://my.url"
t.Run(env, func(t *testing.T) {
os.Setenv(env, val)
cfg := defaultConfig()
assert.Equal(t, val, cfg.apiURL)
os.Unsetenv(env)
})

env, val = "DD_PROFILE_TAGS", "a:b,c:d"
t.Run(env, func(t *testing.T) {
os.Setenv(env, val)
cfg := defaultConfig()
assert.ElementsMatch(t, []string{
"a:b",
"c:d",
fmt.Sprintf("pid:%d", os.Getpid()),
}, cfg.tags)
os.Unsetenv(env)
})
})
}

func TestAddProfileType(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ var (
)

// ErrMissingAPIKey is returned when an API key was not found by the profiler.
var ErrMissingAPIKey = errors.New("API key is missing; provide it using the profiler.WithAPIKey option or the DD_API_KEY environment variable")
var ErrMissingAPIKey = errors.New("API key is missing; provide it using the profiler.WithAPIKey option")

// Start starts the profiler. It may return an error if an API key is not provided by means of an
// option or the DD_API_KEY environment variable, or if a hostname is not found. In the latter case,
// it may be provided using the WithHostname option.
// Start starts the profiler. It may return an error if an API key is not provided by means of
// the WithAPIKey option, or if a hostname is not found. In the latter case, it may be provided
// using the WithHostname option.
func Start(opts ...Option) error {
cfg := defaultConfig()
for _, opt := range opts {
Expand Down

0 comments on commit 24c1643

Please sign in to comment.