From 38b816a509e3f82712c4a7151dc736fd51e518e6 Mon Sep 17 00:00:00 2001 From: Florian Dreier Date: Tue, 19 Nov 2024 09:27:03 +0100 Subject: [PATCH] TS-41001 Fix warning about multiple agents being used --- CHANGELOG.md | 1 + .../com/teamscale/jacoco/agent/PreMain.java | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 456321282..642e0e571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ We use [semantic versioning](http://semver.org/): - PATCH version when you make backwards compatible bug fixes. # Next version +- [fix] _agent_: Warning about multiple agents being present is misleading # 34.2.0 - [fix] _agent_: `search-git-properties-recursively` was not considered when jar was given via `artifactory-git-properties-jar` diff --git a/agent/src/main/java/com/teamscale/jacoco/agent/PreMain.java b/agent/src/main/java/com/teamscale/jacoco/agent/PreMain.java index d3ef01be0..c27929814 100644 --- a/agent/src/main/java/com/teamscale/jacoco/agent/PreMain.java +++ b/agent/src/main/java/com/teamscale/jacoco/agent/PreMain.java @@ -42,8 +42,10 @@ public class PreMain { */ 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. */ + /** + * 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. */ @@ -91,12 +93,13 @@ public static void premain(String options, Instrumentation instrumentation) thro @NotNull private static AgentOptions getAndApplyAgentOptions(String options, String environmentConfigId, - String environmentConfigFile) throws AgentOptionParseException, IOException, AgentOptionReceiveException { + String environmentConfigFile) throws AgentOptionParseException, IOException, AgentOptionReceiveException { DelayedLogger delayedLogger = new DelayedLogger(); List javaAgents = CollectionUtils.filter(ManagementFactory.getRuntimeMXBean().getInputArguments(), s -> s.contains("-javaagent")); - if (javaAgents.size() > 1) { + // We allow multiple instances of the teamscale-jacoco-agent as we ensure with the #LOCKING_SYSTEM_PROPERTY to only use it once + if (!javaAgents.stream().allMatch(javaAgent -> javaAgent.contains("teamscale-jacoco-agent.jar"))) { delayedLogger.warn("Using multiple java agents could interfere with coverage recording."); } if (!javaAgents.get(0).contains("teamscale-jacoco-agent.jar")) { @@ -109,7 +112,8 @@ private static AgentOptions getAndApplyAgentOptions(String options, String envir } AgentOptions agentOptions; try { - agentOptions = AgentOptionsParser.parse(options, environmentConfigId, environmentConfigFile, credentials, delayedLogger); + agentOptions = AgentOptionsParser.parse(options, environmentConfigId, environmentConfigFile, credentials, + delayedLogger); } catch (AgentOptionParseException e) { try (LoggingUtils.LoggingResources ignored = initializeFallbackLogging(options, delayedLogger)) { delayedLogger.errorAndStdErr("Failed to parse agent options: " + e.getMessage(), e); @@ -161,7 +165,7 @@ static void closeLoggingResources() { * the HTTP server is used. */ private static AgentBase createAgent(AgentOptions agentOptions, - Instrumentation instrumentation) throws UploaderException, IOException { + Instrumentation instrumentation) throws UploaderException, IOException { if (agentOptions.useTestwiseCoverageMode()) { return TestwiseCoverageAgent.create(agentOptions); } else { @@ -189,7 +193,7 @@ private static void initializeDebugLogging(AgentOptions agentOptions, DelayedLog * this and falls back to the default logger. */ private static LoggingUtils.LoggingResources initializeFallbackLogging(String premainOptions, - DelayedLogger delayedLogger) { + DelayedLogger delayedLogger) { if (premainOptions == null) { return LoggingUtils.initializeDefaultLogging(); } @@ -222,7 +226,7 @@ private static LoggingUtils.LoggingResources initializeFallbackLogging(String pr /** Creates a fallback logger using the given config file. */ private static LoggingUtils.LoggingResources createFallbackLoggerFromConfig(String configLocation, - DelayedLogger delayedLogger) { + DelayedLogger delayedLogger) { try { return LoggingUtils.initializeLogging( new FilePatternResolver(delayedLogger).parsePath(AgentOptionsParser.LOGGING_CONFIG_OPTION,