@@ -95,6 +95,7 @@ String tag() {
9595 public static final int SOCKET_BUFFER_BYTES = -1 ;
9696 public static final boolean DEFAULT_BLOCKING = false ;
9797 public static final boolean DEFAULT_ENABLE_TELEMETRY = true ;
98+ public static final boolean DEFAULT_ENABLE_JDK_SOCKET = true ;
9899
99100 public static final boolean DEFAULT_ENABLE_AGGREGATION = true ;
100101 public static final boolean DEFAULT_ENABLE_ORIGIN_DETECTION = true ;
@@ -244,6 +245,9 @@ protected static String format(ThreadLocal<NumberFormat> formatter, Number value
244245 * @param connectionTimeout
245246 * the timeout in milliseconds for connecting to the StatsD server. Applies to unix sockets only.
246247 * It is also used to detect if a connection is still alive and re-establish a new one if needed.
248+ * @param enableJdkSocket
249+ * Boolean to enable native JDK UDS support for the UnixStreamClientChannel.
250+ * Only compatible with Java 16 and up.
247251 * @throws StatsDClientException
248252 * if the client could not be started
249253 */
@@ -253,7 +257,8 @@ protected static String format(ThreadLocal<NumberFormat> formatter, Number value
253257 final int maxPacketSizeBytes , String entityID , final int poolSize , final int processorWorkers ,
254258 final int senderWorkers , boolean blocking , final boolean enableTelemetry , final int telemetryFlushInterval ,
255259 final int aggregationFlushInterval , final int aggregationShards , final ThreadFactory customThreadFactory ,
256- String containerID , final boolean originDetectionEnabled , final int connectionTimeout )
260+ String containerID , final boolean originDetectionEnabled , final int connectionTimeout ,
261+ final boolean enableJdkSocket )
257262 throws StatsDClientException {
258263
259264 if ((prefix != null ) && (!prefix .isEmpty ())) {
@@ -298,7 +303,7 @@ protected static String format(ThreadLocal<NumberFormat> formatter, Number value
298303 }
299304
300305 try {
301- clientChannel = createByteChannel (addressLookup , timeout , connectionTimeout , bufferSize );
306+ clientChannel = createByteChannel (addressLookup , timeout , connectionTimeout , bufferSize , enableJdkSocket );
302307
303308 ThreadFactory threadFactory = customThreadFactory != null ? customThreadFactory : new StatsDThreadFactory ();
304309
@@ -317,7 +322,7 @@ protected static String format(ThreadLocal<NumberFormat> formatter, Number value
317322 telemetryClientChannel = clientChannel ;
318323 telemetryStatsDProcessor = statsDProcessor ;
319324 } else {
320- telemetryClientChannel = createByteChannel (telemetryAddressLookup , timeout , connectionTimeout , bufferSize );
325+ telemetryClientChannel = createByteChannel (telemetryAddressLookup , timeout , connectionTimeout , bufferSize , enableJdkSocket );
321326
322327 // similar settings, but a single worker and non-blocking.
323328 telemetryStatsDProcessor = createProcessor (queueSize , handler , getPacketSize (telemetryClientChannel ),
@@ -378,7 +383,8 @@ public NonBlockingStatsDClient(final NonBlockingStatsDClientBuilder builder) thr
378383 builder .blocking , builder .enableTelemetry , builder .telemetryFlushInterval ,
379384 (builder .enableAggregation ? builder .aggregationFlushInterval : 0 ),
380385 builder .aggregationShards , builder .threadFactory , builder .containerID ,
381- builder .originDetectionEnabled , builder .connectionTimeout );
386+ builder .originDetectionEnabled , builder .connectionTimeout ,
387+ builder .enableJdkSocket );
382388 }
383389
384390 protected StatsDProcessor createProcessor (final int queueSize , final StatsDClientErrorHandler handler ,
@@ -480,7 +486,7 @@ StringBuilder tagString(final String[] tags, StringBuilder builder) {
480486 }
481487
482488 ClientChannel createByteChannel (
483- Callable <SocketAddress > addressLookup , int timeout , int connectionTimeout , int bufferSize )
489+ Callable <SocketAddress > addressLookup , int timeout , int connectionTimeout , int bufferSize , boolean enableJdkSocket )
484490 throws Exception {
485491 final SocketAddress address = addressLookup .call ();
486492 if (address instanceof NamedPipeSocketAddress ) {
@@ -493,7 +499,7 @@ ClientChannel createByteChannel(
493499 // Allow us to support `unix://` for both kind of sockets like in go.
494500 switch (unixAddr .getTransportType ()) {
495501 case UDS_STREAM :
496- return new UnixStreamClientChannel (unixAddr .getAddress (), timeout , connectionTimeout , bufferSize );
502+ return new UnixStreamClientChannel (unixAddr .getAddress (), timeout , connectionTimeout , bufferSize , enableJdkSocket );
497503 case UDS_DATAGRAM :
498504 case UDS :
499505 return new UnixDatagramClientChannel (unixAddr .getAddress (), timeout , bufferSize );
0 commit comments