|
1 | 1 | package datadog.trace.civisibility.utils; |
2 | 2 |
|
| 3 | +import datadog.trace.bootstrap.instrumentation.api.AgentTracer; |
| 4 | +import datadog.trace.context.TraceScope; |
3 | 5 | import datadog.trace.util.AgentThreadFactory; |
4 | 6 | import datadog.trace.util.AgentThreadFactory.AgentThread; |
5 | 7 | import datadog.trace.util.Strings; |
@@ -83,35 +85,38 @@ public <T> T executeCommandReadingError(OutputParser<T> errorParser, String... c |
83 | 85 | private <T> T executeCommand( |
84 | 86 | OutputParser<T> outputParser, byte[] input, boolean readFromError, String... command) |
85 | 87 | throws IOException, TimeoutException, InterruptedException { |
86 | | - ProcessBuilder processBuilder = new ProcessBuilder(command); |
87 | | - processBuilder.directory(executionFolder); |
88 | | - |
89 | | - Process p = processBuilder.start(); |
90 | | - |
91 | | - StreamConsumer inputStreamConsumer = new StreamConsumer(p.getInputStream()); |
92 | | - Thread inputStreamThread = |
93 | | - AgentThreadFactory.newAgentThread( |
94 | | - AgentThread.CI_SHELL_COMMAND, |
95 | | - "-input-stream-consumer-" + command[0], |
96 | | - inputStreamConsumer, |
97 | | - true); |
98 | | - inputStreamThread.start(); |
99 | | - |
100 | | - StreamConsumer errorStreamConsumer = new StreamConsumer(p.getErrorStream()); |
101 | | - Thread errorStreamThread = |
102 | | - AgentThreadFactory.newAgentThread( |
103 | | - AgentThread.CI_SHELL_COMMAND, |
104 | | - "-error-stream-consumer-" + command[0], |
105 | | - errorStreamConsumer, |
106 | | - true); |
107 | | - errorStreamThread.start(); |
108 | | - |
109 | | - if (input != null) { |
110 | | - p.getOutputStream().write(input); |
111 | | - p.getOutputStream().close(); |
112 | | - } |
| 88 | + Process p = null; |
| 89 | + |
| 90 | + // mute tracing to prevent process instrumentation from creating a span for the forked process |
| 91 | + try (TraceScope scope = AgentTracer.get().muteTracing()) { |
| 92 | + ProcessBuilder processBuilder = new ProcessBuilder(command); |
| 93 | + processBuilder.directory(executionFolder); |
| 94 | + |
| 95 | + p = processBuilder.start(); |
| 96 | + |
| 97 | + StreamConsumer inputStreamConsumer = new StreamConsumer(p.getInputStream()); |
| 98 | + Thread inputStreamThread = |
| 99 | + AgentThreadFactory.newAgentThread( |
| 100 | + AgentThread.CI_SHELL_COMMAND, |
| 101 | + "-input-stream-consumer-" + command[0], |
| 102 | + inputStreamConsumer, |
| 103 | + true); |
| 104 | + inputStreamThread.start(); |
| 105 | + |
| 106 | + StreamConsumer errorStreamConsumer = new StreamConsumer(p.getErrorStream()); |
| 107 | + Thread errorStreamThread = |
| 108 | + AgentThreadFactory.newAgentThread( |
| 109 | + AgentThread.CI_SHELL_COMMAND, |
| 110 | + "-error-stream-consumer-" + command[0], |
| 111 | + errorStreamConsumer, |
| 112 | + true); |
| 113 | + errorStreamThread.start(); |
| 114 | + |
| 115 | + if (input != null) { |
| 116 | + p.getOutputStream().write(input); |
| 117 | + p.getOutputStream().close(); |
| 118 | + } |
113 | 119 |
|
114 | | - try { |
115 | 120 | if (p.waitFor(timeoutMillis, TimeUnit.MILLISECONDS)) { |
116 | 121 | int exitValue = p.exitValue(); |
117 | 122 | if (exitValue != 0) { |
@@ -152,6 +157,9 @@ private <T> T executeCommand( |
152 | 157 | } |
153 | 158 |
|
154 | 159 | private void terminate(Process p) throws InterruptedException { |
| 160 | + if (p == null) { |
| 161 | + return; |
| 162 | + } |
155 | 163 | p.destroy(); |
156 | 164 | try { |
157 | 165 | if (!p.waitFor(NORMAL_TERMINATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) { |
|
0 commit comments