diff --git a/HISTORY.md b/HISTORY.md index cf67a31..57a048e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/README.md b/README.md index a2d6a2e..e05d313 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/datadog/client.go b/datadog/client.go index dc191cc..f43b1f1 100644 --- a/datadog/client.go +++ b/datadog/client.go @@ -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. @@ -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 { @@ -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()) diff --git a/datadog/client_test.go b/datadog/client_test.go index b850a75..fe8e62c 100644 --- a/datadog/client_test.go +++ b/datadog/client_test.go @@ -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)