Skip to content

Commit 215c0ac

Browse files
authored
Fall back to ports when we cannot use auto-discovered unix domain sockets. (#7794)
This might be due to issues linking the JFFI library or permission problems.
1 parent 3fd69de commit 215c0ac

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

utils/socket-utils/src/main/java/datadog/common/socket/UnixDomainSocketFactory.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package 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;
37
import java.io.File;
48
import java.io.IOException;
59
import java.net.InetAddress;
610
import java.net.InetSocketAddress;
711
import java.net.Socket;
812
import javax.net.SocketFactory;
913
import jnr.unixsocket.UnixSocketChannel;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1016

1117
/**
1218
* Impersonate TCP-style SocketFactory over UNIX domain sockets.
@@ -16,6 +22,10 @@
1622
* examples</a>.
1723
*/
1824
public 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

Comments
 (0)