Skip to content

Commit 321b909

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

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

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: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ import (
1414
)
1515

1616
const (
17+
18+
// DefaultHost is the default host to which the datadog client tries to
19+
DefaultHost = "localhost"
20+
21+
// DefaultPort is the default port to which the datadog client tries to
22+
// connect to.
23+
DefaultPort = "8125"
24+
1725
// DefaultAddress is the default address to which the datadog client tries
1826
// to connect to.
19-
DefaultAddress = "localhost:8125"
27+
DefaultAddress = DefaultHost + ":" + DefaultPort
2028

2129
// DefaultBufferSize is the default size for batches of metrics sent to
2230
// datadog.
@@ -77,11 +85,19 @@ func NewClient(addr string) *Client {
7785
})
7886
}
7987

88+
// NewClientFromEnv creates and returns a new datadog client publishing metrics
89+
// to the server running at the address specified in the environment variable.
90+
// The STATSD_HOST and STATSD_UDP_PORT environment variables are used to
91+
// determine the address.
92+
func NewClientFromEnv() *Client {
93+
return NewClientWith(ClientConfig{})
94+
}
95+
8096
// NewClientWith creates and returns a new datadog client configured with the
8197
// given config.
8298
func NewClientWith(config ClientConfig) *Client {
8399
if len(config.Address) == 0 {
84-
config.Address = DefaultAddress
100+
config.Address = addressFromEnv()
85101
}
86102

87103
if config.BufferSize == 0 {
@@ -153,6 +169,25 @@ func (c *Client) Close() error {
153169
return c.err
154170
}
155171

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

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)