Skip to content

Commit 87832f0

Browse files
committed
Fix a bug in address resolution order
Before this patch, named pipe from the environment would be used even if the application supplies host and port pair explicitly. Make sure the builder uses configuration in this order: 1. explicitly supplied by the application via builder methods 2. auto-detected settings from the environment variables 3. built-in defaults
1 parent 8db8b64 commit 87832f0

1 file changed

Lines changed: 36 additions & 27 deletions

File tree

src/main/java/com/timgroup/statsd/NonBlockingStatsDClientBuilder.java

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,7 @@ protected NonBlockingStatsDClientBuilder resolve() {
207207
}
208208

209209
int packetSize = maxPacketSizeBytes;
210-
Callable<SocketAddress> lookup = addressLookup;
211-
212-
if (lookup == null) {
213-
String namedPipeFromEnv = System.getenv(NonBlockingStatsDClient.DD_NAMED_PIPE_ENV_VAR);
214-
String resolvedNamedPipe = namedPipe == null ? namedPipeFromEnv : namedPipe;
215-
216-
if (resolvedNamedPipe == null) {
217-
lookup = staticStatsDAddressResolution(hostname, port);
218-
} else {
219-
lookup = staticNamedPipeResolution(resolvedNamedPipe);
220-
}
221-
}
210+
Callable<SocketAddress> lookup = getAddressLookup();
222211

223212
if (packetSize == 0) {
224213
packetSize = (port == 0) ? NonBlockingStatsDClient.DEFAULT_UDS_MAX_PACKET_SIZE_BYTES :
@@ -230,7 +219,7 @@ protected NonBlockingStatsDClientBuilder resolve() {
230219
if (telemetryHostname == null) {
231220
telemetryLookup = lookup;
232221
} else {
233-
telemetryLookup = staticStatsDAddressResolution(telemetryHostname, telemetryPort);
222+
telemetryLookup = staticAddress(telemetryHostname, telemetryPort);
234223
}
235224
}
236225

@@ -241,6 +230,32 @@ protected NonBlockingStatsDClientBuilder resolve() {
241230
return resolved;
242231
}
243232

233+
private Callable<SocketAddress> getAddressLookup() {
234+
// First, use explicit configuration on the builder.
235+
if (addressLookup != null) {
236+
return addressLookup;
237+
}
238+
239+
if (namedPipe != null) {
240+
return staticNamedPipeResolution(namedPipe);
241+
}
242+
243+
if (hostname != null) {
244+
return staticAddress(hostname, port);
245+
}
246+
247+
// Next, try various environment variables.
248+
String namedPipeFromEnv = System.getenv(NonBlockingStatsDClient.DD_NAMED_PIPE_ENV_VAR);
249+
if (namedPipeFromEnv != null) {
250+
return staticNamedPipeResolution(namedPipeFromEnv);
251+
}
252+
253+
String hostFromEnv = getHostnameFromEnvVar();
254+
int portFromEnv = getPortFromEnvVar(port);
255+
256+
return staticAddress(hostFromEnv, portFromEnv);
257+
}
258+
244259
/**
245260
* Create dynamic lookup for the given host name and port.
246261
*
@@ -288,20 +303,6 @@ public static Callable<SocketAddress> staticAddressResolution(final String hostn
288303
};
289304
}
290305

291-
protected static Callable<SocketAddress> staticStatsDAddressResolution(String hostname, int port)
292-
throws StatsDClientException {
293-
try {
294-
if (hostname == null) {
295-
hostname = getHostnameFromEnvVar();
296-
port = getPortFromEnvVar(port);
297-
}
298-
299-
return staticAddressResolution(hostname, port);
300-
} catch (final Exception e) {
301-
throw new StatsDClientException("Failed to lookup StatsD host", e);
302-
}
303-
}
304-
305306
protected static Callable<SocketAddress> staticNamedPipeResolution(String namedPipe) {
306307
final NamedPipeSocketAddress socketAddress = new NamedPipeSocketAddress(namedPipe);
307308
return new Callable<SocketAddress>() {
@@ -311,6 +312,14 @@ protected static Callable<SocketAddress> staticNamedPipeResolution(String namedP
311312
};
312313
}
313314

315+
private static Callable<SocketAddress> staticAddress(final String hostname, final int port) {
316+
try {
317+
return staticAddressResolution(hostname, port);
318+
} catch (Exception e) {
319+
throw new StatsDClientException("Failed to lookup StatsD host", e);
320+
}
321+
}
322+
314323
/**
315324
* Retrieves host name from the environment variable "DD_AGENT_HOST".
316325
*

0 commit comments

Comments
 (0)