11package datadog .common .socket ;
22
3+ import static java .util .concurrent .TimeUnit .MINUTES ;
4+
5+ import datadog .trace .api .Config ;
6+ import datadog .trace .relocate .api .RatelimitedLogger ;
37import java .io .File ;
48import java .io .IOException ;
59import java .net .InetAddress ;
610import java .net .InetSocketAddress ;
711import java .net .Socket ;
812import javax .net .SocketFactory ;
913import jnr .unixsocket .UnixSocketChannel ;
14+ import org .slf4j .Logger ;
15+ import org .slf4j .LoggerFactory ;
1016
1117/**
1218 * Impersonate TCP-style SocketFactory over UNIX domain sockets.
1622 * examples</a>.
1723 */
1824public final class UnixDomainSocketFactory extends SocketFactory {
25+ private static final Logger log = LoggerFactory .getLogger (UnixDomainSocketFactory .class );
26+
27+ private final RatelimitedLogger rlLog = new RatelimitedLogger (log , 5 , MINUTES );
28+
1929 private final File path ;
2030
2131 public UnixDomainSocketFactory (final File path ) {
@@ -24,8 +34,21 @@ public UnixDomainSocketFactory(final File path) {
2434
2535 @ Override
2636 public Socket createSocket () throws IOException {
27- final UnixSocketChannel channel = UnixSocketChannel .open ();
28- return new TunnelingUnixSocket (path , channel );
37+ try {
38+ final UnixSocketChannel channel = UnixSocketChannel .open ();
39+ return new TunnelingUnixSocket (path , channel );
40+ } catch (Throwable e ) {
41+ if (Config .get ().isAgentConfiguredUsingDefault ()) {
42+ // fall back to port if we previously auto-discovered this socket file
43+ if (log .isDebugEnabled ()) {
44+ rlLog .warn ("Problem opening {}, using port instead" , path , e );
45+ } else {
46+ rlLog .warn ("Problem opening {}, using port instead: " + e , path );
47+ }
48+ return getDefault ().createSocket ();
49+ }
50+ throw e ;
51+ }
2952 }
3053
3154 @ Override
0 commit comments