Skip to content

Commit 093a451

Browse files
Merge pull request #68 from bugsnag/kattrali/avoid-waiting-on-hostname-resolution
Cache hostname at launch
2 parents cdc8284 + bf70d35 commit 093a451

2 files changed

Lines changed: 34 additions & 23 deletions

File tree

src/main/java/com/bugsnag/Configuration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Configuration {
3434
// Add built-in callbacks
3535
addCallback(new AppCallback(this));
3636
addCallback(new DeviceCallback());
37+
DeviceCallback.initializeCache();
3738

3839
if (ServletCallback.isAvailable()) {
3940
addCallback(new ServletCallback());

src/main/java/com/bugsnag/callbacks/DeviceCallback.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,52 @@
22

33
import com.bugsnag.Report;
44

5+
import com.google.common.base.Supplier;
6+
import com.google.common.base.Suppliers;
7+
58
import java.net.InetAddress;
69
import java.net.UnknownHostException;
710
import java.util.Locale;
811

912
public class DeviceCallback implements Callback {
13+
private static final Supplier<String> hostnameCache =
14+
Suppliers.memoize(new Supplier<String>() {
15+
16+
public String get() {
17+
// Windows always sets COMPUTERNAME
18+
if (System.getProperty("os.name").startsWith("Windows")) {
19+
return System.getenv("COMPUTERNAME");
20+
}
21+
22+
// Try the HOSTNAME env variable (most unix systems)
23+
String hostname = System.getenv("HOSTNAME");
24+
if (hostname != null) {
25+
return hostname;
26+
}
27+
28+
// Resort to dns hostname lookup
29+
try {
30+
return InetAddress.getLocalHost().getHostName();
31+
} catch (UnknownHostException ex) {
32+
// Give up
33+
}
34+
return null;
35+
}
36+
});
37+
38+
public static void initializeCache() {
39+
hostnameCache.get();
40+
}
41+
1042
@Override
1143
public void beforeNotify(Report report) {
1244
report
13-
.setDeviceInfo("hostname", getHostname())
45+
.setDeviceInfo("hostname", hostnameCache.get())
1446
.setDeviceInfo("osName", System.getProperty("os.name"))
1547
.setDeviceInfo("osVersion", System.getProperty("os.version"))
1648
.setDeviceInfo("osArch", System.getProperty("os.arch"))
1749
.setDeviceInfo("runtimeName", System.getProperty("java.runtime.name"))
1850
.setDeviceInfo("runtimeVersion", System.getProperty("java.runtime.version"))
1951
.setDeviceInfo("locale", Locale.getDefault());
2052
}
21-
22-
private String getHostname() {
23-
// Windows always sets COMPUTERNAME
24-
if (System.getProperty("os.name").startsWith("Windows")) {
25-
return System.getenv("COMPUTERNAME");
26-
}
27-
28-
// Try the HOSTNAME env variable (most unix systems)
29-
String hostname = System.getenv("HOSTNAME");
30-
if (hostname != null) {
31-
return hostname;
32-
}
33-
34-
// Resort to dns hostname lookup
35-
try {
36-
return InetAddress.getLocalHost().getHostName();
37-
} catch (UnknownHostException ex) {
38-
// Give up
39-
}
40-
41-
return null;
42-
}
4353
}

0 commit comments

Comments
 (0)