Skip to content

Commit e534db4

Browse files
committed
feat: use environment variables to configure the datadog client
1 parent 2819276 commit e534db4

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# History
22

3+
### Unreleased
4+
5+
- Adds support for datadog client configuration using environment variables.
6+
`STATSD_HOST`, `STATSD_UDP_PORT` and `STATSD_SOCKET` can now be used to
7+
configure the datadog client.
8+
39
### v5.4.0 (February 21, 2025)
410

511
- Fix a regression in configured buffer size for the datadog client. Versions

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ func main() {
151151
}
152152
```
153153

154+
### Configuring Using Environment Variables
155+
156+
The datadog client can be auto-configured using the following environment variables:
157+
158+
* `STATSD_HOST` - the hostname of the UDP server. Defaults to `localhost`
159+
* `STATSD_UDP_PORT` - the port of the UDP server. Defaults to `8125`
160+
* `STATSD_SOCKET` - the path of the unix domain socket of the server.
161+
162+
Note: if the `STATSD_SOCKET` variable is present, the client will be configured to use UDS by default and ignore the UDP settings.
163+
164+
```go
165+
func main() {
166+
stats.Register(datadog.NewClientFromEnv())
167+
defer stats.Flush()
168+
169+
// ...
170+
```
171+
154172
### Flushing Metrics
155173
156174
Metrics are stored in a buffer, which will be flushed when it reaches its

datadog/client.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@ import (
1414
)
1515

1616
const (
17+
18+
// DefaultHost is the default host to which the datadog client tries to
19+
// connect to.
20+
DefaultHost = "localhost"
21+
22+
// DefaultPort is the default port to which the datadog client tries to
23+
// connect to.
24+
DefaultPort = "8125"
25+
1726
// DefaultAddress is the default address to which the datadog client tries
1827
// to connect to.
19-
DefaultAddress = "localhost:8125"
28+
DefaultAddress = DefaultHost + ":" + DefaultPort
2029

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

89+
// NewClientFromEnv creates and returns a new datadog client publishing metrics
90+
// to the server running at the address specified in the environment variable.
91+
// The STATSD_HOST and STATSD_UDP_PORT environment variables are used to
92+
// determine the address.
93+
func NewClientFromEnv() *Client {
94+
return NewClientWith(ClientConfig{})
95+
}
96+
8097
// NewClientWith creates and returns a new datadog client configured with the
8198
// given config.
8299
func NewClientWith(config ClientConfig) *Client {
83100
if len(config.Address) == 0 {
84-
config.Address = DefaultAddress
101+
config.Address = addressFromEnv()
85102
}
86103

87104
if config.BufferSize == 0 {
@@ -153,6 +170,25 @@ func (c *Client) Close() error {
153170
return c.err
154171
}
155172

173+
// Returns the address to which the client will send metrics by
174+
// looking at the STATSD_SOCKET, STATSD_HOST and STATSD_UDP_PORT environment variables.
175+
func addressFromEnv() string {
176+
socket := os.Getenv("STATSD_SOCKET")
177+
if len(socket) > 0 {
178+
return "unixgram://" + socket
179+
}
180+
hostname := os.Getenv("STATSD_HOST")
181+
if len(hostname) == 0 {
182+
hostname = DefaultHost
183+
}
184+
port := os.Getenv("STATSD_UDP_PORT")
185+
if len(port) == 0 {
186+
port = DefaultPort
187+
}
188+
addr := hostname + ":" + port
189+
return addr
190+
}
191+
156192
func bufSizeFromFD(f *os.File, sizehint int) (bufsize int, err error) {
157193
fd := int(f.Fd())
158194

datadog/client_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19+
// TestDefaultAddressFromEnv tests that the addressFromEnv function returns the
20+
// default address when the environment variable is not set.
21+
func TestDefaultAddressFromEnv(t *testing.T) {
22+
address := addressFromEnv()
23+
24+
assert.Equal(t, "localhost:8125", address)
25+
}
26+
27+
// TestUdpAddressFromEnv tests that the addressFromEnv function returns the
28+
// address from the environment variable when it is set.
29+
func TestUdpAddressFromEnv(t *testing.T) {
30+
t.Setenv("STATSD_HOST", "not-localhost")
31+
t.Setenv("STATSD_UDP_PORT", "1234")
32+
33+
address := addressFromEnv()
34+
assert.Equal(t, "not-localhost:1234", address)
35+
}
36+
37+
// TestUdsAddressFromEnv tests that the addressFromEnv function returns the
38+
// address from the environment variable when it is set.
39+
func TestUdsAddressFromEnv(t *testing.T) {
40+
t.Setenv("STATSD_SOCKET", "/dir/file.ext")
41+
42+
address := addressFromEnv()
43+
assert.Equal(t, "unixgram:///dir/file.ext", address)
44+
}
45+
1946
func TestClient(t *testing.T) {
2047
client := NewClient(DefaultAddress)
2148

0 commit comments

Comments
 (0)