Skip to content

Use environment variables to configure the Datadog client #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# History

### Unreleased

- Adds support for datadog client configuration using environment variables.
`STATSD_HOST`, `STATSD_UDP_PORT` and `STATSD_SOCKET` can now be used to
configure the datadog client.

### v5.4.0 (February 21, 2025)

- Fix a regression in configured buffer size for the datadog client. Versions
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ func main() {
}
```

### Configuring Using Environment Variables

The datadog client can be auto-configured using the following environment variables:

* `STATSD_HOST` - the hostname of the UDP server. Defaults to `localhost`
* `STATSD_UDP_PORT` - the port of the UDP server. Defaults to `8125`
* `STATSD_SOCKET` - the path of the unix domain socket of the server.

Note: if the `STATSD_SOCKET` variable is present, the client will be configured to use UDS by default and ignore the UDP settings.

```go
func main() {
stats.Register(datadog.NewClientFromEnv())
defer stats.Flush()

// ...
```

### Flushing Metrics

Metrics are stored in a buffer, which will be flushed when it reaches its
Expand Down
40 changes: 38 additions & 2 deletions datadog/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ import (
)

const (

// DefaultHost is the default host to which the datadog client tries to
// connect to.
DefaultHost = "localhost"

// DefaultPort is the default port to which the datadog client tries to
// connect to.
DefaultPort = "8125"

// DefaultAddress is the default address to which the datadog client tries
// to connect to.
DefaultAddress = "localhost:8125"
DefaultAddress = DefaultHost + ":" + DefaultPort

// DefaultBufferSize is the default size for batches of metrics sent to
// datadog.
Expand Down Expand Up @@ -77,11 +86,19 @@ func NewClient(addr string) *Client {
})
}

// NewClientFromEnv creates and returns a new datadog client publishing metrics
// to the server running at the address specified in the environment variable.
// The STATSD_HOST and STATSD_UDP_PORT environment variables are used to
// determine the address.
func NewClientFromEnv() *Client {
return NewClientWith(ClientConfig{})
}

// NewClientWith creates and returns a new datadog client configured with the
// given config.
func NewClientWith(config ClientConfig) *Client {
if len(config.Address) == 0 {
config.Address = DefaultAddress
config.Address = addressFromEnv()
}

if config.BufferSize == 0 {
Expand Down Expand Up @@ -153,6 +170,25 @@ func (c *Client) Close() error {
return c.err
}

// Returns the address to which the client will send metrics by
// looking at the STATSD_SOCKET, STATSD_HOST and STATSD_UDP_PORT environment variables.
func addressFromEnv() string {
socket := os.Getenv("STATSD_SOCKET")
if len(socket) > 0 {
return "unixgram://" + socket
}
hostname := os.Getenv("STATSD_HOST")
if len(hostname) == 0 {
hostname = DefaultHost
}
port := os.Getenv("STATSD_UDP_PORT")
if len(port) == 0 {
port = DefaultPort
}
addr := hostname + ":" + port
return addr
}

func bufSizeFromFD(f *os.File, sizehint int) (bufsize int, err error) {
fd := int(f.Fd())

Expand Down
27 changes: 27 additions & 0 deletions datadog/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ import (
"github.com/stretchr/testify/assert"
)

// TestDefaultAddressFromEnv tests that the addressFromEnv function returns the
// default address when the environment variable is not set.
func TestDefaultAddressFromEnv(t *testing.T) {
address := addressFromEnv()

assert.Equal(t, "localhost:8125", address)
}

// TestUdpAddressFromEnv tests that the addressFromEnv function returns the
// address from the environment variable when it is set.
func TestUdpAddressFromEnv(t *testing.T) {
t.Setenv("STATSD_HOST", "not-localhost")
t.Setenv("STATSD_UDP_PORT", "1234")

address := addressFromEnv()
assert.Equal(t, "not-localhost:1234", address)
}

// TestUdsAddressFromEnv tests that the addressFromEnv function returns the
// address from the environment variable when it is set.
func TestUdsAddressFromEnv(t *testing.T) {
t.Setenv("STATSD_SOCKET", "/dir/file.ext")

address := addressFromEnv()
assert.Equal(t, "unixgram:///dir/file.ext", address)
}

func TestClient(t *testing.T) {
client := NewClient(DefaultAddress)

Expand Down
Loading