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 20ca144b66..da3c755226 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 @@ -64,6 +64,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ScheduledThreadPoolExecutor; @@ -916,4 +917,9 @@ public T require(Class type) { } return cast; } + + @Override + public Set getTraceHeaderNames() { + return TraceContext.TRACE_TEXTUAL_HEADERS; + } } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/AbstractSpan.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/AbstractSpan.java index 36ccb6af94..0b851e8a55 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/AbstractSpan.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/AbstractSpan.java @@ -45,11 +45,6 @@ import java.util.concurrent.atomic.AtomicLong; public abstract class AbstractSpan> implements Recyclable, ElasticContext, co.elastic.apm.agent.tracer.AbstractSpan { - public static final int PRIO_USER_SUPPLIED = 1000; - public static final int PRIO_HIGH_LEVEL_FRAMEWORK = 100; - public static final int PRIO_METHOD_SIGNATURE = 100; - public static final int PRIO_LOW_LEVEL_FRAMEWORK = 10; - public static final int PRIO_DEFAULT = 0; private static final Logger logger = LoggerFactory.getLogger(AbstractSpan.class); private static final Logger oneTimeDuplicatedEndLogger = LoggerUtils.logOnce(logger); private static final Logger oneTimeMaxSpanLinksLogger = LoggerUtils.logOnce(logger); @@ -69,7 +64,7 @@ public abstract class AbstractSpan> implements Recycla private ChildDurationTimer childDurations = new ChildDurationTimer(); protected AtomicInteger references = new AtomicInteger(); protected volatile boolean finished = true; - private int namePriority = PRIO_DEFAULT; + private int namePriority = PRIORITY_DEFAULT; private boolean discardRequested = false; /** * Flag to mark a span as representing an exit event @@ -272,7 +267,7 @@ public StringBuilder getAndOverrideName(int namePriority, boolean overrideIfSame * @param methodName the method that should be part of this span's name */ public void updateName(Class clazz, String methodName) { - StringBuilder spanName = getAndOverrideName(PRIO_DEFAULT); + StringBuilder spanName = getAndOverrideName(PRIORITY_DEFAULT); if (spanName != null) { String className = clazz.getName(); spanName.append(className, className.lastIndexOf('.') + 1, className.length()); @@ -291,7 +286,7 @@ public String getNameAsString() { @Override public T appendToName(CharSequence cs) { - return appendToName(cs, PRIO_DEFAULT); + return appendToName(cs, PRIORITY_DEFAULT); } @Override @@ -310,7 +305,7 @@ public T appendToName(CharSequence cs, int priority, int startIndex, int endInde @Override public T withName(@Nullable String name) { - return withName(name, PRIO_DEFAULT); + return withName(name, PRIORITY_DEFAULT); } @Override @@ -451,7 +446,7 @@ public void resetState() { traceContext.resetState(); childDurations.resetState(); references.set(0); - namePriority = PRIO_DEFAULT; + namePriority = PRIORITY_DEFAULT; discardRequested = false; isExit = false; childIds = null; 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 e24e4ff983..89b2acea69 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 @@ -35,7 +35,10 @@ import javax.annotation.Nullable; import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.HashSet; import java.util.Objects; +import java.util.Set; /** * This is an implementation of the @@ -82,7 +85,6 @@ public class TraceContext implements Recyclable, co.elastic.apm.agent.tracer.Tra private static final int TEXT_HEADER_PARENT_ID_OFFSET = 36; private static final int TEXT_HEADER_FLAGS_OFFSET = 53; - public static final String TRACE_PARENT_BINARY_HEADER_NAME = "elasticapmtraceparent"; public static final int BINARY_FORMAT_EXPECTED_LENGTH = 29; private static final byte BINARY_FORMAT_CURRENT_VERSION = (byte) 0b0000_0000; // one byte for the trace-id field id (0x00), followed by 16 bytes of the actual ID @@ -98,6 +100,16 @@ public class TraceContext implements Recyclable, co.elastic.apm.agent.tracer.Tra private static final Double SAMPLE_RATE_ZERO = 0d; + public static final Set TRACE_TEXTUAL_HEADERS; + + static { + Set traceParentTextualHeaders = new HashSet<>(); + traceParentTextualHeaders.add(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); + traceParentTextualHeaders.add(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME); + traceParentTextualHeaders.add(TRACESTATE_HEADER_NAME); + TRACE_TEXTUAL_HEADERS = Collections.unmodifiableSet(traceParentTextualHeaders); + } + private static final ChildContextCreator FROM_PARENT_CONTEXT = new ChildContextCreator() { @Override public boolean asChildOf(TraceContext child, TraceContext parent) { @@ -157,7 +169,7 @@ public boolean asChildOf(TraceContext child, @Nullable Object carrier, HeaderGet if (carrier == null) { return false; } - byte[] traceparent = traceContextHeaderGetter.getFirstHeader(TRACE_PARENT_BINARY_HEADER_NAME, carrier); + byte[] traceparent = traceContextHeaderGetter.getFirstHeader(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier); if (traceparent != null) { return child.asChildOf(traceparent); } @@ -184,6 +196,7 @@ public boolean asChildOf(TraceContext child, Object ignore) { public static boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { + // We assume that this header is always present if we found any of the other headers. return headerGetter.getFirstHeader(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier) != null; } @@ -191,7 +204,6 @@ public static void removeTraceContextHeaders(C carrier, HeaderRemover hea headerRemover.remove(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier); headerRemover.remove(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier); headerRemover.remove(TRACESTATE_HEADER_NAME, carrier); - headerRemover.remove(TRACE_PARENT_BINARY_HEADER_NAME, carrier); } public static void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { @@ -576,7 +588,7 @@ boolean propagateTraceContext(C carrier, BinaryHeaderSetter headerSetter) logger.debug("Outgoing TraceContext header injection is disabled"); return false; } - byte[] buffer = headerSetter.getFixedLengthByteArray(TRACE_PARENT_BINARY_HEADER_NAME, BINARY_FORMAT_EXPECTED_LENGTH); + byte[] buffer = headerSetter.getFixedLengthByteArray(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, BINARY_FORMAT_EXPECTED_LENGTH); if (buffer == null || buffer.length != BINARY_FORMAT_EXPECTED_LENGTH) { logger.warn("Header setter {} failed to provide a byte buffer with the proper length. Allocating a buffer for each header.", headerSetter.getClass().getName()); @@ -584,7 +596,7 @@ boolean propagateTraceContext(C carrier, BinaryHeaderSetter headerSetter) } boolean headerBufferFilled = fillOutgoingTraceParentBinaryHeader(buffer); if (headerBufferFilled) { - headerSetter.setHeader(TRACE_PARENT_BINARY_HEADER_NAME, buffer, carrier); + headerSetter.setHeader(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, buffer, carrier); } return headerBufferFilled; } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/Transaction.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/Transaction.java index 38f0b0b3bf..56298222de 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/Transaction.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/Transaction.java @@ -52,8 +52,6 @@ protected Labels.Mutable initialValue() { } }; - public static final String TYPE_REQUEST = "request"; - /** * Context *

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 853586115d..8de27f8ff2 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 @@ -185,14 +185,6 @@ void testTraceContextTextHeadersRemoval() { assertThat(headerMap.get(TraceContext.TRACESTATE_HEADER_NAME)).isNull(); } - @Test - void testTraceContextBinaryHeadersRemoval() { - Map headerMap = new HashMap<>(); - headerMap.put(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01".getBytes(StandardCharsets.UTF_8)); - TraceContext.removeTraceContextHeaders(headerMap, BinaryHeaderMapAccessor.INSTANCE); - assertThat(headerMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isNull(); - } - @Test void testTraceContextHeadersCopy() { Map original = new HashMap<>(); @@ -287,9 +279,9 @@ void testBinaryHeaderCaching() { assertThat(TraceContext.>getFromTraceContextTextHeaders().asChildOf(child, headerMap, TextHeaderMapAccessor.INSTANCE)).isTrue(); HashMap binaryHeaderMap = new HashMap<>(); assertThat(child.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE)).isTrue(); - byte[] outgoingHeader = binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME); + byte[] outgoingHeader = binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); assertThat(child.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE)).isTrue(); - assertThat(binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isSameAs(outgoingHeader); + assertThat(binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isSameAs(outgoingHeader); } @Test @@ -310,9 +302,9 @@ public void setHeader(String headerName, byte[] headerValue, Map }; HashMap binaryHeaderMap = new HashMap<>(); assertThat(child.propagateTraceContext(binaryHeaderMap, headerSetter)).isTrue(); - byte[] outgoingHeader = binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME); + byte[] outgoingHeader = binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); assertThat(child.propagateTraceContext(binaryHeaderMap, headerSetter)).isTrue(); - assertThat(binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isNotSameAs(outgoingHeader); + assertThat(binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotSameAs(outgoingHeader); } private void verifyTraceContextContents(String traceContext, String expectedTraceId, String expectedParentId, @@ -349,7 +341,7 @@ void outgoingHeader() { "0af7651916cd43dd8448eb211c80319c", parentId, "00", "03"); Map headerMap = new HashMap<>(); assertThat(traceContext.propagateTraceContext(headerMap, BinaryHeaderMapAccessor.INSTANCE)).isTrue(); - verifyTraceContextContents(headerMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME), + verifyTraceContextContents(headerMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), "0af7651916cd43dd8448eb211c80319c", parentId, (byte) 0x00, (byte) 0x03); } @@ -364,7 +356,7 @@ void outgoingHeaderRootSpan() { traceContext.getId().toString(), "00", "01"); Map headerMap = new HashMap<>(); assertThat(traceContext.propagateTraceContext(headerMap, BinaryHeaderMapAccessor.INSTANCE)).isTrue(); - verifyTraceContextContents(headerMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME), traceContext.getTraceId().toString(), + verifyTraceContextContents(headerMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), traceContext.getTraceId().toString(), traceContext.getId().toString(), (byte) 0x00, (byte) 0x01); } @@ -398,7 +390,7 @@ void testResetOutgoingBinaryHeader() { final TraceContext traceContext = TraceContext.with64BitId(tracer); Map headerMap = new HashMap<>(); assertThat(traceContext.propagateTraceContext(headerMap, BinaryHeaderMapAccessor.INSTANCE)).isTrue(); - byte[] outgoingByteHeader = headerMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME); + byte[] outgoingByteHeader = headerMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); byte[] tmp = new byte[outgoingByteHeader.length]; System.arraycopy(outgoingByteHeader, 0, tmp, 0, outgoingByteHeader.length); traceContext.asChildOf("00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-00"); @@ -429,12 +421,12 @@ void testCopyFrom() { assertThat(first.getOutgoingTraceParentTextHeader()).isNotEqualTo(second.getOutgoingTraceParentTextHeader()); Map binaryHeaderMap = new HashMap<>(); first.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE); - byte[] outgoingHeader = binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME); + byte[] outgoingHeader = binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); // We must copy because of the byte array caching in BinaryHeaderMapAccessor byte[] firstOutgoingHeader = new byte[outgoingHeader.length]; System.arraycopy(outgoingHeader, 0, firstOutgoingHeader, 0, outgoingHeader.length); second.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE); - assertThat(binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isNotEqualTo(firstOutgoingHeader); + assertThat(binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotEqualTo(firstOutgoingHeader); second.copyFrom(first); assertThat(first.getTraceId()).isEqualTo(second.getTraceId()); @@ -442,7 +434,7 @@ void testCopyFrom() { assertThat(first.isSampled()).isEqualTo(second.isSampled()); assertThat(first.getOutgoingTraceParentTextHeader().toString()).isEqualTo(second.getOutgoingTraceParentTextHeader().toString()); second.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE); - assertThat(binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isEqualTo(firstOutgoingHeader); + assertThat(binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isEqualTo(firstOutgoingHeader); } @Test @@ -593,7 +585,7 @@ void testPropagateTransactionIdForUnsampledSpan() { childContext.getTraceId().toString(), rootContext.getId().toString(), "00", "00"); Map binaryHeaderMap = new HashMap<>(); childContext.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE); - verifyTraceContextContents(binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME), + verifyTraceContextContents(binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), childContext.getTraceId().toString(), rootContext.getId().toString(), (byte) 0x00, (byte) 0x00); } @@ -609,7 +601,7 @@ void testPropagateSpanIdForSampledSpan() { childContext.getTraceId().toString(), childContext.getId().toString(), "00", "01"); Map binaryHeaderMap = new HashMap<>(); childContext.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE); - verifyTraceContextContents(binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME), + verifyTraceContextContents(binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), childContext.getTraceId().toString(), childContext.getId().toString(), (byte) 0x00, (byte) 0x01); } @@ -705,7 +697,7 @@ private void assertValid(byte[] binaryHeader) { assertThat(traceContext.asChildOf(binaryHeader)).isTrue(); Map binaryHeaderMap = new HashMap<>(); traceContext.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE); - verifyTraceContextContents(binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME), + verifyTraceContextContents(binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), traceContext.getTraceId().toString(), traceContext.getId().toString(), (byte) 0x00, binaryHeader[28]); } @@ -714,7 +706,7 @@ private byte[] convertToBinary(String textHeader) { traceContext.asChildOf(textHeader); Map binaryHeaderMap = new HashMap<>(); traceContext.propagateTraceContext(binaryHeaderMap, BinaryHeaderMapAccessor.INSTANCE); - byte[] binaryHeader = binaryHeaderMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME); + byte[] binaryHeader = binaryHeaderMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); // replace the version and parent ID HexUtils.decode(textHeader, 0, 2, binaryHeader, 0); HexUtils.decode(textHeader, 36, 16, binaryHeader, 19); diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient3-plugin/src/main/java/co/elastic/apm/agent/httpclient/v3/HttpClient3Instrumentation.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient3-plugin/src/main/java/co/elastic/apm/agent/httpclient/v3/HttpClient3Instrumentation.java index ea8bce84c4..280d2673d1 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient3-plugin/src/main/java/co/elastic/apm/agent/httpclient/v3/HttpClient3Instrumentation.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient3-plugin/src/main/java/co/elastic/apm/agent/httpclient/v3/HttpClient3Instrumentation.java @@ -20,12 +20,12 @@ import co.elastic.apm.agent.bci.TracerAwareInstrumentation; import co.elastic.apm.agent.httpclient.HttpClientHelper; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import co.elastic.apm.agent.util.LoggerUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; @@ -121,10 +121,10 @@ public static Object onEnter(@Advice.Argument(0) HttpMethod httpMethod, span.activate(); } - if (!TraceContext.containsTraceContextTextHeaders(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + } else if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { // re-adds the header on redirects parent.propagateTraceContext(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE); } diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpAsyncClientRedirectInstrumentation.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpAsyncClientRedirectInstrumentation.java index aacfb1cdf2..6e9afb0e07 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpAsyncClientRedirectInstrumentation.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpAsyncClientRedirectInstrumentation.java @@ -19,7 +19,7 @@ package co.elastic.apm.agent.httpclient.v4; import co.elastic.apm.agent.httpclient.v4.helper.RequestHeaderAccessor; -import co.elastic.apm.agent.impl.transaction.TraceContext; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -49,8 +49,8 @@ public static void onAfterExecute(@Advice.Argument(value = 0) HttpRequest origin return; } // org.apache.http.HttpMessage#containsHeader implementations do not allocate iterator since 4.0.1 - if (TraceContext.containsTraceContextTextHeaders(original, RequestHeaderAccessor.INSTANCE) && !TraceContext.containsTraceContextTextHeaders(redirect, RequestHeaderAccessor.INSTANCE)) { - TraceContext.copyTraceContextTextHeaders(original, RequestHeaderAccessor.INSTANCE, redirect, RequestHeaderAccessor.INSTANCE); + if (HeaderUtils.containsAny(tracer.getTraceHeaderNames(), original, RequestHeaderAccessor.INSTANCE) && !HeaderUtils.containsAny(tracer.getTraceHeaderNames(), redirect, RequestHeaderAccessor.INSTANCE)) { + HeaderUtils.copy(tracer.getTraceHeaderNames(), original, RequestHeaderAccessor.INSTANCE, redirect, RequestHeaderAccessor.INSTANCE); } } } diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpClientInstrumentation.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpClientInstrumentation.java index 66f7f14213..2ccdab635b 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpClientInstrumentation.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/ApacheHttpClientInstrumentation.java @@ -23,7 +23,7 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -64,10 +64,10 @@ public static Object onBeforeExecute(@Advice.Argument(0) HttpRoute route, span.activate(); } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { // re-adds the header on redirects parent.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/LegacyApacheHttpClientInstrumentation.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/LegacyApacheHttpClientInstrumentation.java index 161eea79b9..b05abf5172 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/LegacyApacheHttpClientInstrumentation.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/LegacyApacheHttpClientInstrumentation.java @@ -23,7 +23,7 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -106,10 +106,10 @@ public static Object onBeforeExecute(@Advice.Argument(0) @Nullable HttpHost host span.activate(); } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { // re-adds the header on redirects parent.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/ApacheHttpAsyncClientHelper.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/ApacheHttpAsyncClientHelper.java index 76c9fa9d26..7dd9c313c4 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/ApacheHttpAsyncClientHelper.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/ApacheHttpAsyncClientHelper.java @@ -21,6 +21,7 @@ import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Span; +import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.pooling.Allocator; import co.elastic.apm.agent.tracer.pooling.ObjectPool; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; @@ -34,15 +35,21 @@ public class ApacheHttpAsyncClientHelper { private static final int MAX_POOLED_ELEMENTS = 256; + private final Tracer tracer; private final ObjectPool requestProducerWrapperObjectPool; private final ObjectPool> futureCallbackWrapperObjectPool; public ApacheHttpAsyncClientHelper() { - ObjectPoolFactory factory = GlobalTracer.get().getObjectPoolFactory(); + tracer = GlobalTracer.get(); + ObjectPoolFactory factory = tracer.getObjectPoolFactory(); requestProducerWrapperObjectPool = factory.createRecyclableObjectPool(MAX_POOLED_ELEMENTS, new RequestProducerWrapperAllocator()); futureCallbackWrapperObjectPool = factory.createRecyclableObjectPool(MAX_POOLED_ELEMENTS, new FutureCallbackWrapperAllocator()); } + public Tracer getTracer() { + return tracer; + } + private class RequestProducerWrapperAllocator implements Allocator { @Override public HttpAsyncRequestProducerWrapper createInstance() { diff --git a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/HttpAsyncRequestProducerWrapper.java b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/HttpAsyncRequestProducerWrapper.java index 12752fe00e..1739e01ec2 100644 --- a/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/HttpAsyncRequestProducerWrapper.java +++ b/apm-agent-plugins/apm-apache-httpclient/apm-apache-httpclient4-plugin/src/main/java/co/elastic/apm/agent/httpclient/v4/helper/HttpAsyncRequestProducerWrapper.java @@ -21,7 +21,7 @@ import co.elastic.apm.agent.httpclient.HttpClientHelper; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import co.elastic.apm.agent.tracer.pooling.Recyclable; import org.apache.http.HttpException; import org.apache.http.HttpHost; @@ -95,7 +95,7 @@ public HttpRequest generateRequest() throws IOException, HttpException { } } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(asyncClientHelper.getTracer().getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } else if (parent != null) { diff --git a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/AbstractSpanInstrumentation.java b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/AbstractSpanInstrumentation.java index 042f16e451..21aa3f4f89 100644 --- a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/AbstractSpanInstrumentation.java +++ b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/AbstractSpanInstrumentation.java @@ -32,7 +32,7 @@ import javax.annotation.Nullable; import java.lang.invoke.MethodHandle; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_USER_SUPPLIED; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_USER_SUPPLIED; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.returns; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; @@ -72,7 +72,7 @@ public static class AdviceClass { public static void setName(@Advice.FieldValue(value = "span", typing = Assigner.Typing.DYNAMIC) Object context, @Advice.Argument(0) String name) { if (context instanceof AbstractSpan) { - ((AbstractSpan) context).withName(name, PRIO_USER_SUPPLIED); + ((AbstractSpan) context).withName(name, PRIORITY_USER_SUPPLIED); } } } diff --git a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureTransactionInstrumentation.java b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureTransactionInstrumentation.java index fe3f59f280..969acb6a46 100644 --- a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureTransactionInstrumentation.java +++ b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/CaptureTransactionInstrumentation.java @@ -46,8 +46,8 @@ import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isInAnyPackage; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isProxy; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.overridesOrImplementsMethodThat; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_METHOD_SIGNATURE; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_USER_SUPPLIED; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_METHOD_SIGNATURE; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_USER_SUPPLIED; import static co.elastic.apm.agent.pluginapi.ElasticApmApiInstrumentation.PUBLIC_API_INSTRUMENTATION_GROUP; import static co.elastic.apm.agent.pluginapi.Utils.FRAMEWORK_NAME; import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; @@ -81,9 +81,9 @@ public static Object onMethodEnter(@Advice.Origin Class clazz, Transaction transaction = tracer.startRootTransaction(PrivilegedActionUtils.getClassLoader(clazz)); if (transaction != null) { if (transactionName.isEmpty()) { - transaction.withName(signature, PRIO_METHOD_SIGNATURE); + transaction.withName(signature, PRIORITY_METHOD_SIGNATURE); } else { - transaction.withName(transactionName, PRIO_USER_SUPPLIED); + transaction.withName(transactionName, PRIORITY_USER_SUPPLIED); } transaction.withType(type) .activate(); diff --git a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/LegacySpanInstrumentation.java b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/LegacySpanInstrumentation.java index 6f468d3784..38a24d6e94 100644 --- a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/LegacySpanInstrumentation.java +++ b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/LegacySpanInstrumentation.java @@ -27,7 +27,7 @@ import javax.annotation.Nullable; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_USER_SUPPLIED; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_USER_SUPPLIED; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.not; @@ -71,7 +71,7 @@ public static class AdviceClass { public static void setName(@Advice.FieldValue(value = "span", typing = Assigner.Typing.DYNAMIC) Object span, @Advice.Argument(0) String name) { if (span instanceof Span) { - ((Span) span).withName(name, PRIO_USER_SUPPLIED); + ((Span) span).withName(name, PRIORITY_USER_SUPPLIED); } } } diff --git a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/TracedInstrumentation.java b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/TracedInstrumentation.java index ec21146e65..eae5026f25 100644 --- a/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/TracedInstrumentation.java +++ b/apm-agent-plugins/apm-api-plugin/src/main/java/co/elastic/apm/agent/pluginapi/TracedInstrumentation.java @@ -46,8 +46,8 @@ import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isInAnyPackage; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isProxy; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.overridesOrImplementsMethodThat; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_METHOD_SIGNATURE; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_USER_SUPPLIED; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_METHOD_SIGNATURE; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_USER_SUPPLIED; import static co.elastic.apm.agent.pluginapi.ElasticApmApiInstrumentation.PUBLIC_API_INSTRUMENTATION_GROUP; import static co.elastic.apm.agent.pluginapi.Utils.FRAMEWORK_NAME; import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; @@ -115,13 +115,13 @@ public static Object onMethodEnter( int namePriority; if (spanName.isEmpty()) { name = signature; - namePriority = PRIO_METHOD_SIGNATURE; + namePriority = PRIORITY_METHOD_SIGNATURE; } else { name = spanName; - namePriority = PRIO_USER_SUPPLIED; + namePriority = PRIORITY_USER_SUPPLIED; } return transaction.withName(name, namePriority) - .withType(type.isEmpty() ? co.elastic.apm.agent.impl.transaction.Transaction.TYPE_REQUEST : type) + .withType(type.isEmpty() ? Transaction.TYPE_REQUEST : type) .activate(); } diff --git a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/main/java/co/elastic/apm/agent/awssdk/v1/helper/SQSHelper.java b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/main/java/co/elastic/apm/agent/awssdk/v1/helper/SQSHelper.java index 3b1da59c60..bedf93cb05 100644 --- a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/main/java/co/elastic/apm/agent/awssdk/v1/helper/SQSHelper.java +++ b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-1-plugin/src/main/java/co/elastic/apm/agent/awssdk/v1/helper/SQSHelper.java @@ -24,7 +24,6 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.common.util.WildcardMatcher; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; @@ -72,12 +71,10 @@ public void propagateContext(Span span, AmazonWebServiceRequest request) { public void setMessageAttributeNames(ReceiveMessageRequest receiveMessageRequest) { List messageAttributeNames = receiveMessageRequest.getMessageAttributeNames(); - if (!messageAttributeNames.contains(ATTRIBUTE_NAME_ALL) && !messageAttributeNames.contains(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME)) { - messageAttributeNames.add(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME); - } - - if (!messageAttributeNames.contains(ATTRIBUTE_NAME_ALL) && !messageAttributeNames.contains(TraceContext.TRACESTATE_HEADER_NAME)) { - messageAttributeNames.add(TraceContext.TRACESTATE_HEADER_NAME); + for (String header : tracer.getTraceHeaderNames()) { + if (!messageAttributeNames.contains(ATTRIBUTE_NAME_ALL) && !messageAttributeNames.contains(header)) { + messageAttributeNames.add(header); + } } List attributeNames = receiveMessageRequest.getAttributeNames(); @@ -142,8 +139,7 @@ protected void setMessageContext(@Nullable Message sqsMessage, @Nullable String if (coreConfiguration.isCaptureHeaders()) { for (Map.Entry entry : sqsMessage.getMessageAttributes().entrySet()) { String key = entry.getKey(); - if (!TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME.equals(key) && - !TraceContext.TRACESTATE_HEADER_NAME.equals(key) && + if (!tracer.getTraceHeaderNames().contains(key) && entry.getValue().getDataType().equals(ATTRIBUTE_DATA_TYPE_STRING) && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) { message.addHeader(key, entry.getValue().getStringValue()); diff --git a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/main/java/co/elastic/apm/agent/awssdk/v2/helper/SQSHelper.java b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/main/java/co/elastic/apm/agent/awssdk/v2/helper/SQSHelper.java index 4db9b2794e..8ad8b5c8ad 100644 --- a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/main/java/co/elastic/apm/agent/awssdk/v2/helper/SQSHelper.java +++ b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-2-plugin/src/main/java/co/elastic/apm/agent/awssdk/v2/helper/SQSHelper.java @@ -21,7 +21,6 @@ import co.elastic.apm.agent.awssdk.common.AbstractSQSInstrumentationHelper; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import software.amazon.awssdk.core.SdkRequest; @@ -127,13 +126,11 @@ public void modifyRequestObject(@Nullable Span span, ClientExecutionParams cl } else if (sdkRequest instanceof ReceiveMessageRequest) { ReceiveMessageRequest receiveMessageRequest = (ReceiveMessageRequest) sdkRequest; if (!receiveMessageRequest.messageAttributeNames().contains(ATTRIBUTE_NAME_ALL) && - !(receiveMessageRequest.messageAttributeNames().contains(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME) && - receiveMessageRequest.messageAttributeNames().contains(TraceContext.TRACESTATE_HEADER_NAME))) { + Collections.disjoint(receiveMessageRequest.messageAttributeNames(), tracer.getTraceHeaderNames())) { List newMessageAttributeNames = new ArrayList<>(receiveMessageRequest.messageAttributeNames().size() + 2); newMessageAttributeNames.addAll(receiveMessageRequest.messageAttributeNames()); - newMessageAttributeNames.add(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME); - newMessageAttributeNames.add(TraceContext.TRACESTATE_HEADER_NAME); + newMessageAttributeNames.addAll(tracer.getTraceHeaderNames()); List attributeNames; if (receiveMessageRequest.attributeNamesAsStrings().isEmpty()) { diff --git a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractDynamoDBInstrumentationHelper.java b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractDynamoDBInstrumentationHelper.java index ec94f3c0d9..2c9a9d61b3 100644 --- a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractDynamoDBInstrumentationHelper.java +++ b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractDynamoDBInstrumentationHelper.java @@ -49,7 +49,7 @@ public void enrichSpan(Span span, R sdkRequest, URI httpURI, C context) { } - StringBuilder name = span.getAndOverrideName(co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT); + StringBuilder name = span.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (name != null) { name.append("DynamoDB ").append(operationName); diff --git a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractS3InstrumentationHelper.java b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractS3InstrumentationHelper.java index 97de53a900..1e7dfdaef0 100644 --- a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractS3InstrumentationHelper.java +++ b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractS3InstrumentationHelper.java @@ -49,14 +49,14 @@ public Span startSpan(R request, URI httpURI, C context) { span.withType("storage") .withSubtype(S3_TYPE) .withAction(operationName); - StringBuilder name = span.getAndOverrideName(co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT); + StringBuilder name = span.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (name != null) { name.append("S3 ").append(operationName); if (bucketName != null && !bucketName.isEmpty()) { name.append(" ").append(bucketName); } } - span.withName("S3", co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT - 1); + span.withName("S3", AbstractSpan.PRIORITY_DEFAULT - 1); setDestinationContext(span, httpURI, request, context, S3_TYPE, bucketName); return span; diff --git a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractSQSInstrumentationHelper.java b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractSQSInstrumentationHelper.java index f08796b627..1c504d9b43 100644 --- a/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractSQSInstrumentationHelper.java +++ b/apm-agent-plugins/apm-aws-sdk/apm-aws-sdk-common/src/main/java/co/elastic/apm/agent/awssdk/common/AbstractSQSInstrumentationHelper.java @@ -22,7 +22,6 @@ import co.elastic.apm.agent.configuration.MessagingConfiguration; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -118,14 +117,14 @@ public void enrichSpan(Span span, R request, URI httpURI, C context) { .withAction(action); if (span.isSampled()) { - StringBuilder name = span.getAndOverrideName(co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT); + StringBuilder name = span.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (name != null) { name.append("SQS ").append(spanNameOperation); if (queueName != null && !queueName.isEmpty()) { name.append(" ").append(queueName); } } - span.withName("SQS", co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT - 1); + span.withName("SQS", AbstractSpan.PRIORITY_DEFAULT - 1); if (queueName != null) { span.getContext().getMessage() @@ -194,8 +193,7 @@ protected void setMessageContext(@Nullable MessageT sqsMessage, @Nullable String if (coreConfiguration.isCaptureHeaders()) { for (String key : getMessageAttributeKeys(sqsMessage)) { String value = getMessageAttribute(sqsMessage, key); - if (!TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME.equals(key) && - !TraceContext.TRACESTATE_HEADER_NAME.equals(key) && + if (!tracer.getTraceHeaderNames().contains(key) && value != null && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) { message.addHeader(key, value); diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java index 6f98c79874..fdce366e23 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractAPIGatewayTransactionHelper.java @@ -24,11 +24,11 @@ import co.elastic.apm.agent.impl.context.Response; import co.elastic.apm.agent.impl.context.ServiceOrigin; import co.elastic.apm.agent.impl.context.web.ResultUtil; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; +import co.elastic.apm.agent.tracer.AbstractSpan; import com.amazonaws.services.lambda.runtime.Context; import javax.annotation.Nullable; @@ -157,7 +157,7 @@ private void setRequestHeaders(Transaction transaction, Map head @Override protected void setTransactionName(Transaction transaction, I event, Context lambdaContext) { - StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK); + StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK); if (transactionName != null) { String httpMethod = getHttpMethod(event); String requestContextPath = getRequestContextPath(event); diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java index dae3713629..c13b00d0cd 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractLambdaTransactionHelper.java @@ -27,10 +27,10 @@ import co.elastic.apm.agent.impl.metadata.FaaSMetaDataExtension; import co.elastic.apm.agent.impl.metadata.Framework; import co.elastic.apm.agent.impl.metadata.NameAndIdField; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; +import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.util.LoggerUtils; import co.elastic.apm.agent.util.VersionUtils; import com.amazonaws.services.lambda.runtime.Context; @@ -160,7 +160,7 @@ private void completeMetaData(Context lambdaContext) { } protected void setTransactionName(Transaction transaction, I event, Context lambdaContext) { - StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK); + StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK); if (transactionName != null) { transactionName.append(lambdaContext.getFunctionName()); } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java index fac1e586ca..1adae8585c 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/AbstractMessageBasedTransactionHelper.java @@ -21,8 +21,8 @@ import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.impl.context.CloudOrigin; import co.elastic.apm.agent.impl.context.ServiceOrigin; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; import co.elastic.apm.agent.impl.transaction.Transaction; +import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.util.PrivilegedActionUtils; import com.amazonaws.services.lambda.runtime.Context; @@ -108,7 +108,7 @@ protected void setTransactionTriggerData(Transaction transaction, I event) { } private void updateTransactionName(Transaction transaction, @Nullable String queueName) { - StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK); + StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK); if (null != transactionName && null != queueName && !queueName.isEmpty()) { transactionName.append("RECEIVE ").append(queueName); } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java index d44f36de88..cec3bbdf45 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/S3TransactionHelper.java @@ -19,7 +19,7 @@ package co.elastic.apm.agent.awslambda.helper; import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; +import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.impl.context.CloudOrigin; import co.elastic.apm.agent.impl.context.ServiceOrigin; @@ -91,7 +91,7 @@ protected void setTransactionTriggerData(Transaction transaction, S3Event s3Even @Override protected void setTransactionName(Transaction transaction, S3Event s3Event, Context lambdaContext) { S3EventNotification.S3EventNotificationRecord s3NotificationRecord = getS3NotificationRecord(s3Event); - StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK); + StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK); if (transactionName != null && null != s3NotificationRecord && null != s3NotificationRecord.getS3() && null != s3NotificationRecord.getS3().getBucket()) { transactionName.append(s3NotificationRecord.getEventName()).append(" ").append(s3NotificationRecord.getS3().getBucket().getName()); } else { diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SNSTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SNSTransactionHelper.java index d5862f729a..ed635f00e4 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SNSTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SNSTransactionHelper.java @@ -21,7 +21,6 @@ import co.elastic.apm.agent.awslambda.SNSMessageAttributesGetter; import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; import com.amazonaws.services.lambda.runtime.events.SNSEvent; @@ -83,11 +82,7 @@ protected void addSpanLinks(Transaction transaction, SNSEvent event) { List records = event.getRecords(); if (records != null && !records.isEmpty()) { for (SNSEvent.SNSRecord record : records) { - transaction.addSpanLink( - TraceContext.getFromTraceContextTextHeaders(), - SNSMessageAttributesGetter.INSTANCE, - record - ); + transaction.addLink(SNSMessageAttributesGetter.INSTANCE, record); } } } diff --git a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SQSTransactionHelper.java b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SQSTransactionHelper.java index 0879c568c6..218506a463 100644 --- a/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SQSTransactionHelper.java +++ b/apm-agent-plugins/apm-awslambda-plugin/src/main/java/co/elastic/apm/agent/awslambda/helper/SQSTransactionHelper.java @@ -21,7 +21,6 @@ import co.elastic.apm.agent.awslambda.SQSMessageAttributesGetter; import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; import com.amazonaws.services.lambda.runtime.events.SQSEvent; @@ -81,11 +80,7 @@ protected void addSpanLinks(Transaction transaction, SQSEvent event) { List records = event.getRecords(); if (records != null && !records.isEmpty()) { for (SQSEvent.SQSMessage record : records) { - transaction.addSpanLink( - TraceContext.getFromTraceContextTextHeaders(), - SQSMessageAttributesGetter.INSTANCE, - record - ); + transaction.addLink(SQSMessageAttributesGetter.INSTANCE, record); } } } diff --git a/apm-agent-plugins/apm-cassandra/apm-cassandra-core-plugin/src/main/java/co/elastic/apm/agent/cassandra/CassandraHelper.java b/apm-agent-plugins/apm-cassandra/apm-cassandra-core-plugin/src/main/java/co/elastic/apm/agent/cassandra/CassandraHelper.java index 0863642b1f..9fdbb9f319 100644 --- a/apm-agent-plugins/apm-cassandra/apm-cassandra-core-plugin/src/main/java/co/elastic/apm/agent/cassandra/CassandraHelper.java +++ b/apm-agent-plugins/apm-cassandra/apm-cassandra-core-plugin/src/main/java/co/elastic/apm/agent/cassandra/CassandraHelper.java @@ -52,11 +52,11 @@ public Span startCassandraSpan(@Nullable String query, boolean preparedStatem .withStatement(query) .withInstance(keyspace); - StringBuilder name = span.getAndOverrideName(co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT); + StringBuilder name = span.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (query != null && name != null) { signatureParser.querySignature(query, name, preparedStatement); } - span.withName(CASSANDRA, co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT - 1); + span.withName(CASSANDRA, AbstractSpan.PRIORITY_DEFAULT - 1); span.getContext().getServiceTarget() .withType(CASSANDRA) diff --git a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/helper/DubboTraceHelper.java b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/helper/DubboTraceHelper.java index ede10c2686..0f062f7821 100644 --- a/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/helper/DubboTraceHelper.java +++ b/apm-agent-plugins/apm-dubbo-plugin/src/main/java/co/elastic/apm/agent/dubbo/helper/DubboTraceHelper.java @@ -47,7 +47,7 @@ public static Span createConsumerSpan(Tracer tracer, Class apiClass, Strin span.withType(EXTERNAL_TYPE) .withSubtype(DUBBO_SUBTYPE); - StringBuilder spanName = span.getAndOverrideName(co.elastic.apm.agent.impl.transaction.Span.PRIO_DEFAULT); + StringBuilder spanName = span.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (spanName != null) { String className = apiClass.getName(); spanName.append(className, className.lastIndexOf('.') + 1, className.length()); @@ -66,12 +66,12 @@ public static Span createConsumerSpan(Tracer tracer, Class apiClass, Strin } public static void fillTransaction(Transaction transaction, Class apiClass, String methodName) { - StringBuilder spanName = transaction.getAndOverrideName(co.elastic.apm.agent.impl.transaction.Transaction.PRIO_DEFAULT); + StringBuilder spanName = transaction.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (spanName != null) { String className = apiClass.getName(); spanName.append(className, className.lastIndexOf('.') + 1, className.length()); spanName.append("#").append(methodName); } - transaction.withType(co.elastic.apm.agent.impl.transaction.Transaction.TYPE_REQUEST); + transaction.withType(Transaction.TYPE_REQUEST); } } diff --git a/apm-agent-plugins/apm-finagle-httpclient-plugin/src/main/java/co/elastic/apm/agent/finaglehttpclient/FinaglePayloadSizeFilterInstrumentation.java b/apm-agent-plugins/apm-finagle-httpclient-plugin/src/main/java/co/elastic/apm/agent/finaglehttpclient/FinaglePayloadSizeFilterInstrumentation.java index eacd91e0ee..13634c5633 100644 --- a/apm-agent-plugins/apm-finagle-httpclient-plugin/src/main/java/co/elastic/apm/agent/finaglehttpclient/FinaglePayloadSizeFilterInstrumentation.java +++ b/apm-agent-plugins/apm-finagle-httpclient-plugin/src/main/java/co/elastic/apm/agent/finaglehttpclient/FinaglePayloadSizeFilterInstrumentation.java @@ -24,11 +24,11 @@ import co.elastic.apm.agent.httpclient.HttpClientHelper; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import com.twitter.finagle.http.Request; import com.twitter.finagle.http.Response; import com.twitter.finagle.tracing.Trace; @@ -149,10 +149,10 @@ public static Object onBeforeExecute(@Nullable @Advice.Argument(0) Request reque } } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { // adds headers of potential parent exit-spans parent.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } diff --git a/apm-agent-plugins/apm-grails-plugin/src/main/java/co/elastic/apm/agent/grails/GrailsTransactionNameInstrumentation.java b/apm-agent-plugins/apm-grails-plugin/src/main/java/co/elastic/apm/agent/grails/GrailsTransactionNameInstrumentation.java index 4ef51d1f96..872f86d6ea 100644 --- a/apm-agent-plugins/apm-grails-plugin/src/main/java/co/elastic/apm/agent/grails/GrailsTransactionNameInstrumentation.java +++ b/apm-agent-plugins/apm-grails-plugin/src/main/java/co/elastic/apm/agent/grails/GrailsTransactionNameInstrumentation.java @@ -32,7 +32,7 @@ import java.util.Collections; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; import static grails.core.GrailsControllerClass.INDEX_ACTION; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isInterface; @@ -98,7 +98,7 @@ public static void setTransactionName(@Advice.Argument(2) Object handler) { className = handler.getClass().getSimpleName(); methodName = null; } - TransactionNameUtils.setNameFromClassAndMethod(className, methodName, transaction.getAndOverrideName(PRIO_HIGH_LEVEL_FRAMEWORK)); + TransactionNameUtils.setNameFromClassAndMethod(className, methodName, transaction.getAndOverrideName(PRIORITY_HIGH_LEVEL_FRAMEWORK)); } } } diff --git a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java index bd0a6b1114..22700e9e85 100644 --- a/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java +++ b/apm-agent-plugins/apm-grpc/apm-grpc-plugin/src/main/java/co/elastic/apm/agent/grpc/GrpcHelper.java @@ -23,12 +23,12 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.dispatch.AbstractHeaderGetter; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import io.grpc.CallOptions; @@ -88,6 +88,8 @@ public static GrpcHelper getInstance() { private final TextHeaderSetter headerSetter; private final TextHeaderGetter headerGetter; + private final Tracer tracer; + public GrpcHelper() { clientCallSpans = WeakConcurrentProviderImpl.createWeakSpanMap(); delayedClientCallSpans = WeakConcurrentProviderImpl.createWeakSpanMap(); @@ -100,6 +102,8 @@ public GrpcHelper() { headerSetter = new GrpcHeaderSetter(); headerGetter = new GrpcHeaderGetter(); + + tracer = GlobalTracer.get(); } // transaction management (server part) @@ -447,7 +451,7 @@ public Span clientCallStartEnter(ClientCall clientCall, clientCallListenerSpans.put(listener, span); - if (!TraceContext.containsTraceContextTextHeaders(headers, headerGetter)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), headers, headerGetter)) { span.propagateTraceContext(headers, headerSetter); } diff --git a/apm-agent-plugins/apm-jakarta-websocket-plugin/src/main/java/co/elastic/apm/agent/websocket/BaseServerEndpointInstrumentation.java b/apm-agent-plugins/apm-jakarta-websocket-plugin/src/main/java/co/elastic/apm/agent/websocket/BaseServerEndpointInstrumentation.java index f44ea39f21..bea7ca3c0c 100644 --- a/apm-agent-plugins/apm-jakarta-websocket-plugin/src/main/java/co/elastic/apm/agent/websocket/BaseServerEndpointInstrumentation.java +++ b/apm-agent-plugins/apm-jakarta-websocket-plugin/src/main/java/co/elastic/apm/agent/websocket/BaseServerEndpointInstrumentation.java @@ -36,7 +36,7 @@ import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isInAnyPackage; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isProxy; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; import static net.bytebuddy.matcher.ElementMatchers.isBootstrapClassLoader; import static net.bytebuddy.matcher.ElementMatchers.isInterface; @@ -113,8 +113,8 @@ protected static void endTransaction(@Nullable Object transactionOrNull, @Advice } private static void setTransactionTypeAndName(Transaction transaction, String signature, String frameworkName, @Nullable String frameworkVersion) { - transaction.withType(co.elastic.apm.agent.impl.transaction.Transaction.TYPE_REQUEST); - transaction.withName(signature, PRIO_HIGH_LEVEL_FRAMEWORK, false); + transaction.withType(Transaction.TYPE_REQUEST); + transaction.withName(signature, PRIORITY_HIGH_LEVEL_FRAMEWORK, false); transaction.setFrameworkName(frameworkName); transaction.setFrameworkVersion(frameworkVersion); } diff --git a/apm-agent-plugins/apm-javalin-plugin/src/main/java/co/elastic/apm/agent/javalin/JavalinInstrumentation.java b/apm-agent-plugins/apm-javalin-plugin/src/main/java/co/elastic/apm/agent/javalin/JavalinInstrumentation.java index 651fe35211..f4694d3b10 100644 --- a/apm-agent-plugins/apm-javalin-plugin/src/main/java/co/elastic/apm/agent/javalin/JavalinInstrumentation.java +++ b/apm-agent-plugins/apm-javalin-plugin/src/main/java/co/elastic/apm/agent/javalin/JavalinInstrumentation.java @@ -45,7 +45,7 @@ import java.util.concurrent.CompletableFuture; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isInterface; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -151,7 +151,7 @@ public static Object setSpanAndTransactionName(@Advice.This Handler handler, // transaction name gets only set if we are dealing with a HTTP method processing, not before/after handlers if (handlerType.isHttpMethod()) { - final StringBuilder name = transaction.getAndOverrideName(PRIO_HIGH_LEVEL_FRAMEWORK, false); + final StringBuilder name = transaction.getAndOverrideName(PRIORITY_HIGH_LEVEL_FRAMEWORK, false); if (name != null) { transaction.setFrameworkName(FRAMEWORK_NAME); transaction.setFrameworkVersion(VersionUtils.getVersion(Handler.class, "io.javalin", "javalin")); diff --git a/apm-agent-plugins/apm-jaxrs-plugin/src/main/java/co/elastic/apm/agent/jaxrs/JaxRsTransactionNameInstrumentation.java b/apm-agent-plugins/apm-jaxrs-plugin/src/main/java/co/elastic/apm/agent/jaxrs/JaxRsTransactionNameInstrumentation.java index f9f53cd3e1..4f32f46ec0 100644 --- a/apm-agent-plugins/apm-jaxrs-plugin/src/main/java/co/elastic/apm/agent/jaxrs/JaxRsTransactionNameInstrumentation.java +++ b/apm-agent-plugins/apm-jaxrs-plugin/src/main/java/co/elastic/apm/agent/jaxrs/JaxRsTransactionNameInstrumentation.java @@ -38,7 +38,7 @@ import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isInAnyPackage; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isProxy; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.overridesOrImplementsMethodThat; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; import static net.bytebuddy.matcher.ElementMatchers.isBootstrapClassLoader; @@ -133,7 +133,7 @@ protected static void setTransactionName(String signature, @Nullable String path transactionName = pathAnnotationValue; } } - transaction.withName(transactionName, PRIO_HIGH_LEVEL_FRAMEWORK, false); + transaction.withName(transactionName, PRIORITY_HIGH_LEVEL_FRAMEWORK, false); transaction.setFrameworkName("JAX-RS"); transaction.setFrameworkVersion(frameworkVersion); } diff --git a/apm-agent-plugins/apm-jaxws-plugin/src/main/java/co/elastic/apm/agent/jaxws/JaxWsTransactionNameInstrumentation.java b/apm-agent-plugins/apm-jaxws-plugin/src/main/java/co/elastic/apm/agent/jaxws/JaxWsTransactionNameInstrumentation.java index a1ff1cee3f..45a81d54e7 100644 --- a/apm-agent-plugins/apm-jaxws-plugin/src/main/java/co/elastic/apm/agent/jaxws/JaxWsTransactionNameInstrumentation.java +++ b/apm-agent-plugins/apm-jaxws-plugin/src/main/java/co/elastic/apm/agent/jaxws/JaxWsTransactionNameInstrumentation.java @@ -37,7 +37,7 @@ import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isInAnyPackage; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isProxy; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.overridesOrImplementsMethodThat; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; import static net.bytebuddy.matcher.ElementMatchers.isBootstrapClassLoader; import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy; @@ -61,7 +61,7 @@ public static class AdviceClass { public static void setTransactionName(@SimpleMethodSignature String signature) { final Transaction transaction = tracer.currentTransaction(); if (transaction != null) { - transaction.withName(signature, PRIO_HIGH_LEVEL_FRAMEWORK, false); + transaction.withName(signature, PRIORITY_HIGH_LEVEL_FRAMEWORK, false); transaction.setFrameworkName(FRAMEWORK_NAME); } } diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java index fc57ff688e..424485f68c 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java @@ -96,7 +96,7 @@ public Span createJdbcSpan(@Nullable String sql, Object statement, @Nullable if (sql.isEmpty()) { span.withName("empty query"); } else if (span.isSampled()) { - StringBuilder spanName = span.getAndOverrideName(co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT); + StringBuilder spanName = span.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (spanName != null) { signatureParser.querySignature(sql, spanName, preparedStatement); } diff --git a/apm-agent-plugins/apm-jdk-httpserver-plugin/src/main/java/co/elastic/apm/agent/httpserver/HttpHandlerAdvice.java b/apm-agent-plugins/apm-jdk-httpserver-plugin/src/main/java/co/elastic/apm/agent/httpserver/HttpHandlerAdvice.java index 7e2fa48a9b..9bbd6d9e7a 100644 --- a/apm-agent-plugins/apm-jdk-httpserver-plugin/src/main/java/co/elastic/apm/agent/httpserver/HttpHandlerAdvice.java +++ b/apm-agent-plugins/apm-jdk-httpserver-plugin/src/main/java/co/elastic/apm/agent/httpserver/HttpHandlerAdvice.java @@ -19,7 +19,7 @@ package co.elastic.apm.agent.httpserver; import co.elastic.apm.agent.configuration.CoreConfiguration; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; +import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.impl.context.web.ResultUtil; import co.elastic.apm.agent.impl.context.web.WebConfiguration; @@ -69,10 +69,10 @@ public static Object onEnterHandle(@Advice.Argument(0) HttpExchange exchange) { TransactionNameUtils.setNameFromHttpRequestPath( exchange.getRequestMethod(), exchange.getRequestURI().getPath(), - transaction.getAndOverrideName(AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK), + transaction.getAndOverrideName(AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK), webConfiguration.getUrlGroups()); - transaction.withType(co.elastic.apm.agent.impl.transaction.Transaction.TYPE_REQUEST) + transaction.withType(Transaction.TYPE_REQUEST) .setFrameworkName("JDK HTTP Server"); Request request = transaction.getContext().getRequest(); diff --git a/apm-agent-plugins/apm-jdk-httpserver-plugin/src/test/java/co/elastic/apm/agent/httpserver/HttpHandlerTest.java b/apm-agent-plugins/apm-jdk-httpserver-plugin/src/test/java/co/elastic/apm/agent/httpserver/HttpHandlerTest.java index 3decd217bc..4911607273 100644 --- a/apm-agent-plugins/apm-jdk-httpserver-plugin/src/test/java/co/elastic/apm/agent/httpserver/HttpHandlerTest.java +++ b/apm-agent-plugins/apm-jdk-httpserver-plugin/src/test/java/co/elastic/apm/agent/httpserver/HttpHandlerTest.java @@ -107,7 +107,7 @@ void testRootTransaction(int expectedStatus) throws IOException { Transaction transaction = reporter.getFirstTransaction(500); assertThat(transaction.getTraceContext().getParentId().toString()).isEqualTo("0000000000000000"); - assertThat(transaction.getType()).isEqualTo(Transaction.TYPE_REQUEST); + assertThat(transaction.getType()).isEqualTo(co.elastic.apm.agent.tracer.Transaction.TYPE_REQUEST); assertThat(transaction.getNameAsString()).isEqualTo("GET /status_%d", expectedStatus); assertThat(transaction.getResult()).isEqualTo(ResultUtil.getResultByHttpStatus(expectedStatus)); assertThat(transaction.getOutcome()).isEqualTo(ResultUtil.getOutcomeByHttpServerStatus(expectedStatus)); diff --git a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/BaseJmsInstrumentation.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/BaseJmsInstrumentation.java index 053b6780f8..62e5aa45c3 100644 --- a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/BaseJmsInstrumentation.java +++ b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/BaseJmsInstrumentation.java @@ -56,7 +56,7 @@ protected static class BaseAdvice { // loading helper class will load JMS-related classes if loaded from Instrumentation static init // that fails when trying to load instrumentation classes without JMS dependencies, for example when generating // documentation that relies on instrumentation group names - helper = new JmsInstrumentationHelper(tracer); + helper = JmsInstrumentationHelper.get(); messagingConfiguration = tracer.getConfig(MessagingConfiguration.class); coreConfiguration = tracer.getConfig(CoreConfiguration.class); diff --git a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsInstrumentationHelper.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsInstrumentationHelper.java index ac3721d867..3d5bb6cddd 100644 --- a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsInstrumentationHelper.java +++ b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsInstrumentationHelper.java @@ -21,8 +21,8 @@ import co.elastic.apm.agent.configuration.CoreConfiguration; import co.elastic.apm.agent.configuration.MessagingConfiguration; import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -41,14 +41,13 @@ import javax.jms.TextMessage; import javax.jms.Topic; import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; public class JmsInstrumentationHelper { - /** - * In some cases, dashes are not allowed in JMS Message property names - */ - protected static String JMS_TRACE_PARENT_PROPERTY = TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME.replace('-', '_'); - /** * When the agent computes a destination name instead of using the default queue name- it should be passed as a * message property, in case the receiver side cannot apply the same computation. For example, temporary queues are @@ -84,14 +83,36 @@ public class JmsInstrumentationHelper { static final String FRAMEWORK_NAME = "JMS"; private static final Logger logger = LoggerFactory.getLogger(JmsInstrumentationHelper.class); + + private static final JmsInstrumentationHelper INSTANCE = new JmsInstrumentationHelper(GlobalTracer.get()); + private final Tracer tracer; private final CoreConfiguration coreConfiguration; private final MessagingConfiguration messagingConfiguration; + private final Set jmsTraceHeaders = new HashSet<>(); + private final Map translatedTraceHeaders = new HashMap<>(); - public JmsInstrumentationHelper(Tracer tracer) { + public static JmsInstrumentationHelper get() { + return INSTANCE; + } + + private JmsInstrumentationHelper(Tracer tracer) { this.tracer = tracer; coreConfiguration = tracer.getConfig(CoreConfiguration.class); messagingConfiguration = tracer.getConfig(MessagingConfiguration.class); + Set traceHeaders = tracer.getTraceHeaderNames(); + for (String traceHeader : traceHeaders) { + String jmsTraceHeader = traceHeader.replace('-', '_'); + if (!jmsTraceHeaders.add(jmsTraceHeader)) { + throw new IllegalStateException("Ambiguous translation of trace headers into JMS-compatible format: " + traceHeaders); + } + translatedTraceHeaders.put(traceHeader, jmsTraceHeader); + } + } + + public String resolvePossibleTraceHeader(String header) { + String translation = translatedTraceHeaders.get(header); + return translation == null ? header : translation; } @SuppressWarnings("Duplicates") @@ -258,8 +279,9 @@ public void addMessageDetails(@Nullable Message message, AbstractSpan span) { if (properties != null) { while (properties.hasMoreElements()) { String propertyName = String.valueOf(properties.nextElement()); - if (!propertyName.equals(JMS_DESTINATION_NAME_PROPERTY) && !propertyName.equals(JMS_TRACE_PARENT_PROPERTY) - && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), propertyName) == null) { + if (!propertyName.equals(JMS_DESTINATION_NAME_PROPERTY) && + !jmsTraceHeaders.contains(propertyName) && + WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), propertyName) == null) { messageContext.addHeader(propertyName, String.valueOf(message.getObjectProperty(propertyName))); } } diff --git a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessageConsumerInstrumentation.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessageConsumerInstrumentation.java index 18192a738e..e118f8e84b 100644 --- a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessageConsumerInstrumentation.java +++ b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessageConsumerInstrumentation.java @@ -21,7 +21,6 @@ import co.elastic.apm.agent.configuration.MessagingConfiguration; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.util.PrivilegedActionUtils; import net.bytebuddy.asm.Advice; diff --git a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessor.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessor.java index 292a4d7756..a70ad093e8 100644 --- a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessor.java +++ b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessor.java @@ -18,21 +18,17 @@ */ package co.elastic.apm.agent.jms; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; import co.elastic.apm.agent.tracer.dispatch.AbstractHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; -import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageNotWriteableException; -import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.JMS_TRACE_PARENT_PROPERTY; - public class JmsMessagePropertyAccessor extends AbstractHeaderGetter implements TextHeaderGetter, TextHeaderSetter { private static final Logger logger = LoggerFactory.getLogger(JmsMessagePropertyAccessor.class); @@ -43,13 +39,16 @@ public static JmsMessagePropertyAccessor instance() { return INSTANCE; } + private final JmsInstrumentationHelper helper; + private JmsMessagePropertyAccessor() { + helper = JmsInstrumentationHelper.get(); } @Nullable @Override public String getFirstHeader(String headerName, Message message) { - headerName = jmsifyHeaderName(headerName); + headerName = helper.resolvePossibleTraceHeader(headerName); String value = null; try { value = message.getStringProperty(headerName); @@ -59,18 +58,9 @@ public String getFirstHeader(String headerName, Message message) { return value; } - @Nonnull - private String jmsifyHeaderName(String headerName) { - if (headerName.equals(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)) { - // replacing with the JMS equivalent - headerName = JMS_TRACE_PARENT_PROPERTY; - } - return headerName; - } - @Override public void setHeader(String headerName, String headerValue, Message message) { - headerName = jmsifyHeaderName(headerName); + headerName = helper.resolvePossibleTraceHeader(headerName); if (getFirstHeader(headerName, message) != null) { return; } diff --git a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsInstrumentationIT.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsInstrumentationIT.java index a2acd815f3..f322f06864 100644 --- a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsInstrumentationIT.java +++ b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsInstrumentationIT.java @@ -67,7 +67,6 @@ import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.JMS_EXPIRATION_HEADER; import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.JMS_MESSAGE_ID_HEADER; import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.JMS_TIMESTAMP_HEADER; -import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.JMS_TRACE_PARENT_PROPERTY; import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.MESSAGING_TYPE; import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.TEMP; import static co.elastic.apm.agent.jms.JmsInstrumentationHelper.TIBCO_TMP_QUEUE_PREFIX; @@ -78,6 +77,8 @@ @RunWith(Parameterized.class) public class JmsInstrumentationIT extends AbstractInstrumentationTest { + private static final String JMS_TRACE_PARENT_PROPERTY = TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME.replace('-', '_'); + // Keeping a static reference for resource cleaning private final static Set staticBrokerFacade = new HashSet<>(); diff --git a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessorTest.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessorTest.java index cdca20f4ca..b174492e96 100644 --- a/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessorTest.java +++ b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/test/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessorTest.java @@ -18,6 +18,7 @@ */ package co.elastic.apm.agent.jms; +import co.elastic.apm.agent.AbstractInstrumentationTest; import co.elastic.apm.agent.impl.transaction.TraceContext; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -35,7 +36,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -class JmsMessagePropertyAccessorTest { +class JmsMessagePropertyAccessorTest extends AbstractInstrumentationTest { @Test void returnsNullOnJMSException() throws JMSException { diff --git a/apm-agent-plugins/apm-jsf-plugin/src/main/java/co/elastic/apm/agent/jsf/AbstractJsfLifecycleExecuteInstrumentation.java b/apm-agent-plugins/apm-jsf-plugin/src/main/java/co/elastic/apm/agent/jsf/AbstractJsfLifecycleExecuteInstrumentation.java index 78ae54253b..f7e27e7376 100644 --- a/apm-agent-plugins/apm-jsf-plugin/src/main/java/co/elastic/apm/agent/jsf/AbstractJsfLifecycleExecuteInstrumentation.java +++ b/apm-agent-plugins/apm-jsf-plugin/src/main/java/co/elastic/apm/agent/jsf/AbstractJsfLifecycleExecuteInstrumentation.java @@ -26,7 +26,7 @@ import javax.annotation.Nullable; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; @@ -60,9 +60,9 @@ protected static Object createAndActivateSpan(boolean withExternalContext, @Null if (transaction != null) { try { if (withExternalContext) { - transaction.withName(requestServletPath, PRIO_HIGH_LEVEL_FRAMEWORK); + transaction.withName(requestServletPath, PRIORITY_HIGH_LEVEL_FRAMEWORK); if (requestPathInfo != null) { - transaction.appendToName(requestPathInfo, PRIO_HIGH_LEVEL_FRAMEWORK); + transaction.appendToName(requestPathInfo, PRIORITY_HIGH_LEVEL_FRAMEWORK); } } transaction.setFrameworkName(FRAMEWORK_NAME); diff --git a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-base-plugin/src/main/java/co/elastic/apm/agent/kafka/KafkaConsumerInstrumentation.java b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-base-plugin/src/main/java/co/elastic/apm/agent/kafka/KafkaConsumerInstrumentation.java index 74523809d5..93f62638f0 100644 --- a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-base-plugin/src/main/java/co/elastic/apm/agent/kafka/KafkaConsumerInstrumentation.java +++ b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-base-plugin/src/main/java/co/elastic/apm/agent/kafka/KafkaConsumerInstrumentation.java @@ -84,7 +84,7 @@ public static void pollStart() { span.withType("messaging") .withSubtype("kafka") .withAction("poll") - .withName("KafkaConsumer#poll", co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK); + .withName("KafkaConsumer#poll", AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK); span.getContext().getServiceTarget().withType("kafka"); diff --git a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIterableWrapper.java b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIterableWrapper.java index aa5521676b..4666d79f64 100644 --- a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIterableWrapper.java +++ b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIterableWrapper.java @@ -22,19 +22,22 @@ import org.apache.kafka.clients.consumer.ConsumerRecord; import java.util.Iterator; +import java.util.Set; class ConsumerRecordsIterableWrapper implements Iterable> { private final Iterable> delegate; private final Tracer tracer; + private final Set binaryTraceHeaders; - public ConsumerRecordsIterableWrapper(Iterable> delegate, Tracer tracer) { + public ConsumerRecordsIterableWrapper(Iterable> delegate, Tracer tracer, Set binaryTraceHeaders) { this.delegate = delegate; this.tracer = tracer; + this.binaryTraceHeaders = binaryTraceHeaders; } @Override public Iterator> iterator() { - return new ConsumerRecordsIteratorWrapper(delegate.iterator(), tracer); + return new ConsumerRecordsIteratorWrapper(delegate.iterator(), tracer, binaryTraceHeaders); } } diff --git a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIteratorWrapper.java b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIteratorWrapper.java index 6dc8cea9ee..6231faa21e 100644 --- a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIteratorWrapper.java +++ b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsIteratorWrapper.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.configuration.CoreConfiguration; import co.elastic.apm.agent.configuration.MessagingConfiguration; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -33,6 +32,7 @@ import co.elastic.apm.agent.sdk.logging.LoggerFactory; import java.util.Iterator; +import java.util.Set; class ConsumerRecordsIteratorWrapper implements Iterator> { @@ -41,12 +41,14 @@ class ConsumerRecordsIteratorWrapper implements Iterator> { private final Iterator> delegate; private final Tracer tracer; + private final Set binaryTraceHeaders; private final CoreConfiguration coreConfiguration; private final MessagingConfiguration messagingConfiguration; - public ConsumerRecordsIteratorWrapper(Iterator> delegate, Tracer tracer) { + public ConsumerRecordsIteratorWrapper(Iterator> delegate, Tracer tracer, Set binaryTraceHeaders) { this.delegate = delegate; this.tracer = tracer; + this.binaryTraceHeaders = binaryTraceHeaders; coreConfiguration = tracer.getConfig(CoreConfiguration.class); messagingConfiguration = tracer.getConfig(MessagingConfiguration.class); } @@ -89,7 +91,7 @@ public void endCurrentTransaction() { if (transaction.isSampled() && coreConfiguration.isCaptureHeaders()) { for (Header header : record.headers()) { String key = header.key(); - if (!TraceContext.TRACE_PARENT_BINARY_HEADER_NAME.equals(key) && + if (!binaryTraceHeaders.contains(key) && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) { message.addHeader(key, header.value()); } diff --git a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsListWrapper.java b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsListWrapper.java index 357de0f3da..9723e40c4d 100644 --- a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsListWrapper.java +++ b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/ConsumerRecordsListWrapper.java @@ -25,15 +25,18 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; +import java.util.Set; class ConsumerRecordsListWrapper implements List> { private final List> delegate; private final Tracer tracer; + private final Set binaryTraceHeaders; - public ConsumerRecordsListWrapper(List> delegate, Tracer tracer) { + public ConsumerRecordsListWrapper(List> delegate, Tracer tracer, Set binaryTraceHeaders) { this.delegate = delegate; this.tracer = tracer; + this.binaryTraceHeaders = binaryTraceHeaders; } @Override @@ -53,7 +56,7 @@ public boolean contains(Object o) { @Override public Iterator> iterator() { - return new ConsumerRecordsIteratorWrapper(delegate.iterator(), tracer); + return new ConsumerRecordsIteratorWrapper(delegate.iterator(), tracer, binaryTraceHeaders); } @Override @@ -158,6 +161,6 @@ public int lastIndexOf(Object o) { @Override public List> subList(int fromIndex, int toIndex) { - return new ConsumerRecordsListWrapper(delegate.subList(fromIndex, toIndex), tracer); + return new ConsumerRecordsListWrapper(delegate.subList(fromIndex, toIndex), tracer, binaryTraceHeaders); } } diff --git a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaInstrumentationHeadersHelper.java b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaInstrumentationHeadersHelper.java index 256b4681ce..6320a1cdeb 100644 --- a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaInstrumentationHeadersHelper.java +++ b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaInstrumentationHeadersHelper.java @@ -21,8 +21,8 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.tracer.Tracer; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.producer.ProducerRecord; @@ -30,8 +30,13 @@ import co.elastic.apm.agent.sdk.logging.LoggerFactory; import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; public class KafkaInstrumentationHeadersHelper { @@ -46,6 +51,8 @@ protected Boolean initialValue() { }; private final Tracer tracer; + private final Set binaryTraceHeaders = new HashSet<>(); + private final Map translatedTraceHeaders = new HashMap<>(); public static KafkaInstrumentationHeadersHelper get() { return INSTANCE; @@ -53,11 +60,25 @@ public static KafkaInstrumentationHeadersHelper get() { public KafkaInstrumentationHeadersHelper(Tracer tracer) { this.tracer = tracer; + Pattern pattern = Pattern.compile("[^a-zA-Z0-9]"); + Set traceHeaders = tracer.getTraceHeaderNames(); + for (String traceHeader : traceHeaders) { + String binaryTraceHeader = pattern.matcher(traceHeader).replaceAll(""); + if (!binaryTraceHeaders.add(binaryTraceHeader)) { + throw new IllegalStateException("Ambiguous translation of trace headers into binary format: " + traceHeaders); + } + translatedTraceHeaders.put(traceHeader, binaryTraceHeader); + } + } + + public String resolvePossibleTraceHeader(String header) { + String translation = translatedTraceHeaders.get(header); + return translation == null ? header : translation; } public Iterator> wrapConsumerRecordIterator(Iterator> consumerRecordIterator) { try { - return new ConsumerRecordsIteratorWrapper(consumerRecordIterator, tracer); + return new ConsumerRecordsIteratorWrapper(consumerRecordIterator, tracer, binaryTraceHeaders); } catch (Throwable throwable) { logger.debug("Failed to wrap Kafka ConsumerRecords iterator", throwable); return consumerRecordIterator; @@ -66,7 +87,7 @@ public KafkaInstrumentationHeadersHelper(Tracer tracer) { public Iterable> wrapConsumerRecordIterable(Iterable> consumerRecordIterable) { try { - return new ConsumerRecordsIterableWrapper(consumerRecordIterable, tracer); + return new ConsumerRecordsIterableWrapper(consumerRecordIterable, tracer, binaryTraceHeaders); } catch (Throwable throwable) { logger.debug("Failed to wrap Kafka ConsumerRecords", throwable); return consumerRecordIterable; @@ -75,7 +96,7 @@ public KafkaInstrumentationHeadersHelper(Tracer tracer) { public List> wrapConsumerRecordList(List> consumerRecordList) { try { - return new ConsumerRecordsListWrapper(consumerRecordList, tracer); + return new ConsumerRecordsListWrapper(consumerRecordList, tracer, binaryTraceHeaders); } catch (Throwable throwable) { logger.debug("Failed to wrap Kafka ConsumerRecords list", throwable); return consumerRecordList; @@ -121,6 +142,6 @@ public void setOutgoingTraceContextHeaders(Span span, ProducerRecord pr } public void removeTraceContextHeader(ProducerRecord producerRecord) { - TraceContext.removeTraceContextHeaders(producerRecord, KafkaRecordHeaderAccessor.instance()); + HeaderUtils.remove(binaryTraceHeaders, producerRecord, KafkaRecordHeaderAccessor.instance()); } } diff --git a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaRecordHeaderAccessor.java b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaRecordHeaderAccessor.java index 450512df57..4ea45c0fc2 100644 --- a/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaRecordHeaderAccessor.java +++ b/apm-agent-plugins/apm-kafka-plugin/apm-kafka-headers-plugin/src/main/java/co/elastic/apm/agent/kafka/helper/KafkaRecordHeaderAccessor.java @@ -45,9 +45,12 @@ public static KafkaRecordHeaderAccessor instance() { return INSTANCE; } + private final KafkaInstrumentationHeadersHelper helper = KafkaInstrumentationHeadersHelper.get(); + @Nullable @Override public byte[] getFirstHeader(String headerName, ConsumerRecord record) { + headerName = helper.resolvePossibleTraceHeader(headerName); Header traceParentHeader = record.headers().lastHeader(headerName); if (traceParentHeader != null) { return traceParentHeader.value(); @@ -57,6 +60,7 @@ public byte[] getFirstHeader(String headerName, ConsumerRecord record) { @Override public void forEach(String headerName, ConsumerRecord carrier, S state, HeaderConsumer consumer) { + headerName = helper.resolvePossibleTraceHeader(headerName); for (Header header : carrier.headers().headers(headerName)) { consumer.accept(header.value(), state); } @@ -65,6 +69,7 @@ public void forEach(String headerName, ConsumerRecord carrier, S state, Head @Override @Nullable public byte[] getFixedLengthByteArray(String headerName, int length) { + headerName = helper.resolvePossibleTraceHeader(headerName); Map headerMap = threadLocalHeaderMap.get(); if (headerMap == null) { headerMap = new HashMap<>(); @@ -80,6 +85,7 @@ public byte[] getFixedLengthByteArray(String headerName, int length) { @Override public void setHeader(String headerName, byte[] headerValue, ProducerRecord record) { + headerName = helper.resolvePossibleTraceHeader(headerName); ElasticHeaderImpl header = null; Map headerMap = threadLocalHeaderMap.get(); if (headerMap != null) { @@ -96,6 +102,7 @@ public void setHeader(String headerName, byte[] headerValue, ProducerRecord reco @Override public void remove(String headerName, ProducerRecord carrier) { + headerName = helper.resolvePossibleTraceHeader(headerName); carrier.headers().remove(headerName); } diff --git a/apm-agent-plugins/apm-mongodb/apm-mongodb-common/src/main/java/co/elastic/apm/agent/mongodb/MongoHelper.java b/apm-agent-plugins/apm-mongodb/apm-mongodb-common/src/main/java/co/elastic/apm/agent/mongodb/MongoHelper.java index a8d011f08e..89d70fb2f1 100644 --- a/apm-agent-plugins/apm-mongodb/apm-mongodb-common/src/main/java/co/elastic/apm/agent/mongodb/MongoHelper.java +++ b/apm-agent-plugins/apm-mongodb/apm-mongodb-common/src/main/java/co/elastic/apm/agent/mongodb/MongoHelper.java @@ -71,7 +71,7 @@ public Span startSpan(@Nullable String database, @Nullable String collection, .withInstance(database) .withStatement(statement); - StringBuilder name = span.getAndOverrideName(co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT); + StringBuilder name = span.getAndOverrideName(AbstractSpan.PRIORITY_DEFAULT); if (name != null) { appendToName(name, database); appendToName(name, collection); diff --git a/apm-agent-plugins/apm-okhttp-plugin/src/main/java/co/elastic/apm/agent/okhttp/OkHttp3ClientAsyncInstrumentation.java b/apm-agent-plugins/apm-okhttp-plugin/src/main/java/co/elastic/apm/agent/okhttp/OkHttp3ClientAsyncInstrumentation.java index f47a94b52d..82e351fef3 100644 --- a/apm-agent-plugins/apm-okhttp-plugin/src/main/java/co/elastic/apm/agent/okhttp/OkHttp3ClientAsyncInstrumentation.java +++ b/apm-agent-plugins/apm-okhttp-plugin/src/main/java/co/elastic/apm/agent/okhttp/OkHttp3ClientAsyncInstrumentation.java @@ -22,7 +22,7 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; @@ -87,7 +87,7 @@ public static Object[] onBeforeEnqueue(final @Advice.Origin Class abstractSpan, long finishMic Transaction transaction = (Transaction) abstractSpan; if (transaction.getType() == null) { if (transaction.getContext().getRequest().hasContent()) { - transaction.withType(Transaction.TYPE_REQUEST); + transaction.withType(co.elastic.apm.agent.tracer.Transaction.TYPE_REQUEST); } } } @@ -100,7 +100,7 @@ public static class AdviceClass { public static void setOperationName(@Advice.FieldValue(value = "dispatcher", typing = Assigner.Typing.DYNAMIC) @Nullable Object context, @Advice.Argument(0) @Nullable String operationName) { if (context instanceof AbstractSpan) { - ((AbstractSpan) context).withName(operationName, PRIO_USER_SUPPLIED); + ((AbstractSpan) context).withName(operationName, PRIORITY_USER_SUPPLIED); } else { logger.warn("Calling setOperationName on an already finished span"); } @@ -202,15 +202,15 @@ private static boolean handleSpecialTransactionTag(Transaction transaction, Stri transaction.getContext().getResponse().withStatusCode(status); transaction.withResultIfUnset(ResultUtil.getResultByHttpStatus(status)); transaction.withOutcome(ResultUtil.getOutcomeByHttpServerStatus(status)); - transaction.withType(Transaction.TYPE_REQUEST); + transaction.withType(co.elastic.apm.agent.tracer.Transaction.TYPE_REQUEST); return true; } else if ("http.method".equals(key)) { transaction.getContext().getRequest().withMethod(value.toString()); - transaction.withType(Transaction.TYPE_REQUEST); + transaction.withType(co.elastic.apm.agent.tracer.Transaction.TYPE_REQUEST); return true; } else if ("http.url".equals(key)) { transaction.getContext().getRequest().getUrl().withFull(value.toString()); - transaction.withType(Transaction.TYPE_REQUEST); + transaction.withType(co.elastic.apm.agent.tracer.Transaction.TYPE_REQUEST); return true; } else if ("sampling.priority".equals(key)) { // mid-trace sampling is not allowed diff --git a/apm-agent-plugins/apm-profiling-plugin/src/main/java/co/elastic/apm/agent/profiler/CallTree.java b/apm-agent-plugins/apm-profiling-plugin/src/main/java/co/elastic/apm/agent/profiler/CallTree.java index 58fb78f584..95fe7f414a 100644 --- a/apm-agent-plugins/apm-profiling-plugin/src/main/java/co/elastic/apm/agent/profiler/CallTree.java +++ b/apm-agent-plugins/apm-profiling-plugin/src/main/java/co/elastic/apm/agent/profiler/CallTree.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.collections.LongList; import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; import co.elastic.apm.agent.impl.transaction.Span; import co.elastic.apm.agent.impl.transaction.StackFrame; import co.elastic.apm.agent.impl.transaction.TraceContext; @@ -405,7 +404,7 @@ protected Span asSpan(Root root, TraceContext parentContext) { String classFqn = frame.getClassName(); if (classFqn != null) { - span.appendToName(classFqn, AbstractSpan.PRIO_DEFAULT, frame.getSimpleClassNameOffset(), classFqn.length()); + span.appendToName(classFqn, co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_DEFAULT, frame.getSimpleClassNameOffset(), classFqn.length()); } else { span.appendToName("null"); } diff --git a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java index 468055c225..f3646443af 100644 --- a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java +++ b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java @@ -19,7 +19,6 @@ package co.elastic.apm.agent.servlet; import co.elastic.apm.agent.configuration.CoreConfiguration; -import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Outcome; @@ -41,7 +40,7 @@ import java.util.List; import java.util.Map; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK; import static co.elastic.apm.agent.servlet.ServletTransactionHelper.TRANSACTION_ATTRIBUTE; public abstract class ServletApiAdvice { @@ -189,7 +188,7 @@ public static currentTransaction = tracer.currentTransaction(); if (currentTransaction != null) { - TransactionNameUtils.setTransactionNameByServletClass(adapter.getMethod(httpServletRequest), thiz.getClass(), currentTransaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK)); + TransactionNameUtils.setTransactionNameByServletClass(adapter.getMethod(httpServletRequest), thiz.getClass(), currentTransaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK)); String userName = ServletTransactionHelper.getUserFromPrincipal(adapter.getUserPrincipal(httpServletRequest)); if (userName != null) { diff --git a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java index fe2e40010b..2daa1fe5e5 100644 --- a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java +++ b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java @@ -43,8 +43,8 @@ import java.util.Set; import static co.elastic.apm.agent.configuration.CoreConfiguration.EventType.OFF; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_DEFAULT; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK; public class ServletTransactionHelper { @@ -263,9 +263,9 @@ void applyDefaultTransactionName(String method, String servletPath, @Nullable St // JSPs don't contain path params and the name is more telling than the generated servlet class if (webConfiguration.isUsePathAsName() || ENDS_WITH_JSP.matches(servletPath, pathInfo)) { // should override ServletName#doGet - TransactionNameUtils.setNameFromHttpRequestPath(method, servletPath, pathInfo, transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK + 1), webConfiguration.getUrlGroups()); + TransactionNameUtils.setNameFromHttpRequestPath(method, servletPath, pathInfo, transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK + 1), webConfiguration.getUrlGroups()); } else { - TransactionNameUtils.setNameUnknownRoute(method, transaction.getAndOverrideName(PRIO_DEFAULT)); + TransactionNameUtils.setNameUnknownRoute(method, transaction.getAndOverrideName(PRIORITY_DEFAULT)); } } diff --git a/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ApmFilterTest.java b/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ApmFilterTest.java index 26c0e95a30..a9d69b109e 100644 --- a/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ApmFilterTest.java +++ b/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ApmFilterTest.java @@ -52,7 +52,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_USER_SUPPLIED; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_USER_SUPPLIED; import static org.assertj.core.api.Java6Assertions.assertThat; import static org.assertj.core.api.Java6Assertions.assertThatThrownBy; import static org.mockito.Mockito.doReturn; @@ -327,7 +327,7 @@ public void init(FilterConfig filterConfig) throws ServletException { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - Objects.requireNonNull(tracer.currentTransaction()).withName(customName, PRIO_USER_SUPPLIED); + Objects.requireNonNull(tracer.currentTransaction()).withName(customName, PRIORITY_USER_SUPPLIED); chain.doFilter(request, response); } diff --git a/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ServletTransactionHelperTest.java b/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ServletTransactionHelperTest.java index e89a61df90..e0c924a15a 100644 --- a/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ServletTransactionHelperTest.java +++ b/apm-agent-plugins/apm-servlet-plugin/src/test/java/co/elastic/apm/agent/servlet/ServletTransactionHelperTest.java @@ -35,7 +35,7 @@ import java.util.List; import java.util.stream.Stream; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -54,14 +54,14 @@ void setUp() { @Test void setTransactionNameByServletClass() { Transaction transaction = new Transaction(MockTracer.create()); - TransactionNameUtils.setTransactionNameByServletClass("GET", ServletTransactionHelperTest.class, transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK)); + TransactionNameUtils.setTransactionNameByServletClass("GET", ServletTransactionHelperTest.class, transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK)); assertThat(transaction.getNameAsString()).isEqualTo("ServletTransactionHelperTest#doGet"); } @Test void setTransactionNameByServletClassNullMethod() { Transaction transaction = new Transaction(MockTracer.create()); - TransactionNameUtils.setTransactionNameByServletClass(null, ServletTransactionHelperTest.class, transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK)); + TransactionNameUtils.setTransactionNameByServletClass(null, ServletTransactionHelperTest.class, transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK)); assertThat(transaction.getNameAsString()).isEqualTo("ServletTransactionHelperTest"); } @@ -87,7 +87,7 @@ void testGroupUrlsOverridesServletName() { )).when(webConfig).getUrlGroups(); Transaction transaction = new Transaction(MockTracer.create()); - TransactionNameUtils.setTransactionNameByServletClass("GET", ServletTransactionHelperTest.class, transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK)); + TransactionNameUtils.setTransactionNameByServletClass("GET", ServletTransactionHelperTest.class, transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK)); servletTransactionHelper.applyDefaultTransactionName("GET", "/foo/bar/baz", null, transaction); assertThat(transaction.getNameAsString()).isEqualTo("GET /foo/bar/*"); } diff --git a/apm-agent-plugins/apm-sparkjava-plugin/src/main/java/co/elastic/apm/agent/sparkjava/RoutesAdvice.java b/apm-agent-plugins/apm-sparkjava-plugin/src/main/java/co/elastic/apm/agent/sparkjava/RoutesAdvice.java index 0725430bb7..4870c88c9a 100644 --- a/apm-agent-plugins/apm-sparkjava-plugin/src/main/java/co/elastic/apm/agent/sparkjava/RoutesAdvice.java +++ b/apm-agent-plugins/apm-sparkjava-plugin/src/main/java/co/elastic/apm/agent/sparkjava/RoutesAdvice.java @@ -28,7 +28,7 @@ import spark.Route; import spark.routematch.RouteMatch; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK; public class RoutesAdvice { @@ -44,7 +44,7 @@ public static void onExitFind(@Advice.Return RouteMatch routeMatch) { String method = routeMatch.getHttpMethod().name().toUpperCase(); TransactionNameUtils.setNameFromHttpRequestPath(method, routeMatch.getMatchUri(), - transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK + 1), + transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK + 1), tracer.getConfig(WebConfiguration.class).getUrlGroups()); transaction.setFrameworkName("Spark"); diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/HandlerAdapterInstrumentation.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/HandlerAdapterInstrumentation.java index d5e03e2c00..b662d2c5d8 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/HandlerAdapterInstrumentation.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/HandlerAdapterInstrumentation.java @@ -18,7 +18,7 @@ */ package co.elastic.apm.agent.springwebflux; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; +import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.util.TransactionNameUtils; import net.bytebuddy.asm.Advice; @@ -82,7 +82,7 @@ public static Object onEnter(@Advice.Argument(0) ServerWebExchange exchange, TransactionNameUtils.setNameFromClassAndMethod( handlerMethod.getBeanType().getSimpleName(), handlerMethod.getMethod().getName(), - ((Transaction) exchangeTransaction).getAndOverrideName(AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK, false)); + ((Transaction) exchangeTransaction).getAndOverrideName(AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK, false)); } diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java index e91665d144..e802ae382f 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java @@ -57,8 +57,8 @@ import java.util.Map; import java.util.function.BiFunction; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK; import static org.springframework.web.reactive.function.server.RouterFunctions.MATCHING_PATTERN_ATTRIBUTE; public class WebfluxHelper { @@ -189,10 +189,10 @@ public static void setTransactionName(@Nullable Transaction transaction, Serv String path = null; PathPattern pattern = exchange.getAttribute(MATCHING_PATTERN_ATTRIBUTE); if (pattern != null) { - namePriority = PRIO_HIGH_LEVEL_FRAMEWORK; + namePriority = PRIORITY_HIGH_LEVEL_FRAMEWORK; path = pattern.getPatternString(); } else { - namePriority = PRIO_LOW_LEVEL_FRAMEWORK + 1; + namePriority = PRIORITY_LOW_LEVEL_FRAMEWORK + 1; if (webConfig.isUsePathAsName()) { path = exchange.getRequest().getPath().value(); } diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingAnnotated.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingAnnotated.java index 180bef789d..378358409d 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingAnnotated.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingAnnotated.java @@ -19,7 +19,8 @@ package co.elastic.apm.agent.springwebflux.testapp; import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; +import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.impl.transaction.Id; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.sdk.logging.Logger; @@ -178,7 +179,7 @@ public Mono customTransactionName() { ElasticApmTracer tracer = GlobalTracer.get().require(ElasticApmTracer.class); Transaction transaction = Objects.requireNonNull(tracer.currentTransaction(), "active transaction is required"); // This mimics setting the name through the public API. We cannot use the public API if we want to test span recycling - transaction.withName("user-provided-name", AbstractSpan.PRIO_USER_SUPPLIED); + transaction.withName("user-provided-name", AbstractSpan.PRIORITY_USER_SUPPLIED); transactionId = transaction.getTraceContext().getId(); } diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingFunctional.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingFunctional.java index 774a69fb49..2ade37aa58 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingFunctional.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-testapp/src/main/java/co/elastic/apm/agent/springwebflux/testapp/GreetingFunctional.java @@ -19,7 +19,8 @@ package co.elastic.apm.agent.springwebflux.testapp; import co.elastic.apm.agent.impl.ElasticApmTracer; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; +import co.elastic.apm.agent.tracer.AbstractSpan; +import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.impl.transaction.Id; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.tracer.GlobalTracer; @@ -87,7 +88,7 @@ public RouterFunction route(GreetingHandler greetingHandler) { ElasticApmTracer tracer = GlobalTracer.get().require(ElasticApmTracer.class); Transaction transaction = Objects.requireNonNull(tracer.currentTransaction(), "active transaction is required"); // This mimics setting the name through the public API. We cannot use the public API if we want to test span recycling - transaction.withName("user-provided-name", AbstractSpan.PRIO_USER_SUPPLIED); + transaction.withName("user-provided-name", AbstractSpan.PRIORITY_USER_SUPPLIED); transactionId = transaction.getTraceContext().getId(); } return response(greetingHandler.helloMessage("transaction=" + transactionId)); diff --git a/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/ServletWrappingControllerTransactionNameInstrumentation.java b/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/ServletWrappingControllerTransactionNameInstrumentation.java index 29e77188ea..5488e93218 100644 --- a/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/ServletWrappingControllerTransactionNameInstrumentation.java +++ b/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/ServletWrappingControllerTransactionNameInstrumentation.java @@ -30,7 +30,7 @@ import java.util.Collection; import java.util.Collections; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; import static net.bytebuddy.matcher.ElementMatchers.named; /** @@ -61,7 +61,7 @@ public static void onEnter(@Advice.FieldValue("servletClass") Class servletCl if (transaction == null) { return; } - TransactionNameUtils.setTransactionNameByServletClass(request.getMethod(), servletClass, transaction.getAndOverrideName(PRIO_HIGH_LEVEL_FRAMEWORK)); + TransactionNameUtils.setTransactionNameByServletClass(request.getMethod(), servletClass, transaction.getAndOverrideName(PRIORITY_HIGH_LEVEL_FRAMEWORK)); } } diff --git a/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/SpringTransactionNameInstrumentation.java b/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/SpringTransactionNameInstrumentation.java index a5dbbd918d..3407bd47df 100644 --- a/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/SpringTransactionNameInstrumentation.java +++ b/apm-agent-plugins/apm-spring-webmvc-plugin/src/main/java/co/elastic/apm/agent/springwebmvc/SpringTransactionNameInstrumentation.java @@ -35,8 +35,8 @@ import java.util.Collections; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isInterface; import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; @@ -126,7 +126,7 @@ public static void setTransactionName(@Advice.Argument(0) HttpServletRequest req TransactionNameUtils.setNameFromClassAndMethod( className, methodName, - transaction.getAndOverrideName(PRIO_HIGH_LEVEL_FRAMEWORK) + transaction.getAndOverrideName(PRIORITY_HIGH_LEVEL_FRAMEWORK) ); } else if (webConfig.isUsePathAsName()) { // When method name or class name are not known, we treat the calculated name as a fallback only, thus using lower priority. @@ -135,7 +135,7 @@ public static void setTransactionName(@Advice.Argument(0) HttpServletRequest req request.getMethod(), request.getServletPath(), request.getPathInfo(), - transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK + 1), + transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK + 1), webConfig.getUrlGroups() ); } else if (!className.isEmpty()) { @@ -143,11 +143,11 @@ public static void setTransactionName(@Advice.Argument(0) HttpServletRequest req TransactionNameUtils.setNameFromClassAndMethod( className, methodName, - transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK + 1) + transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK + 1) ); } else { // Class name is empty - probably an anonymous handler class - StringBuilder transactionName = transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK + 1); + StringBuilder transactionName = transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK + 1); if (transactionName != null) { transactionName.append(request.getMethod()).append(" unknown route"); } diff --git a/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ActionProxyAdvice.java b/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ActionProxyAdvice.java index c322bfd763..f5780ccacf 100644 --- a/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ActionProxyAdvice.java +++ b/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ActionProxyAdvice.java @@ -29,7 +29,7 @@ import javax.annotation.Nullable; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; public class ActionProxyAdvice { @@ -44,10 +44,10 @@ public static Object onEnterExecute(@Advice.This ActionProxy actionProxy) { String methodName = actionProxy.getMethod(); if (ActionContext.getContext().get("CHAIN_HISTORY") != null) { Span span = transaction.createSpan().withType("app").withSubtype("internal"); - TransactionNameUtils.setNameFromClassAndMethod(className, methodName, span.getAndOverrideName(PRIO_HIGH_LEVEL_FRAMEWORK)); + TransactionNameUtils.setNameFromClassAndMethod(className, methodName, span.getAndOverrideName(PRIORITY_HIGH_LEVEL_FRAMEWORK)); return span.activate(); } else { - TransactionNameUtils.setNameFromClassAndMethod(className, methodName, transaction.getAndOverrideName(PRIO_HIGH_LEVEL_FRAMEWORK)); + TransactionNameUtils.setNameFromClassAndMethod(className, methodName, transaction.getAndOverrideName(PRIORITY_HIGH_LEVEL_FRAMEWORK)); StrutsFrameworkUtils.setFrameworkNameAndVersion(transaction); return null; } diff --git a/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ExecuteOperationsAdvice.java b/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ExecuteOperationsAdvice.java index eb9f53a727..bc805662bb 100644 --- a/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ExecuteOperationsAdvice.java +++ b/apm-agent-plugins/apm-struts-plugin/src/main/java/co/elastic/apm/agent/struts/ExecuteOperationsAdvice.java @@ -26,7 +26,7 @@ import javax.servlet.http.HttpServletRequest; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK; public class ExecuteOperationsAdvice { @@ -39,7 +39,7 @@ public static void setTransactionName(@Advice.Argument(0) HttpServletRequest req return; } - StringBuilder transactionName = transaction.getAndOverrideName(PRIO_HIGH_LEVEL_FRAMEWORK); + StringBuilder transactionName = transaction.getAndOverrideName(PRIORITY_HIGH_LEVEL_FRAMEWORK); if (transactionName != null) { TransactionNameUtils.setNameFromHttpRequestPath(request.getMethod(), request.getServletPath(), transactionName, webConfig.getUrlGroups()); StrutsFrameworkUtils.setFrameworkNameAndVersion(transaction); diff --git a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java index dfcc125770..4978e7485c 100644 --- a/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java +++ b/apm-agent-plugins/apm-urlconnection-plugin/src/main/java/co/elastic/apm/agent/urlconnection/HttpUrlConnectionInstrumentation.java @@ -24,10 +24,10 @@ import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Outcome; import co.elastic.apm.agent.tracer.Span; -import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.sdk.state.CallDepth; import co.elastic.apm.agent.sdk.state.GlobalState; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -86,7 +86,7 @@ public static Object enter(@Advice.This HttpURLConnection thiz, if (span == null && !connected) { final URL url = thiz.getURL(); span = HttpClientHelper.startHttpClientSpan(parent, thiz.getRequestMethod(), url.toString(), url.getProtocol(), url.getHost(), url.getPort()); - if (!TraceContext.containsTraceContextTextHeaders(thiz, UrlConnectionPropertyAccessor.instance())) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), thiz, UrlConnectionPropertyAccessor.instance())) { if (span != null) { span.propagateTraceContext(thiz, UrlConnectionPropertyAccessor.instance()); } else { diff --git a/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractHttpTransactionHelper.java b/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractHttpTransactionHelper.java index 064713c9e4..a40d7d3329 100644 --- a/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractHttpTransactionHelper.java +++ b/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractHttpTransactionHelper.java @@ -37,8 +37,8 @@ import java.util.Set; import static co.elastic.apm.agent.configuration.CoreConfiguration.EventType.OFF; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_DEFAULT; -import static co.elastic.apm.agent.impl.transaction.AbstractSpan.PRIO_LOW_LEVEL_FRAMEWORK; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_DEFAULT; +import static co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_LOW_LEVEL_FRAMEWORK; public abstract class AbstractHttpTransactionHelper { private static final Logger logger = LoggerFactory.getLogger(AbstractHttpTransactionHelper.class); @@ -101,10 +101,10 @@ public void applyDefaultTransactionName(String method, String pathFirstPart, @Nu method, pathFirstPart, pathSecondPart, - transaction.getAndOverrideName(PRIO_LOW_LEVEL_FRAMEWORK + 1 + priorityOffset), + transaction.getAndOverrideName(PRIORITY_LOW_LEVEL_FRAMEWORK + 1 + priorityOffset), webConfiguration.getUrlGroups()); } else { - TransactionNameUtils.setNameUnknownRoute(method, transaction.getAndOverrideName(PRIO_DEFAULT)); + TransactionNameUtils.setNameUnknownRoute(method, transaction.getAndOverrideName(PRIORITY_DEFAULT)); } } diff --git a/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractVertxWebHelper.java b/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractVertxWebHelper.java index 442a3d345f..91b1642261 100644 --- a/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractVertxWebHelper.java +++ b/apm-agent-plugins/apm-vertx/apm-vertx-common/src/main/java/co/elastic/apm/agent/vertx/AbstractVertxWebHelper.java @@ -19,9 +19,7 @@ package co.elastic.apm.agent.vertx; import co.elastic.apm.agent.impl.context.web.ResultUtil; -import co.elastic.apm.agent.impl.transaction.AbstractSpan; -import co.elastic.apm.agent.sdk.logging.Logger; -import co.elastic.apm.agent.sdk.logging.LoggerFactory; +import co.elastic.apm.agent.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.tracer.metadata.Request; @@ -36,6 +34,8 @@ import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerResponse; import io.vertx.ext.web.RoutingContext; +import co.elastic.apm.agent.sdk.logging.Logger; +import co.elastic.apm.agent.sdk.logging.LoggerFactory; import javax.annotation.Nullable; import java.nio.CharBuffer; @@ -78,7 +78,7 @@ protected void setRouteBasedTransactionName(Transaction transaction, RoutingC if (!webConfiguration.isUsePathAsName()) { String path = routingContext.currentRoute().getPath(); if (path != null) { - StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIO_HIGH_LEVEL_FRAMEWORK - 1); + StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK - 1); if (transactionName != null) { transactionName.append(routingContext.request().method().name()) .append(" ").append(path); diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java index 2767ee206c..905ff403ef 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/AbstractSpan.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; -import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; @@ -28,6 +27,12 @@ public interface AbstractSpan> extends ElasticContext { + int PRIORITY_DEFAULT = 0; + int PRIORITY_LOW_LEVEL_FRAMEWORK = 10; + int PRIORITY_METHOD_SIGNATURE = 10 * PRIORITY_LOW_LEVEL_FRAMEWORK; + int PRIORITY_HIGH_LEVEL_FRAMEWORK = 10 * PRIORITY_LOW_LEVEL_FRAMEWORK; + int PRIORITY_USER_SUPPLIED = 100 * PRIORITY_LOW_LEVEL_FRAMEWORK; + AbstractContext getContext(); TraceContext getTraceContext(); diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java index 0c96307ffd..6916b2e23a 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/GlobalTracer.java @@ -23,6 +23,7 @@ import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; +import java.util.Set; public class GlobalTracer implements Tracer { @@ -81,6 +82,11 @@ public ObjectPoolFactory getObjectPoolFactory() { return tracer.getObjectPoolFactory(); } + @Override + public Set getTraceHeaderNames() { + return tracer.getTraceHeaderNames(); + } + @Nullable @Override public AbstractSpan getActive() { diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java index 3e9c980ec4..edca7331a7 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/NoopTracer.java @@ -23,6 +23,8 @@ import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Set; class NoopTracer implements Tracer { @@ -57,6 +59,11 @@ public ObjectPoolFactory getObjectPoolFactory() { throw new IllegalStateException(); } + @Override + public Set getTraceHeaderNames() { + return Collections.emptySet(); + } + @Nullable @Override public AbstractSpan getActive() { diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java index abb6373bbe..bef46b15b4 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Tracer.java @@ -24,6 +24,7 @@ import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; +import java.util.Set; public interface Tracer { @@ -38,6 +39,8 @@ public interface Tracer { ObjectPoolFactory getObjectPoolFactory(); + Set getTraceHeaderNames(); + @Nullable AbstractSpan getActive(); diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Transaction.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Transaction.java index 32174d1e69..1424da3766 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Transaction.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Transaction.java @@ -22,6 +22,8 @@ public interface Transaction> extends AbstractSpan { + String TYPE_REQUEST = "request"; + /** * Context *

diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/dispatch/HeaderUtils.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/dispatch/HeaderUtils.java new file mode 100644 index 0000000000..7925ef1aa8 --- /dev/null +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/dispatch/HeaderUtils.java @@ -0,0 +1,33 @@ +package co.elastic.apm.agent.tracer.dispatch; + +import java.util.Set; + +public class HeaderUtils { + + private HeaderUtils() { + } + + public static boolean containsAny(Set headerNames, C carrier, TextHeaderGetter headerGetter) { + for (String headerName : headerNames) { + if (headerGetter.getFirstHeader(headerName, carrier) != null) { + return true; + } + } + return false; + } + + public static void copy(Set headerNames, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + for (String headerName : headerNames) { + String headerValue = headerGetter.getFirstHeader(headerName, source); + if (headerValue != null) { + headerSetter.setHeader(headerName, headerValue, destination); + } + } + } + + public static void remove(Set headerNames, C carrier, HeaderRemover headerRemover) { + for (String headerName : headerNames) { + headerRemover.remove(headerName, carrier); + } + } +}