Date: Mon, 9 Oct 2023 21:13:04 +0200
Subject: [PATCH 19/21] Update
apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java
Co-authored-by: SylvainJuge <763082+SylvainJuge@users.noreply.github.com>
---
.../java/co/elastic/apm/agent/tracer/LifecycleListener.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java
index fe905248d1..3c13cae318 100644
--- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java
+++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/LifecycleListener.java
@@ -43,7 +43,7 @@ public interface LifecycleListener {
void start() throws Exception;
/**
- * Callback for when {@link co.elastic.apm.agent.tracer.Tracer} has been paused.
+ * Callback for when {@link Tracer} has been paused.
*
* Typically, this method is used to reduce overhead on the application to a minimum. This can be done by cleaning
* up resources like object pools, as well as by avoiding tracing-related overhead.
From 99255085c12b7ecb268e9a95819bc1c21b65ff0c Mon Sep 17 00:00:00 2001
From: Rafael Winterhalter
Date: Mon, 9 Oct 2023 21:29:55 +0200
Subject: [PATCH 20/21] Rename init lifecycle listener.
---
.../agent/bci/InstrumentationStatsLifecycleListener.java | 4 ++--
.../agent/configuration/ApmServerConfigurationSource.java | 4 ++--
.../co/elastic/apm/agent/configuration/StartupInfo.java | 4 ++--
...leLifecycleListener.java => InitLifecycleListener.java} | 2 +-
.../java/co/elastic/apm/agent/impl/ElasticApmTracer.java | 6 +++---
.../co/elastic/apm/agent/logging/ApmServerLogAppender.java | 7 +++----
.../test/java/co/elastic/apm/agent/impl/LifecycleTest.java | 4 ++--
7 files changed, 15 insertions(+), 16 deletions(-)
rename apm-agent-core/src/main/java/co/elastic/apm/agent/context/{InitializableLifecycleListener.java => InitLifecycleListener.java} (94%)
diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java
index 1429e79ae1..2e5e022870 100644
--- a/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java
+++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/bci/InstrumentationStatsLifecycleListener.java
@@ -19,7 +19,7 @@
package co.elastic.apm.agent.bci;
import co.elastic.apm.agent.bci.bytebuddy.MatcherTimer;
-import co.elastic.apm.agent.context.InitializableLifecycleListener;
+import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.sdk.logging.Logger;
@@ -28,7 +28,7 @@
import java.util.ArrayList;
import java.util.Collections;
-public class InstrumentationStatsLifecycleListener extends AbstractLifecycleListener implements InitializableLifecycleListener {
+public class InstrumentationStatsLifecycleListener extends AbstractLifecycleListener implements InitLifecycleListener {
private static final Logger logger = LoggerFactory.getLogger(InstrumentationStatsLifecycleListener.class);
@Override
diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java
index 604bb59f52..7afda0b25b 100644
--- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java
+++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ApmServerConfigurationSource.java
@@ -18,7 +18,7 @@
*/
package co.elastic.apm.agent.configuration;
-import co.elastic.apm.agent.context.InitializableLifecycleListener;
+import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.report.ApmServerClient;
import co.elastic.apm.agent.report.serialize.DslJsonSerializer;
@@ -44,7 +44,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-public class ApmServerConfigurationSource extends AbstractConfigurationSource implements InitializableLifecycleListener {
+public class ApmServerConfigurationSource extends AbstractConfigurationSource implements InitLifecycleListener {
// log correlation is enabled by default in Java agent, thus removing it from warnings
private static final Set IGNORED_REMOTE_KEYS = Collections.singleton("enable_log_correlation");
diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java
index 9033a82c35..27882d7610 100644
--- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java
+++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java
@@ -18,7 +18,7 @@
*/
package co.elastic.apm.agent.configuration;
-import co.elastic.apm.agent.context.InitializableLifecycleListener;
+import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.tracer.configuration.TimeDuration;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
@@ -39,7 +39,7 @@
* under Apache license 2.0.
*
*/
-public class StartupInfo extends AbstractLifecycleListener implements InitializableLifecycleListener {
+public class StartupInfo extends AbstractLifecycleListener implements InitLifecycleListener {
private static final Logger logger = LoggerFactory.getLogger(StartupInfo.class);
private final String elasticApmVersion;
diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitializableLifecycleListener.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitLifecycleListener.java
similarity index 94%
rename from apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitializableLifecycleListener.java
rename to apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitLifecycleListener.java
index 47c3f6e33d..97403f0547 100644
--- a/apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitializableLifecycleListener.java
+++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/context/InitLifecycleListener.java
@@ -21,7 +21,7 @@
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.LifecycleListener;
-public interface InitializableLifecycleListener extends LifecycleListener {
+public interface InitLifecycleListener extends LifecycleListener {
/**
* Callback for tracer initialization. As opposed to {@link LifecycleListener#start()}, which may
diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java
index 061ac6f91d..e68dbf6664 100644
--- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java
+++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java
@@ -26,7 +26,7 @@
import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.configuration.MetricsConfiguration;
import co.elastic.apm.agent.configuration.ServerlessConfiguration;
-import co.elastic.apm.agent.context.InitializableLifecycleListener;
+import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.impl.metadata.FaaSMetaDataExtension;
import co.elastic.apm.agent.impl.metadata.Framework;
import co.elastic.apm.agent.impl.metadata.NameAndIdField;
@@ -694,9 +694,9 @@ public List getActivationListeners() {
void init(List lifecycleListeners) {
this.lifecycleListeners.addAll(lifecycleListeners);
for (LifecycleListener lifecycleListener : lifecycleListeners) {
- if (lifecycleListener instanceof InitializableLifecycleListener) {
+ if (lifecycleListener instanceof InitLifecycleListener) {
try {
- ((InitializableLifecycleListener) lifecycleListener).init(this);
+ ((InitLifecycleListener) lifecycleListener).init(this);
} catch (Exception e) {
logger.error("Failed to init " + lifecycleListener.getClass().getName(), e);
}
diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java
index a7d35ac1fb..5cf4fa486c 100644
--- a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java
+++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/ApmServerLogAppender.java
@@ -18,9 +18,8 @@
*/
package co.elastic.apm.agent.logging;
-import co.elastic.apm.agent.context.InitializableLifecycleListener;
+import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
-import co.elastic.apm.agent.tracer.LifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.report.Reporter;
import co.elastic.logging.log4j2.EcsLayout;
@@ -101,8 +100,8 @@ public void append(LogEvent event) {
sendLogEvent(event);
}
- public InitializableLifecycleListener getInitListener() {
- class InitListener extends AbstractLifecycleListener implements InitializableLifecycleListener {
+ public InitLifecycleListener getInitListener() {
+ class InitListener extends AbstractLifecycleListener implements InitLifecycleListener {
@Override
public void init(ElasticApmTracer tracer) {
initStreaming(tracer.getConfig(LoggingConfiguration.class), tracer.getReporter());
diff --git a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java
index 2281ae3cab..e2f4ec032f 100644
--- a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java
+++ b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/LifecycleTest.java
@@ -21,7 +21,7 @@
import co.elastic.apm.agent.MockReporter;
import co.elastic.apm.agent.bci.ElasticApmAgent;
import co.elastic.apm.agent.configuration.SpyConfiguration;
-import co.elastic.apm.agent.context.InitializableLifecycleListener;
+import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.objectpool.TestObjectPoolFactory;
import net.bytebuddy.agent.ByteBuddyAgent;
@@ -141,7 +141,7 @@ void testStartDisabled() throws Exception {
ConfigurationRegistry configRegistry = SpyConfiguration.createSpyConfig(SimpleSource.forTest("enabled", "false"));
final AtomicBoolean initialized = new AtomicBoolean();
final AtomicBoolean started = new AtomicBoolean();
- class TestListener extends AbstractLifecycleListener implements InitializableLifecycleListener {
+ class TestListener extends AbstractLifecycleListener implements InitLifecycleListener {
@Override
public void init(ElasticApmTracer tracer) {
initialized.set(true);
From f06060f811b733b6d33cd135b2f7b20eb41e69e6 Mon Sep 17 00:00:00 2001
From: Rafael Winterhalter
Date: Mon, 9 Oct 2023 22:17:02 +0200
Subject: [PATCH 21/21] Add test for header caching.
---
.../apm/agent/impl/transaction/TraceContext.java | 10 +++++++++-
.../apm/agent/impl/transaction/TraceContextTest.java | 8 ++++++++
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/TraceContext.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/TraceContext.java
index 1d8b2f6388..019ae2e94a 100644
--- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/TraceContext.java
+++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/TraceContext.java
@@ -350,12 +350,20 @@ public void asRootSpan(Sampler sampler) {
cachedSampleRate = rate;
cachedHeader = TraceState.getHeaderValue(rate);
}
- traceState.set(rate, cachedHeader);
+ traceState.set(rate, getHeader(rate));
}
clock.init();
onMutation();
}
+ String getHeader(double rate) {
+ if (cachedHeader == null || cachedSampleRate != rate) {
+ cachedSampleRate = rate;
+ cachedHeader = TraceState.getHeaderValue(rate);
+ }
+ return cachedHeader;
+ }
+
public void asChildOf(TraceContext parent) {
traceId.copyFrom(parent.traceId);
parentId.copyFrom(parent.id);
diff --git a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/transaction/TraceContextTest.java b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/transaction/TraceContextTest.java
index 2042067555..f1d4f55023 100644
--- a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/transaction/TraceContextTest.java
+++ b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/transaction/TraceContextTest.java
@@ -646,4 +646,12 @@ void testSetServiceInfoWithNonEmptyServiceName() {
assertThat(traceContext.getServiceName()).isEqualTo("My Service");
assertThat(traceContext.getServiceVersion()).isEqualTo("My Version");
}
+
+ @Test
+ void testCachedTraceHeader() {
+ TraceContext traceContext = TraceContext.with64BitId(tracer);
+ String header = traceContext.getHeader(0.5);
+ assertThat(header).isNotNull().isSameAs(traceContext.getHeader(0.5));
+ assertThat(header).isNotEqualTo(traceContext.getHeader(1));
+ }
}