Skip to content

Commit d706efa

Browse files
jeanbisuttitrask
andauthored
Improve agent already loaded check (#2403)
* Improve agent already loaded check * Renaming * Update agent/agent/src/main/java/com/microsoft/applicationinsights/agent/Agent.java Co-authored-by: Trask Stalnaker <[email protected]> * Don't reset alreadyLoaded value * Remove unused method Co-authored-by: Trask Stalnaker <[email protected]>
1 parent ecd6896 commit d706efa

File tree

1 file changed

+3
-3
lines changed
  • agent/agent/src/main/java/com/microsoft/applicationinsights/agent

1 file changed

+3
-3
lines changed

agent/agent/src/main/java/com/microsoft/applicationinsights/agent/Agent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import io.opentelemetry.javaagent.OpenTelemetryAgent;
2525
import java.lang.instrument.Instrumentation;
26+
import java.util.concurrent.atomic.AtomicBoolean;
2627

2728
// IMPORTANT!! If this class is renamed, be sure to add the previous name to
2829
// DuplicateAgentClassFileTransformer
@@ -34,10 +35,10 @@ public class Agent {
3435
// this is to prevent the agent from loading and instrumenting everything twice
3536
// (leading to unpredictable results) when -javaagent:applicationinsights-agent.jar
3637
// appears multiple times on the command line
37-
private static volatile boolean alreadyLoaded;
38+
private static final AtomicBoolean alreadyLoaded = new AtomicBoolean(false);
3839

3940
public static void premain(String agentArgs, Instrumentation inst) {
40-
if (alreadyLoaded) {
41+
if (alreadyLoaded.getAndSet(true)) {
4142
return;
4243
}
4344

@@ -46,7 +47,6 @@ public static void premain(String agentArgs, Instrumentation inst) {
4647
}
4748

4849
OpenTelemetryAgent.premain(agentArgs, inst);
49-
alreadyLoaded = true;
5050
}
5151

5252
// this is provided only for dynamic attach in the first line of main

0 commit comments

Comments
 (0)