DataDog / datadog-go

go dogstatsd client library for datadog
MIT License
353 stars 133 forks source link

fix: Support cloning clients that use DD_AGENT_HOST #314

Closed abhinav closed 2 months ago

abhinav commented 3 months ago

Today, statsd client that use DD_AGENT_HOST for their address cannot be cloned:

// export DD_AGENT_HOST=localhost:8125
client, err := statsd.New("")
if err != nil {
  log.Fatal(err)
}

cloned, err := statsd.CloneWithExtraOptions(client, /* ... */)
fmt.Println(err)  // can't clone client with no addrOption

This is because we resolve the address in createWriter, but do not record the resolved address in the client.

func New(addr string, options ...Option) (*Client, error) {
  // ...
  ... := createWriter(addr, /* ... */) // resolves addr internally
  // ...
  client.addrOption = addr  // addr is old, unresolved value
}

This commit fixes this issue by replacing the address with the resolved value so that that's what we record on the client, allowing it to be cloned.

Testing: Includes a regression test for the bug.

Resolves #313