-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPreMain.java
More file actions
311 lines (275 loc) · 13.4 KB
/
PreMain.java
File metadata and controls
311 lines (275 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.teamscale.jacoco.agent;
import com.teamscale.client.FileSystemUtils;
import com.teamscale.client.HttpUtils;
import com.teamscale.client.StringUtils;
import com.teamscale.jacoco.agent.configuration.AgentOptionReceiveException;
import com.teamscale.jacoco.agent.logging.DebugLogDirectoryPropertyDefiner;
import com.teamscale.jacoco.agent.logging.LogDirectoryPropertyDefiner;
import com.teamscale.jacoco.agent.logging.LogToTeamscaleAppender;
import com.teamscale.jacoco.agent.logging.LoggingUtils;
import com.teamscale.jacoco.agent.options.AgentOptionParseException;
import com.teamscale.jacoco.agent.options.AgentOptions;
import com.teamscale.jacoco.agent.options.AgentOptionsParser;
import com.teamscale.jacoco.agent.options.FilePatternResolver;
import com.teamscale.jacoco.agent.options.JacocoAgentOptionsBuilder;
import com.teamscale.jacoco.agent.options.TeamscaleCredentials;
import com.teamscale.jacoco.agent.options.TeamscalePropertiesUtils;
import com.teamscale.jacoco.agent.testimpact.TestwiseCoverageAgent;
import com.teamscale.jacoco.agent.upload.UploaderException;
import com.teamscale.jacoco.agent.util.AgentUtils;
import com.teamscale.report.util.ILogger;
import kotlin.Pair;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static com.teamscale.jacoco.agent.logging.LoggingUtils.getLoggerContext;
/** Container class for the premain entry point for the agent. */
public class PreMain {
private static LoggingUtils.LoggingResources loggingResources = null;
/**
* System property that we use to prevent this agent from being attached to the same VM twice. This can happen if
* the agent is registered via multiple JVM environment variables and/or the command line at the same time.
*/
private static final String LOCKING_SYSTEM_PROPERTY = "TEAMSCALE_JAVA_PROFILER_ATTACHED";
/**
* Environment variable from which to read the config ID to use. This is an ID for a profiler configuration that is
* stored in Teamscale.
*/
private static final String CONFIG_ID_ENVIRONMENT_VARIABLE = "TEAMSCALE_JAVA_PROFILER_CONFIG_ID";
/** Environment variable from which to read the config file to use. */
private static final String CONFIG_FILE_ENVIRONMENT_VARIABLE = "TEAMSCALE_JAVA_PROFILER_CONFIG_FILE";
/** Environment variable from which to read the Teamscale access token. */
private static final String ACCESS_TOKEN_ENVIRONMENT_VARIABLE = "TEAMSCALE_ACCESS_TOKEN";
/**
* Entry point for the agent, called by the JVM. If this method throws an exception, the JVM will abort the
* entire application. Therefore, configuration errors must be handled gracefully by returning normally, which
* allows the application to start without coverage collection.
*/
public static void premain(String options, Instrumentation instrumentation) throws Exception {
if (System.getProperty(LOCKING_SYSTEM_PROPERTY) != null) {
return;
}
System.setProperty(LOCKING_SYSTEM_PROPERTY, "true");
String environmentConfigId = System.getenv(CONFIG_ID_ENVIRONMENT_VARIABLE);
String environmentConfigFile = System.getenv(CONFIG_FILE_ENVIRONMENT_VARIABLE);
if (StringUtils.isEmpty(options) && environmentConfigId == null && environmentConfigFile == null) {
// profiler was registered globally and no config was set explicitly by the user, thus ignore this process
// and don't profile anything
return;
}
AgentOptions agentOptions = null;
try {
Pair<AgentOptions, List<Exception>> parseResult = getAndApplyAgentOptions(options, environmentConfigId,
environmentConfigFile);
agentOptions = parseResult.getFirst();
// After parsing everything and configuring logging, we now
// can throw the caught exceptions.
for (Exception exception : parseResult.getSecond()) {
throw exception;
}
} catch (AgentOptionParseException e) {
// Flush logs to Teamscale, if configured.
closeLoggingResources();
// Unregister the profiler from Teamscale.
if (agentOptions != null && agentOptions.configurationViaTeamscale != null) {
agentOptions.configurationViaTeamscale.unregisterProfiler();
}
// Don't crash the profiled application due to a configuration error
// (see TS-43260). The error has already been logged in getAndApplyAgentOptions.
return;
} catch (AgentOptionReceiveException e) {
// When Teamscale is not available, we don't want to fail hard to still allow for testing even if no
// coverage is collected (see TS-33237)
return;
}
Logger logger = LoggingUtils.getLogger(Agent.class);
logger.info("Teamscale Java profiler version " + AgentUtils.VERSION);
logger.info("Starting JaCoCo's agent");
JacocoAgentOptionsBuilder agentBuilder = new JacocoAgentOptionsBuilder(agentOptions);
JaCoCoPreMain.premain(agentBuilder.createJacocoAgentOptions(), instrumentation, logger);
if (agentOptions.configurationViaTeamscale != null) {
agentOptions.configurationViaTeamscale.startHeartbeatThreadAndRegisterShutdownHook();
}
AgentBase agent = createAgent(agentOptions, instrumentation);
agent.registerShutdownHook();
}
private static Pair<AgentOptions, List<Exception>> getAndApplyAgentOptions(String options,
String environmentConfigId,
String environmentConfigFile) throws AgentOptionParseException, IOException, AgentOptionReceiveException {
DelayedLogger delayedLogger = new DelayedLogger();
List<String> javaAgents = ManagementFactory.getRuntimeMXBean().getInputArguments().stream().filter(
s -> s.contains("-javaagent")).collect(Collectors.toList());
// We allow multiple instances of the teamscale-jacoco-agent as we ensure with the #LOCKING_SYSTEM_PROPERTY to only use it once
List<String> differentAgents = javaAgents.stream()
.filter(javaAgent -> !javaAgent.contains("teamscale-jacoco-agent.jar")).collect(
Collectors.toList());
if (!differentAgents.isEmpty()) {
delayedLogger.warn(
"Using multiple java agents could interfere with coverage recording: " +
String.join(", ", differentAgents));
}
if (!javaAgents.get(0).contains("teamscale-jacoco-agent.jar")) {
delayedLogger.warn("For best results consider registering the Teamscale Java Profiler first.");
}
TeamscaleCredentials credentials = TeamscalePropertiesUtils.parseCredentials();
if (credentials == null) {
// As many users still don't use the installer based setup, this log message will be shown in almost every log.
// We use a debug log, as this message can be confusing for customers that think a teamscale.properties file is synonymous with a config file.
delayedLogger.debug(
"No explicit teamscale.properties file given. Looking for Teamscale credentials in a config file or via a command line argument. This is expected unless the installer based setup was used.");
}
String environmentAccessToken = System.getenv(ACCESS_TOKEN_ENVIRONMENT_VARIABLE);
Pair<AgentOptions, List<Exception>> parseResult;
AgentOptions agentOptions;
try {
parseResult = AgentOptionsParser.parse(
options, environmentConfigId, environmentConfigFile, credentials, environmentAccessToken,
delayedLogger);
agentOptions = parseResult.getFirst();
} catch (AgentOptionParseException e) {
try (LoggingUtils.LoggingResources ignored = initializeFallbackLogging(options, delayedLogger)) {
delayedLogger.errorAndStdErr(
"Failed to parse agent options: " + e.getMessage() + " The application should start up normally, but NO coverage will be collected!",
e);
attemptLogAndThrow(delayedLogger);
throw e;
}
} catch (AgentOptionReceiveException e) {
try (LoggingUtils.LoggingResources ignored = initializeFallbackLogging(options, delayedLogger)) {
delayedLogger.errorAndStdErr(
e.getMessage() + " The application should start up normally, but NO coverage will be collected! Check the log file for details.",
e);
attemptLogAndThrow(delayedLogger);
throw e;
}
}
initializeLogging(agentOptions, delayedLogger);
Logger logger = LoggingUtils.getLogger(Agent.class);
delayedLogger.logTo(logger);
HttpUtils.setShouldValidateSsl(agentOptions.shouldValidateSsl());
return parseResult;
}
private static void attemptLogAndThrow(DelayedLogger delayedLogger) {
// We perform actual logging output after writing to console to
// ensure the console is reached even in case of logging issues
// (see TS-23151). We use the Agent class here (same as below)
Logger logger = LoggingUtils.getLogger(Agent.class);
delayedLogger.logTo(logger);
}
/** Initializes logging during {@link #premain(String, Instrumentation)} and also logs the log directory. */
private static void initializeLogging(AgentOptions agentOptions, DelayedLogger logger) throws IOException {
if (agentOptions.isDebugLogging()) {
initializeDebugLogging(agentOptions, logger);
} else {
loggingResources = LoggingUtils.initializeLogging(agentOptions.getLoggingConfig());
logger.info("Logging to " + new LogDirectoryPropertyDefiner().getPropertyValue());
}
if (agentOptions.getTeamscaleServerOptions().isConfiguredForServerConnection()) {
if (LogToTeamscaleAppender.addTeamscaleAppenderTo(getLoggerContext(), agentOptions)) {
logger.info("Logs are being forwarded to Teamscale at " + agentOptions.getTeamscaleServerOptions().url);
}
}
}
/** Closes the opened logging contexts. */
static void closeLoggingResources() {
if (loggingResources != null) {
loggingResources.close();
}
}
/**
* Returns in instance of the agent that was configured. Either an agent with interval based line-coverage dump or
* the HTTP server is used.
*/
private static AgentBase createAgent(AgentOptions agentOptions,
Instrumentation instrumentation) throws UploaderException, IOException {
if (agentOptions.useTestwiseCoverageMode()) {
return TestwiseCoverageAgent.create(agentOptions);
} else {
return new Agent(agentOptions, instrumentation);
}
}
/**
* Initializes debug logging during {@link #premain(String, Instrumentation)} and also logs the log directory if
* given.
*/
private static void initializeDebugLogging(AgentOptions agentOptions, DelayedLogger logger) {
loggingResources = LoggingUtils.initializeDebugLogging(agentOptions.getDebugLogDirectory());
Path logDirectory = Paths.get(new DebugLogDirectoryPropertyDefiner().getPropertyValue());
if (FileSystemUtils.isValidPath(logDirectory.toString()) && Files.isWritable(logDirectory)) {
logger.info("Logging to " + logDirectory);
} else {
logger.warn("Could not create " + logDirectory + ". Logging to console only.");
}
}
/**
* Initializes fallback logging in case of an error during the parsing of the options to
* {@link #premain(String, Instrumentation)} (see TS-23151). This tries to extract the logging configuration and use
* this and falls back to the default logger.
*/
private static LoggingUtils.LoggingResources initializeFallbackLogging(String premainOptions,
DelayedLogger delayedLogger) {
if (premainOptions == null) {
return LoggingUtils.initializeDefaultLogging();
}
for (String optionPart : premainOptions.split(",")) {
if (optionPart.startsWith(AgentOptionsParser.DEBUG + "=")) {
String value = optionPart.split("=", 2)[1];
boolean debugDisabled = value.equalsIgnoreCase("false");
boolean debugEnabled = value.equalsIgnoreCase("true");
if (debugDisabled) {
continue;
}
Path debugLogDirectory = null;
if (!value.isEmpty() && !debugEnabled) {
debugLogDirectory = Paths.get(value);
}
return LoggingUtils.initializeDebugLogging(debugLogDirectory);
}
if (optionPart.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")) {
return createFallbackLoggerFromConfig(optionPart.split("=", 2)[1], delayedLogger);
}
if (optionPart.startsWith(AgentOptionsParser.CONFIG_FILE_OPTION + "=")) {
String configFileValue = optionPart.split("=", 2)[1];
Optional<String> loggingConfigLine = Optional.empty();
try {
File configFile = new FilePatternResolver(delayedLogger).parsePath(
AgentOptionsParser.CONFIG_FILE_OPTION, configFileValue).toFile();
loggingConfigLine = FileSystemUtils.readLinesUTF8(configFile).stream()
.filter(line -> line.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "="))
.findFirst();
} catch (IOException e) {
delayedLogger.error("Failed to load configuration from " + configFileValue + ": " + e.getMessage(),
e);
}
if (loggingConfigLine.isPresent()) {
return createFallbackLoggerFromConfig(loggingConfigLine.get().split("=", 2)[1], delayedLogger);
}
}
}
return LoggingUtils.initializeDefaultLogging();
}
/** Creates a fallback logger using the given config file. */
private static LoggingUtils.LoggingResources createFallbackLoggerFromConfig(String configLocation,
ILogger delayedLogger) {
try {
return LoggingUtils.initializeLogging(
new FilePatternResolver(delayedLogger).parsePath(AgentOptionsParser.LOGGING_CONFIG_OPTION,
configLocation));
} catch (IOException e) {
String message = "Failed to load log configuration from location " + configLocation + ": " + e.getMessage();
delayedLogger.error(message, e);
// output the message to console as well, as this might
// otherwise not make it to the user
System.err.println(message);
return LoggingUtils.initializeDefaultLogging();
}
}
}