DataDog / datadog-go

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

[BUG] Statsd defaults to 8125 port even when the DD_DOGSTATSD_PORT is set (when addr is not empty) #317

Open MrBlaise opened 1 week ago

MrBlaise commented 1 week ago

When the statsd client is created with a non-empty address but without ports. For example localhost instead of localhost:8125 it should rely on DD_DOGSTATSD_PORT to set the port properly. However the code has a bug where populating the variable containing the result of this envar is never used.

In the code here: https://github.com/DataDog/datadog-go/blob/master/statsd/statsd.go#L310-L344

    envPort := ""

    if addr == "" {
        addr = os.Getenv(agentHostEnvVarName)
        envPort = os.Getenv(agentPortEnvVarName)
        agentURL, _ := os.LookupEnv(agentURLEnvVarName)
        agentURL = parseAgentURL(agentURL)

        // agentURLEnvVarName has priority over agentHostEnvVarName
        if agentURL != "" {
            return agentURL
        }
    }

envPort is only set within this if statement, later on it is used like so:

    if envPort != "" {
        addr = fmt.Sprintf("%s:%s", addr, envPort)
    } else {
        addr = fmt.Sprintf("%s:%s", addr, defaultUDPPort)
    }

which means that the else statement will be called always if the addr was not empty