-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPreMain.kt
More file actions
335 lines (300 loc) · 13 KB
/
PreMain.kt
File metadata and controls
335 lines (300 loc) · 13 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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.PreMain.premain
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.*
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 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
/** Container class for the premain entry point for the agent. */
object PreMain {
private var loggingResources: LoggingUtils.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 const val 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 const val CONFIG_ID_ENVIRONMENT_VARIABLE = "TEAMSCALE_JAVA_PROFILER_CONFIG_ID"
/** Environment variable from which to read the config file to use. */
private const val CONFIG_FILE_ENVIRONMENT_VARIABLE = "TEAMSCALE_JAVA_PROFILER_CONFIG_FILE"
/** Environment variable from which to read the Teamscale access token. */
private const val ACCESS_TOKEN_ENVIRONMENT_VARIABLE = "TEAMSCALE_ACCESS_TOKEN"
/**
* Entry point for the agent, called by the JVM.
*/
@Throws(Exception::class)
@JvmStatic
fun premain(options: String?, instrumentation: Instrumentation?) {
if (System.getProperty(LOCKING_SYSTEM_PROPERTY) != null) return
System.setProperty(LOCKING_SYSTEM_PROPERTY, "true")
val environmentConfigId = System.getenv(CONFIG_ID_ENVIRONMENT_VARIABLE)
val environmentConfigFile = System.getenv(CONFIG_FILE_ENVIRONMENT_VARIABLE)
if (options.isNullOrEmpty() && 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
}
var agentOptions: AgentOptions? = null
try {
val parseResult = getAndApplyAgentOptions(
options, environmentConfigId, environmentConfigFile
)
agentOptions = parseResult.first
// After parsing everything and configuring logging, we now
// can throw the caught exceptions.
parseResult.second.forEach { exception ->
throw exception
}
} catch (e: AgentOptionParseException) {
LoggingUtils.loggerContext.getLogger(PreMain::class.java).error(e.message, e)
// Flush logs to Teamscale, if configured.
closeLoggingResources()
// Unregister the profiler from Teamscale.
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) {
// 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
}
// Post-parse phase: runtime failures must not crash the profiled application.
try {
startProfiling(agentOptions, instrumentation)
} catch (t: Throwable) {
logStartupFailure(t)
}
}
/** Starts JaCoCo instrumentation and registers the agent. May throw at runtime. */
@Throws(Exception::class)
private fun startProfiling(agentOptions: AgentOptions, instrumentation: Instrumentation?) {
val logger = LoggingUtils.getLogger(Agent::class.java)
logger.info("Teamscale Java profiler version ${AgentUtils.VERSION}")
logger.info("Starting JaCoCo's agent")
val agentBuilder = JacocoAgentOptionsBuilder(agentOptions)
JaCoCoPreMain.premain(agentBuilder.createJacocoAgentOptions(), instrumentation, logger)
agentOptions.configurationViaTeamscale?.startHeartbeatThreadAndRegisterShutdownHook()
createAgent(agentOptions, instrumentation).registerShutdownHook()
}
/**
* Logs a startup failure on a best-effort basis. Must not throw under any circumstances.
*/
private fun logStartupFailure(t: Throwable) {
try {
LoggingUtils.getLogger(PreMain::class.java).error(
"Teamscale Java Profiler failed to start up. The profiled application will continue to run " +
"without coverage collection.", t
)
} catch (loggingFailure: Throwable) {
try {
System.err.println("[Teamscale Java Profiler] Failed to start up and will not collect coverage: $t")
loggingFailure.addSuppressed(t)
loggingFailure.printStackTrace()
} catch (_: Throwable) { }
}
}
@Throws(AgentOptionParseException::class, IOException::class, AgentOptionReceiveException::class)
private fun getAndApplyAgentOptions(
options: String?,
environmentConfigId: String?,
environmentConfigFile: String?
): Pair<AgentOptions, List<Exception>> {
val delayedLogger = DelayedLogger()
val javaAgents = ManagementFactory.getRuntimeMXBean().inputArguments
.filter { it.contains("-javaagent") }
// We allow multiple instances of the teamscale-jacoco-agent as we ensure with the #LOCKING_SYSTEM_PROPERTY to only use it once
val differentAgents = javaAgents.filter { !it.contains("teamscale-jacoco-agent.jar") }
if (!differentAgents.isEmpty()) {
delayedLogger.warn(
"Using multiple java agents could interfere with coverage recording: ${
AgentOptions.obfuscateAccessToken(differentAgents.joinToString())
}"
)
}
if (!javaAgents.first().contains("teamscale-jacoco-agent.jar")) {
delayedLogger.warn("For best results consider registering the Teamscale Java Profiler first.")
}
val 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."
)
}
val environmentAccessToken = System.getenv(ACCESS_TOKEN_ENVIRONMENT_VARIABLE)
val parseResult: Pair<AgentOptions, List<Exception>>
val agentOptions: AgentOptions
try {
parseResult = AgentOptionsParser.parse(
options, environmentConfigId, environmentConfigFile, credentials, environmentAccessToken, delayedLogger
)
agentOptions = parseResult.first
} catch (e: AgentOptionParseException) {
initializeFallbackLogging(options, delayedLogger).use { _ ->
delayedLogger.errorAndStdErr("Failed to parse agent options: ${e.message}", e)
attemptLogAndThrow(delayedLogger)
throw e
}
} catch (e: AgentOptionReceiveException) {
initializeFallbackLogging(options, delayedLogger).use { _ ->
delayedLogger.errorAndStdErr("${e.message} 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)
val logger = LoggingUtils.getLogger(Agent::class.java)
delayedLogger.logTo(logger)
HttpUtils.setShouldValidateSsl(agentOptions.validateSsl)
return parseResult
}
private fun 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)
val logger = LoggingUtils.getLogger(Agent::class.java)
delayedLogger.logTo(logger)
}
/** Initializes logging during [premain] and also logs the log directory. */
@Throws(IOException::class)
private fun initializeLogging(agentOptions: AgentOptions, logger: DelayedLogger) {
if (agentOptions.isDebugLogging) {
initializeDebugLogging(agentOptions, logger)
} else {
loggingResources = LoggingUtils.initializeLogging(agentOptions.loggingConfig)
logger.info("Logging to ${LogDirectoryPropertyDefiner().getPropertyValue()}")
}
if (agentOptions.teamscaleServer.isConfiguredForServerConnection) {
if (LogToTeamscaleAppender.addTeamscaleAppenderTo(LoggingUtils.loggerContext, agentOptions)) {
logger.info("Logs are being forwarded to Teamscale at ${agentOptions.teamscaleServer.url}")
}
}
}
/** Closes the opened logging contexts if initialized. */
fun closeLoggingResources() {
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.
*/
@Throws(UploaderException::class, IOException::class)
private fun createAgent(
agentOptions: AgentOptions,
instrumentation: Instrumentation?
): AgentBase = if (agentOptions.useTestwiseCoverageMode()) {
TestwiseCoverageAgent.create(agentOptions)
} else {
Agent(agentOptions, instrumentation)
}
/**
* Initializes debug logging during [.premain] and also logs the log directory if
* given.
*/
private fun initializeDebugLogging(agentOptions: AgentOptions, logger: DelayedLogger) {
loggingResources = LoggingUtils.initializeDebugLogging(agentOptions.debugLogDirectory)
val logDirectory = Paths.get(DebugLogDirectoryPropertyDefiner().getPropertyValue())
if (FileSystemUtils.isValidPath(logDirectory.toString()) && Files.isWritable(logDirectory)) {
logger.info("Logging to $logDirectory")
} else {
logger.warn(
"Could not create debug log directory '$logDirectory'" +
" (check permissions and free disk space). Falling back to console-only logging."
)
}
}
/**
* Initializes fallback logging in case of an error during the parsing of the options to
* [premain] (see TS-23151). This tries to extract the logging configuration and use
* this and falls back to the default logger.
*/
private fun initializeFallbackLogging(
premainOptions: String?,
delayedLogger: DelayedLogger
): LoggingUtils.LoggingResources? {
if (premainOptions == null) {
return LoggingUtils.initializeDefaultLogging()
}
premainOptions
.split(",".toRegex())
.dropLastWhile { it.isEmpty() }
.forEach { optionPart ->
if (optionPart.startsWith(AgentOptionsParser.DEBUG + "=")) {
val value = optionPart.split("=".toRegex(), limit = 2)[1]
val debugDisabled = value.equals("false", ignoreCase = true)
val debugEnabled = value.equals("true", ignoreCase = true)
if (debugDisabled) return@forEach
var debugLogDirectory: Path? = null
if (!value.isEmpty() && !debugEnabled) {
debugLogDirectory = Paths.get(value)
}
return LoggingUtils.initializeDebugLogging(debugLogDirectory)
}
if (optionPart.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")) {
return createFallbackLoggerFromConfig(
optionPart.split("=".toRegex(), limit = 2)[1],
delayedLogger
)
}
if (optionPart.startsWith(AgentOptionsParser.CONFIG_FILE_OPTION + "=")) {
val configFileValue = optionPart.split("=".toRegex(), limit = 2)[1]
var loggingConfigLine: String? = null
try {
val configFile = FilePatternResolver(delayedLogger).parsePath(
AgentOptionsParser.CONFIG_FILE_OPTION, configFileValue
).toFile()
loggingConfigLine = configFile.readLines()
.firstOrNull { it.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=") }
} catch (e: IOException) {
delayedLogger.error("Failed to load configuration from $configFileValue: ${e.message}", e)
}
loggingConfigLine?.let { config ->
return createFallbackLoggerFromConfig(
config.split("=".toRegex(), limit = 2)[1], delayedLogger
)
}
}
}
return LoggingUtils.initializeDefaultLogging()
}
/** Creates a fallback logger using the given config file. */
private fun createFallbackLoggerFromConfig(
configLocation: String,
delayedLogger: ILogger
): LoggingUtils.LoggingResources {
try {
return LoggingUtils.initializeLogging(
FilePatternResolver(delayedLogger).parsePath(
AgentOptionsParser.LOGGING_CONFIG_OPTION,
configLocation
)
)
} catch (e: IOException) {
val message = "Failed to load log configuration from location $configLocation: ${e.message}"
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()
}
}
}