Skip to content

Commit 83b280c

Browse files
committed
Experiment: defer use of java.nio during premain
1 parent 52a8cc7 commit 83b280c

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

internal-api/src/main/java/datadog/trace/api/git/GitInfoProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import datadog.trace.api.civisibility.telemetry.tag.GitShaDiscrepancyType;
1010
import datadog.trace.api.civisibility.telemetry.tag.GitShaMatch;
1111
import datadog.trace.util.Strings;
12-
import java.nio.file.Paths;
12+
import java.io.File;
1313
import java.util.ArrayList;
1414
import java.util.Collection;
1515
import java.util.Collections;
@@ -33,7 +33,7 @@ public class GitInfoProvider {
3333
INSTANCE.registerGitInfoBuilder(new UserSuppliedGitInfoBuilder());
3434
}
3535

36-
static final String NULL_PATH_STRING = Paths.get("").toAbsolutePath().toString();
36+
static final String NULL_PATH_STRING = new File("").getAbsolutePath();
3737

3838
private volatile Collection<GitInfoBuilder> builders = Collections.emptyList();
3939

utils/container-utils/src/main/java/datadog/common/container/ContainerInfo.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import de.thetaphi.forbiddenapis.SuppressForbidden;
44
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
5+
import java.io.File;
56
import java.io.IOException;
67
import java.nio.file.Files;
7-
import java.nio.file.Path;
88
import java.nio.file.Paths;
99
import java.text.ParseException;
1010
import java.util.ArrayList;
@@ -28,9 +28,9 @@ public class ContainerInfo {
2828

2929
private static final Logger log = LoggerFactory.getLogger(ContainerInfo.class);
3030

31-
private static final Path CGROUP_DEFAULT_PROCFILE = Paths.get("/proc/self/cgroup");
32-
private static final Path DEFAULT_CGROUP_MOUNT_PATH = Paths.get("/sys/fs/cgroup");
33-
private static final Path HOST_GROUP_NAMESPACE = Paths.get("/proc/self/ns/cgroup");
31+
private static final String CGROUP_DEFAULT_PROCFILE = "/proc/self/cgroup";
32+
private static final String DEFAULT_CGROUP_MOUNT_PATH = "/sys/fs/cgroup";
33+
private static final String HOST_GROUP_NAMESPACE = "/proc/self/ns/cgroup";
3434

3535
// The second part is the PCF/Garden regexp. We assume no suffix ($) to avoid matching pod UIDs
3636
// See https://github.com/DataDog/datadog-agent/blob/7.40.x/pkg/util/cgroups/reader.go#L50
@@ -106,7 +106,7 @@ public void setcGroups(List<CGroupInfo> cGroups) {
106106
}
107107

108108
static @Nullable String readEntityID(
109-
ContainerInfo containerInfo, boolean isHostCgroupNamespace, Path cgroupMountPath) {
109+
ContainerInfo containerInfo, boolean isHostCgroupNamespace, String cgroupMountPath) {
110110
String cid = containerInfo.getContainerId();
111111
if (cid != null && !cid.isEmpty()) {
112112
return "cid-" + cid;
@@ -129,13 +129,18 @@ static boolean isHostCgroupNamespace() {
129129
* Returns the inode of the cgroup controller if it exists, otherwise an empty string. The inode
130130
* is prefixed with "in-" and is used by the agent to retrieve the container ID.
131131
*/
132-
static @Nullable String getCgroupInode(Path cgroupMountPath, List<CGroupInfo> cgroups) {
132+
static @Nullable String getCgroupInode(String cgroupMountPath, List<CGroupInfo> cgroups) {
133133
// It first tries to get the cGroupV1 "memory" controller inode, and if that fails, it tries to
134134
// get the cGroupV2 inode.
135135
for (String controller : Arrays.asList(CGROUPV1_BASE_CONTROLLER, CGROUPV2_BASE_CONTROLLER)) {
136136
for (CGroupInfo cgroup : cgroups) {
137137
if (cgroup.getControllers().contains(controller)) {
138-
Path path = Paths.get(cgroupMountPath.toString(), controller, cgroup.getPath());
138+
String path =
139+
cgroupMountPath
140+
+ File.pathSeparatorChar
141+
+ controller
142+
+ File.pathSeparatorChar
143+
+ cgroup.getPath();
139144
long inode = readInode(path);
140145
// ignore invalid and root inode
141146
if (inode > 2) {
@@ -148,9 +153,10 @@ static boolean isHostCgroupNamespace() {
148153
}
149154

150155
/** @return 0 - if it couldn't read inode */
151-
static long readInode(Path path) {
156+
static long readInode(String path) {
152157
try {
153-
return (long) Files.getAttribute(path, "unix:ino");
158+
File f = new File(path);
159+
return f.exists() ? (long) Files.getAttribute(f.toPath(), "unix:ino") : 0L;
154160
} catch (Exception e) {
155161
log.debug("Unable to read cgroup inode of {} because of {}", path, e.getClass());
156162
}
@@ -214,15 +220,15 @@ public static String getEntityId() {
214220
}
215221

216222
public static boolean isRunningInContainer() {
217-
return Files.isReadable(CGROUP_DEFAULT_PROCFILE);
223+
return new File(CGROUP_DEFAULT_PROCFILE).canRead();
218224
}
219225

220226
public static ContainerInfo fromDefaultProcFile() throws IOException, ParseException {
221227
return fromProcFile(CGROUP_DEFAULT_PROCFILE);
222228
}
223229

224-
static ContainerInfo fromProcFile(Path path) throws IOException, ParseException {
225-
final String content = new String(Files.readAllBytes(path));
230+
static ContainerInfo fromProcFile(String path) throws IOException, ParseException {
231+
final String content = new String(Files.readAllBytes(Paths.get(path)));
226232
if (content.isEmpty()) {
227233
log.debug("Proc file is empty");
228234
return new ContainerInfo();

0 commit comments

Comments
 (0)