From 3cb453f7cc866bc2af239c9b4b455e49ad6b72be Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Thu, 23 Mar 2023 09:26:01 +0100 Subject: [PATCH 01/11] Offer static trace context methods via tracer API to allow custom implementations and refactor use. --- .../apm/agent/impl/ElasticApmTracer.java | 17 +++++++++++++++++ .../v3/HttpClient3Instrumentation.java | 5 ++--- ...eHttpAsyncClientRedirectInstrumentation.java | 5 ++--- .../v4/ApacheHttpClientInstrumentation.java | 5 ++--- .../LegacyApacheHttpClientInstrumentation.java | 5 ++--- .../v4/helper/ApacheHttpAsyncClientHelper.java | 9 ++++++++- .../helper/HttpAsyncRequestProducerWrapper.java | 3 +-- .../awslambda/helper/SNSTransactionHelper.java | 7 +------ .../awslambda/helper/SQSTransactionHelper.java | 7 +------ ...FinaglePayloadSizeFilterInstrumentation.java | 5 ++--- .../co/elastic/apm/agent/grpc/GrpcHelper.java | 7 +++++-- .../jms/JmsMessageConsumerInstrumentation.java | 1 - .../KafkaInstrumentationHeadersHelper.java | 3 +-- .../OkHttp3ClientAsyncInstrumentation.java | 3 +-- .../okhttp/OkHttp3ClientInstrumentation.java | 3 +-- .../OkHttpClientAsyncInstrumentation.java | 3 +-- .../okhttp/OkHttpClientInstrumentation.java | 3 +-- .../HttpUrlConnectionInstrumentation.java | 3 +-- .../elastic/apm/agent/tracer/GlobalTracer.java | 17 +++++++++++++++++ .../co/elastic/apm/agent/tracer/NoopTracer.java | 15 +++++++++++++++ .../co/elastic/apm/agent/tracer/Tracer.java | 8 ++++++++ 21 files changed, 89 insertions(+), 45 deletions(-) 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..b47a461e72 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 @@ -47,6 +47,8 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.GlobalTracer; +import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; +import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.util.DependencyInjectingServiceLoader; import co.elastic.apm.agent.util.ExecutorUtils; import co.elastic.apm.agent.tracer.Scope; @@ -916,4 +918,19 @@ public T require(Class type) { } return cast; } + + @Override + public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { + return TraceContext.containsTraceContextTextHeaders(carrier, headerGetter); + } + + @Override + public void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + TraceContext.copyTraceContextTextHeaders(source, headerGetter, destination, headerSetter); + } + + @Override + public void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { + TraceContext.removeTraceContextHeaders(carrier, headerRemover); + } } 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..9165c6c1e9 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,7 +20,6 @@ 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; @@ -121,10 +120,10 @@ public static Object onEnter(@Advice.Argument(0) HttpMethod httpMethod, span.activate(); } - if (!TraceContext.containsTraceContextTextHeaders(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(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..b0974ace02 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,6 @@ 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 net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -49,8 +48,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 (tracer.containsTraceContextTextHeaders(original, RequestHeaderAccessor.INSTANCE) && !tracer.containsTraceContextTextHeaders(redirect, RequestHeaderAccessor.INSTANCE)) { + tracer.copyTraceContextTextHeaders(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..3dcd0c474b 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,6 @@ 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 net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -64,10 +63,10 @@ public static Object onBeforeExecute(@Advice.Argument(0) HttpRoute route, span.activate(); } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(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..380ba297f3 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,6 @@ 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 net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -106,10 +105,10 @@ public static Object onBeforeExecute(@Advice.Argument(0) @Nullable HttpHost host span.activate(); } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(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..330ffb39e8 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,6 @@ 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.pooling.Recyclable; import org.apache.http.HttpException; import org.apache.http.HttpHost; @@ -95,7 +94,7 @@ public HttpRequest generateRequest() throws IOException, HttpException { } } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!asyncClientHelper.getTracer().containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } else if (parent != null) { 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-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..a8aef57df3 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,7 +24,6 @@ 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; @@ -149,10 +148,10 @@ public static Object onBeforeExecute(@Nullable @Advice.Argument(0) Request reque } } - if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!TraceContext.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { // adds headers of potential parent exit-spans parent.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } 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..2ac71287fd 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,7 +23,6 @@ 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; @@ -88,6 +87,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 +101,8 @@ public GrpcHelper() { headerSetter = new GrpcHeaderSetter(); headerGetter = new GrpcHeaderGetter(); + + tracer = GlobalTracer.get(); } // transaction management (server part) @@ -447,7 +450,7 @@ public Span clientCallStartEnter(ClientCall clientCall, clientCallListenerSpans.put(listener, span); - if (!TraceContext.containsTraceContextTextHeaders(headers, headerGetter)) { + if (!tracer.containsTraceContextTextHeaders(headers, headerGetter)) { span.propagateTraceContext(headers, headerSetter); } 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-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..ad3daaa7d8 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,7 +21,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.tracer.Tracer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; @@ -121,6 +120,6 @@ public void setOutgoingTraceContextHeaders(Span span, ProducerRecord pr } public void removeTraceContextHeader(ProducerRecord producerRecord) { - TraceContext.removeTraceContextHeaders(producerRecord, KafkaRecordHeaderAccessor.instance()); + tracer.removeTraceContextHeaders(producerRecord, KafkaRecordHeaderAccessor.instance()); } } 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..ee7a5e372c 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,6 @@ 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 net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; @@ -87,7 +86,7 @@ public static Object[] onBeforeEnqueue(final @Advice.Origin Class Transaction startChildTransaction(@Nullable C headerCarrier, TextH public Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader) { return tracer.startChildTransaction(headerCarrier, binaryHeadersGetter, initiatingClassLoader); } + + @Override + public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { + return tracer.containsTraceContextTextHeaders(carrier, headerGetter); + } + + @Override + public void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + tracer.copyTraceContextTextHeaders(source, headerGetter, destination, headerSetter); + } + + @Override + public void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { + tracer.removeTraceContextHeaders(carrier, headerRemover); + } } 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..cc6a878217 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 @@ -19,7 +19,9 @@ package co.elastic.apm.agent.tracer; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; +import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; +import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; @@ -86,4 +88,17 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, TextH public Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader) { return null; } + + @Override + public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { + return false; + } + + @Override + public void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + } + + @Override + public void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { + } } 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..a062332f83 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 @@ -20,7 +20,9 @@ import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; +import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; +import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; @@ -81,4 +83,10 @@ public interface Tracer { */ @Nullable Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader); + + boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter); + + void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter); + + void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover); } From 512ef270591e6e37b9fd05e35cd19e665da4b852 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Thu, 23 Mar 2023 09:49:18 +0100 Subject: [PATCH 02/11] Avoid use of TraceContext constants in favor of tracer API interaction. --- .../elastic/apm/agent/impl/ElasticApmTracer.java | 6 ++++++ .../apm/agent/impl/transaction/TraceContext.java | 12 ++++++++++++ .../apm/agent/awssdk/v1/helper/SQSHelper.java | 14 +++++--------- .../apm/agent/awssdk/v2/helper/SQSHelper.java | 7 ++----- .../common/AbstractSQSInstrumentationHelper.java | 4 +--- .../co/elastic/apm/agent/tracer/GlobalTracer.java | 6 ++++++ .../co/elastic/apm/agent/tracer/NoopTracer.java | 7 +++++++ .../java/co/elastic/apm/agent/tracer/Tracer.java | 3 +++ 8 files changed, 42 insertions(+), 17 deletions(-) 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 b47a461e72..338821f02b 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 @@ -66,6 +66,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; @@ -919,6 +920,11 @@ public T require(Class type) { return cast; } + @Override + public Set getTraceParentHeaders() { + return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS; + } + @Override public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { return TraceContext.containsTraceContextTextHeaders(carrier, headerGetter); 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..9e116b8abe 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 @@ -98,6 +101,15 @@ public class TraceContext implements Recyclable, co.elastic.apm.agent.tracer.Tra private static final Double SAMPLE_RATE_ZERO = 0d; + public static final Set TRACE_PARENT_TEXTUAL_HEADERS; + + static { + Set traceParentTextualHeaders = new HashSet<>(); + traceParentTextualHeaders.add(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); + traceParentTextualHeaders.add(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME); + TRACE_PARENT_TEXTUAL_HEADERS = Collections.unmodifiableSet(traceParentTextualHeaders); + } + private static final ChildContextCreator FROM_PARENT_CONTEXT = new ChildContextCreator() { @Override public boolean asChildOf(TraceContext child, TraceContext parent) { 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..cc0b7e7ff2 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.getTraceParentHeaders()) { + 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.getTraceParentHeaders().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..34e587c252 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.getTraceParentHeaders())) { 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.getTraceParentHeaders()); 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/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..3514d82be4 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; @@ -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.getTraceParentHeaders().contains(key) && value != null && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) { message.addHeader(key, value); 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 4d0c28c455..ec4452e0d8 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 @@ -25,6 +25,7 @@ import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; +import java.util.Set; public class GlobalTracer implements Tracer { @@ -113,6 +114,11 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, Binar return tracer.startChildTransaction(headerCarrier, binaryHeadersGetter, initiatingClassLoader); } + @Override + public Set getTraceParentHeaders() { + return tracer.getTraceParentHeaders(); + } + @Override public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { return tracer.containsTraceContextTextHeaders(carrier, headerGetter); 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 cc6a878217..e076701e8d 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 @@ -25,6 +25,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 { @@ -89,6 +91,11 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, Binar return null; } + @Override + public Set getTraceParentHeaders() { + return Collections.emptySet(); + } + @Override public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { return false; 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 a062332f83..a44ded254b 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 @@ -26,6 +26,7 @@ import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; +import java.util.Set; public interface Tracer { @@ -84,6 +85,8 @@ public interface Tracer { @Nullable Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader); + Set getTraceParentHeaders(); + boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter); void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter); From fbe176026149fa0035260d332ef6861811fb00ba Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Thu, 23 Mar 2023 10:12:41 +0100 Subject: [PATCH 03/11] Move decision over what trace headers are to tracer API to allow for their modification. --- .../apm/agent/impl/ElasticApmTracer.java | 14 ++++++++-- .../agent/impl/transaction/TraceContext.java | 16 +++++++++-- .../apm/agent/awssdk/v1/helper/SQSHelper.java | 5 ++-- .../apm/agent/awssdk/v2/helper/SQSHelper.java | 5 ++-- .../AbstractSQSInstrumentationHelper.java | 3 +- .../agent/jms/JmsInstrumentationHelper.java | 12 +++----- .../agent/jms/JmsMessagePropertyAccessor.java | 13 +++++---- .../ConsumerRecordsIteratorWrapper.java | 4 +-- .../apm/agent/tracer/GlobalTracer.java | 4 +-- .../elastic/apm/agent/tracer/NoopTracer.java | 2 +- .../apm/agent/tracer/TraceHeaderDisplay.java | 28 +++++++++++++++++++ .../co/elastic/apm/agent/tracer/Tracer.java | 2 +- 12 files changed, 80 insertions(+), 28 deletions(-) create mode 100644 apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java 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 338821f02b..62ab8da30e 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 @@ -47,6 +47,7 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.GlobalTracer; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.util.DependencyInjectingServiceLoader; @@ -921,8 +922,17 @@ public T require(Class type) { } @Override - public Set getTraceParentHeaders() { - return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS; + public Set getTraceParentHeaders(TraceHeaderDisplay display) { + switch (display) { + case REGULAR: + return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS_REGULAR; + case BINARY: + return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS_BINARY; + case QUEUE: + return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS_QUEUE; + default: + throw new IllegalStateException(); + } } @Override 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 9e116b8abe..5082aa7c4e 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 @@ -22,6 +22,7 @@ import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.impl.Tracer; import co.elastic.apm.agent.impl.sampling.Sampler; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; @@ -101,13 +102,24 @@ public class TraceContext implements Recyclable, co.elastic.apm.agent.tracer.Tra private static final Double SAMPLE_RATE_ZERO = 0d; - public static final Set TRACE_PARENT_TEXTUAL_HEADERS; + public static final Set TRACE_PARENT_TEXTUAL_HEADERS_REGULAR; + public static final Set TRACE_PARENT_TEXTUAL_HEADERS_BINARY; + public static final Set TRACE_PARENT_TEXTUAL_HEADERS_QUEUE; static { Set traceParentTextualHeaders = new HashSet<>(); traceParentTextualHeaders.add(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); traceParentTextualHeaders.add(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME); - TRACE_PARENT_TEXTUAL_HEADERS = Collections.unmodifiableSet(traceParentTextualHeaders); + TRACE_PARENT_TEXTUAL_HEADERS_REGULAR = Collections.unmodifiableSet(traceParentTextualHeaders); + Set traceParentTextualHeadersBinary = new HashSet<>(); + Set traceParentTextualHeadersQueue = new HashSet<>(); + for (String header : traceParentTextualHeaders) { + traceParentTextualHeadersBinary.add(TraceHeaderDisplay.BINARY.format(header)); + traceParentTextualHeadersQueue.add(TraceHeaderDisplay.QUEUE.format(header)); + } + TRACE_PARENT_TEXTUAL_HEADERS_BINARY = Collections.unmodifiableSet(traceParentTextualHeadersBinary); + TRACE_PARENT_TEXTUAL_HEADERS_QUEUE = Collections.unmodifiableSet(traceParentTextualHeadersQueue); + } private static final ChildContextCreator FROM_PARENT_CONTEXT = new ChildContextCreator() { 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 cc0b7e7ff2..4c1a67241b 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 @@ -25,6 +25,7 @@ import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.common.util.WildcardMatcher; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import com.amazonaws.AmazonWebServiceRequest; @@ -71,7 +72,7 @@ public void propagateContext(Span span, AmazonWebServiceRequest request) { public void setMessageAttributeNames(ReceiveMessageRequest receiveMessageRequest) { List messageAttributeNames = receiveMessageRequest.getMessageAttributeNames(); - for (String header : tracer.getTraceParentHeaders()) { + for (String header : tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR)) { if (!messageAttributeNames.contains(ATTRIBUTE_NAME_ALL) && !messageAttributeNames.contains(header)) { messageAttributeNames.add(header); } @@ -139,7 +140,7 @@ protected void setMessageContext(@Nullable Message sqsMessage, @Nullable String if (coreConfiguration.isCaptureHeaders()) { for (Map.Entry entry : sqsMessage.getMessageAttributes().entrySet()) { String key = entry.getKey(); - if (!tracer.getTraceParentHeaders().contains(key) && + if (!tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR).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 34e587c252..947ecd1b7e 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,6 +21,7 @@ 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.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import software.amazon.awssdk.core.SdkRequest; @@ -126,11 +127,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) && - Collections.disjoint(receiveMessageRequest.messageAttributeNames(), tracer.getTraceParentHeaders())) { + Collections.disjoint(receiveMessageRequest.messageAttributeNames(), tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR))) { List newMessageAttributeNames = new ArrayList<>(receiveMessageRequest.messageAttributeNames().size() + 2); newMessageAttributeNames.addAll(receiveMessageRequest.messageAttributeNames()); - newMessageAttributeNames.addAll(tracer.getTraceParentHeaders()); + newMessageAttributeNames.addAll(tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR)); 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/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 3514d82be4..063e06598b 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,6 +22,7 @@ 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.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -193,7 +194,7 @@ protected void setMessageContext(@Nullable MessageT sqsMessage, @Nullable String if (coreConfiguration.isCaptureHeaders()) { for (String key : getMessageAttributeKeys(sqsMessage)) { String value = getMessageAttribute(sqsMessage, key); - if (!tracer.getTraceParentHeaders().contains(key) && + if (!tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR).contains(key) && value != null && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) { message.addHeader(key, value); 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..f5c9a46b7e 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 @@ -22,7 +22,7 @@ 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.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -44,11 +44,6 @@ 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 @@ -258,8 +253,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) && + !tracer.getTraceParentHeaders(TraceHeaderDisplay.QUEUE).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/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..b963446d09 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,9 +18,11 @@ */ 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.GlobalTracer; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.AbstractHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; @@ -31,8 +33,6 @@ 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,7 +43,10 @@ public static JmsMessagePropertyAccessor instance() { return INSTANCE; } + private final Tracer tracer; + private JmsMessagePropertyAccessor() { + tracer = GlobalTracer.get(); } @Nullable @@ -61,9 +64,9 @@ public String getFirstHeader(String headerName, Message message) { @Nonnull private String jmsifyHeaderName(String headerName) { - if (headerName.equals(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)) { + if (tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR).contains(headerName)) { // replacing with the JMS equivalent - headerName = JMS_TRACE_PARENT_PROPERTY; + headerName = TraceHeaderDisplay.QUEUE.format(headerName); } return headerName; } 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..b58df56b05 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,7 @@ 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.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -89,7 +89,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 (!tracer.getTraceParentHeaders(TraceHeaderDisplay.BINARY).contains(key) && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) { message.addHeader(key, header.value()); } 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 ec4452e0d8..d96f90482b 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 @@ -115,8 +115,8 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, Binar } @Override - public Set getTraceParentHeaders() { - return tracer.getTraceParentHeaders(); + public Set getTraceParentHeaders(TraceHeaderDisplay display) { + return tracer.getTraceParentHeaders(display); } @Override 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 e076701e8d..b9ff374600 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 @@ -92,7 +92,7 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, Binar } @Override - public Set getTraceParentHeaders() { + public Set getTraceParentHeaders(TraceHeaderDisplay display) { return Collections.emptySet(); } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java new file mode 100644 index 0000000000..9e505815e3 --- /dev/null +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java @@ -0,0 +1,28 @@ +package co.elastic.apm.agent.tracer; + +import java.util.regex.Pattern; + +public enum TraceHeaderDisplay { + REGULAR { + @Override + public String format(String header) { + return header; + } + }, + BINARY { + @Override + public String format(String header) { + return pattern.matcher(header).replaceAll(""); + } + }, + QUEUE { + @Override + public String format(String header) { + return header.replace('-', '_'); + } + }; + + final Pattern pattern = Pattern.compile("[^a-zA-Z0-9]"); + + public abstract String format(String header); +} 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 a44ded254b..bd6f367a9f 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 @@ -85,7 +85,7 @@ public interface Tracer { @Nullable Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader); - Set getTraceParentHeaders(); + Set getTraceParentHeaders(TraceHeaderDisplay display); boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter); From ca78d37bf1ad89928d7d6c9b04bae609da3bde00 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Sat, 25 Mar 2023 12:38:05 +0100 Subject: [PATCH 04/11] Move remaining span-related constants and unroll unnecessary abbreviation in 'priority' values. --- .../apm/agent/impl/transaction/AbstractSpan.java | 15 +++++---------- .../apm/agent/impl/transaction/Transaction.java | 2 -- .../pluginapi/AbstractSpanInstrumentation.java | 4 ++-- .../CaptureTransactionInstrumentation.java | 8 ++++---- .../pluginapi/LegacySpanInstrumentation.java | 4 ++-- .../agent/pluginapi/TracedInstrumentation.java | 10 +++++----- .../AbstractDynamoDBInstrumentationHelper.java | 2 +- .../common/AbstractS3InstrumentationHelper.java | 4 ++-- .../common/AbstractSQSInstrumentationHelper.java | 4 ++-- .../AbstractAPIGatewayTransactionHelper.java | 4 ++-- .../helper/AbstractLambdaTransactionHelper.java | 4 ++-- .../AbstractMessageBasedTransactionHelper.java | 4 ++-- .../awslambda/helper/S3TransactionHelper.java | 4 ++-- .../apm/agent/cassandra/CassandraHelper.java | 4 ++-- .../apm/agent/dubbo/helper/DubboTraceHelper.java | 6 +++--- .../GrailsTransactionNameInstrumentation.java | 4 ++-- .../BaseServerEndpointInstrumentation.java | 6 +++--- .../apm/agent/javalin/JavalinInstrumentation.java | 4 ++-- .../JaxRsTransactionNameInstrumentation.java | 4 ++-- .../JaxWsTransactionNameInstrumentation.java | 4 ++-- .../elastic/apm/agent/jdbc/helper/JdbcHelper.java | 2 +- .../apm/agent/httpserver/HttpHandlerAdvice.java | 6 +++--- .../apm/agent/httpserver/HttpHandlerTest.java | 2 +- ...bstractJsfLifecycleExecuteInstrumentation.java | 6 +++--- .../agent/kafka/KafkaConsumerInstrumentation.java | 2 +- .../co/elastic/apm/agent/mongodb/MongoHelper.java | 2 +- .../apm/agent/opentelemetry/tracing/OTelSpan.java | 2 +- .../tracing/ElasticOpenTelemetryTest.java | 2 +- .../opentracingimpl/ApmSpanInstrumentation.java | 12 ++++++------ .../co/elastic/apm/agent/profiler/CallTree.java | 3 +-- .../apm/agent/servlet/ServletApiAdvice.java | 5 ++--- .../agent/servlet/ServletTransactionHelper.java | 8 ++++---- .../elastic/apm/agent/servlet/ApmFilterTest.java | 4 ++-- .../servlet/ServletTransactionHelperTest.java | 8 ++++---- .../elastic/apm/agent/sparkjava/RoutesAdvice.java | 4 ++-- .../HandlerAdapterInstrumentation.java | 4 ++-- .../apm/agent/springwebflux/WebfluxHelper.java | 8 ++++---- .../springwebflux/testapp/GreetingAnnotated.java | 4 ++-- .../springwebflux/testapp/GreetingFunctional.java | 4 ++-- ...gControllerTransactionNameInstrumentation.java | 4 ++-- .../SpringTransactionNameInstrumentation.java | 12 ++++++------ .../apm/agent/struts/ActionProxyAdvice.java | 6 +++--- .../apm/agent/struts/ExecuteOperationsAdvice.java | 4 ++-- .../vertx/AbstractHttpTransactionHelper.java | 8 ++++---- .../apm/agent/vertx/AbstractVertxWebHelper.java | 4 ++-- .../co/elastic/apm/agent/tracer/AbstractSpan.java | 7 ++++++- .../co/elastic/apm/agent/tracer/Transaction.java | 2 ++ 47 files changed, 117 insertions(+), 119 deletions(-) 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/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-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-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 063e06598b..54e598b9f5 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 @@ -118,14 +118,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() 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-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-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-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-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-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-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelSpan.java b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelSpan.java index 872e759052..cb69675368 100644 --- a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelSpan.java +++ b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/tracing/OTelSpan.java @@ -107,7 +107,7 @@ public Span recordException(Throwable exception, Attributes additionalAttributes @Override public Span updateName(String name) { - span.withName(name, AbstractSpan.PRIO_USER_SUPPLIED); + span.withName(name, co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_USER_SUPPLIED); return this; } diff --git a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java index 2bce882ea5..effe25f7fc 100644 --- a/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java +++ b/apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/test/java/co/elastic/apm/agent/opentelemetry/tracing/ElasticOpenTelemetryTest.java @@ -452,7 +452,7 @@ public void otelSpanOverActiveElasticTransaction() { @Test public void overrideElasticTransactionName() { Transaction transaction = startTestRootTransaction() - .withName("Elastic Provided High-Prio Name", AbstractSpan.PRIO_USER_SUPPLIED); + .withName("Elastic Provided High-Prio Name", co.elastic.apm.agent.tracer.AbstractSpan.PRIORITY_USER_SUPPLIED); try { Span.current().updateName("Otel updated name"); diff --git a/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanInstrumentation.java b/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanInstrumentation.java index 154ad78f71..16bd9e34df 100644 --- a/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanInstrumentation.java +++ b/apm-agent-plugins/apm-opentracing-plugin/src/main/java/co/elastic/apm/agent/opentracingimpl/ApmSpanInstrumentation.java @@ -33,7 +33,7 @@ import javax.annotation.Nullable; import java.util.Map; -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.takesArguments; @@ -77,7 +77,7 @@ public static void doFinishInternal(AbstractSpan 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-plugin/src/main/java/co/elastic/apm/agent/springwebflux/HandlerAdapterInstrumentation.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/HandlerAdapterInstrumentation.java index d5e03e2c00..b662d2c5d8 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/HandlerAdapterInstrumentation.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/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-plugin/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java index e91665d144..e802ae382f 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-plugin/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 4a3bd4633e..d244e364ac 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,7 @@ 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.Transaction; import co.elastic.apm.agent.sdk.logging.Logger; @@ -174,7 +174,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); return greetingHandler.helloMessage("transaction=" + Objects.requireNonNull(tracer.currentTransaction()).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 9962c5b7c8..bc9334f0d7 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,7 @@ 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.Transaction; import org.springframework.context.annotation.Bean; @@ -84,7 +84,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); return response(greetingHandler.helloMessage("transaction=" + Objects.requireNonNull(tracer.currentTransaction()).getTraceContext().getId())); }) .GET("/functional/child-flux", accept(MediaType.TEXT_PLAIN), request -> ServerResponse.ok() 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-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 d7fa427e94..fa3c449a95 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,7 +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.tracer.AbstractSpan; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.tracer.metadata.Request; @@ -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); + StringBuilder transactionName = transaction.getAndOverrideName(AbstractSpan.PRIORITY_HIGH_LEVEL_FRAMEWORK); 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/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 *

From 4c2a840b4f182ddb0672e8edda25894e2aabb5e6 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 28 Mar 2023 20:08:25 +0200 Subject: [PATCH 05/11] Add display arguments for copy/contains/remove methods. --- .../apm/agent/impl/ElasticApmTracer.java | 12 ++++----- .../agent/impl/transaction/TraceContext.java | 27 +++++++++---------- .../impl/transaction/TraceContextTest.java | 7 ++--- ...ttpAsyncClientRedirectInstrumentation.java | 6 +++-- .../KafkaInstrumentationHeadersHelper.java | 3 ++- .../apm/agent/tracer/GlobalTracer.java | 12 ++++----- .../elastic/apm/agent/tracer/NoopTracer.java | 6 ++--- .../apm/agent/tracer/TraceHeaderDisplay.java | 4 +-- .../co/elastic/apm/agent/tracer/Tracer.java | 6 ++--- 9 files changed, 43 insertions(+), 40 deletions(-) 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 62ab8da30e..42ecaac9b6 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 @@ -936,17 +936,17 @@ public Set getTraceParentHeaders(TraceHeaderDisplay display) { } @Override - public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { - return TraceContext.containsTraceContextTextHeaders(carrier, headerGetter); + public boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { + return TraceContext.containsTraceContextTextHeaders(display, carrier, headerGetter); } @Override - public void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - TraceContext.copyTraceContextTextHeaders(source, headerGetter, destination, headerSetter); + public void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + TraceContext.copyTraceContextTextHeaders(display, source, headerGetter, destination, headerSetter); } @Override - public void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { - TraceContext.removeTraceContextHeaders(carrier, headerRemover); + public void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { + TraceContext.removeTraceContextHeaders(display, carrier, headerRemover); } } 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 5082aa7c4e..076d53b76b 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 @@ -207,30 +207,29 @@ public boolean asChildOf(TraceContext child, Object ignore) { }; - public static boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { - return headerGetter.getFirstHeader(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier) != null; + public static boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { + return headerGetter.getFirstHeader(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier) != null; } - public static void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { - 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 removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { + headerRemover.remove(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); + headerRemover.remove(display.format(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); + headerRemover.remove(display.format(TRACESTATE_HEADER_NAME), carrier); } - public static void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - String w3cApmTraceParent = headerGetter.getFirstHeader(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, source); + public static void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + String w3cApmTraceParent = headerGetter.getFirstHeader(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); if (w3cApmTraceParent != null) { - headerSetter.setHeader(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, w3cApmTraceParent, destination); + headerSetter.setHeader(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), w3cApmTraceParent, destination); } - String elasticApmTraceParent = headerGetter.getFirstHeader(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, source); + String elasticApmTraceParent = headerGetter.getFirstHeader(display.format(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); if (elasticApmTraceParent != null) { - headerSetter.setHeader(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, elasticApmTraceParent, destination); + headerSetter.setHeader(display.format(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), elasticApmTraceParent, destination); } // copying only the first tracestate header - String tracestate = headerGetter.getFirstHeader(TRACESTATE_HEADER_NAME, source); + String tracestate = headerGetter.getFirstHeader(display.format(TRACESTATE_HEADER_NAME), source); if (tracestate != null) { - headerSetter.setHeader(TRACESTATE_HEADER_NAME, tracestate, destination); + headerSetter.setHeader(display.format(TRACESTATE_HEADER_NAME), tracestate, destination); } } 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..bb4718fd0f 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 @@ -29,6 +29,7 @@ import co.elastic.apm.agent.impl.sampling.ConstantSampler; import co.elastic.apm.agent.impl.sampling.Sampler; import co.elastic.apm.agent.objectpool.TestObjectPoolFactory; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.util.HexUtils; import co.elastic.apm.agent.util.PotentiallyMultiValuedMap; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; @@ -179,7 +180,7 @@ void testTraceContextTextHeadersRemoval() { headerMap.put(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); headerMap.put(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); headerMap.put(TraceContext.TRACESTATE_HEADER_NAME, "foo=bar,baz=qux"); - TraceContext.removeTraceContextHeaders(headerMap, TextHeaderMapAccessor.INSTANCE); + TraceContext.removeTraceContextHeaders(TraceHeaderDisplay.REGULAR, headerMap, TextHeaderMapAccessor.INSTANCE); assertThat(headerMap.get(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNull(); assertThat(headerMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNull(); assertThat(headerMap.get(TraceContext.TRACESTATE_HEADER_NAME)).isNull(); @@ -189,7 +190,7 @@ void testTraceContextTextHeadersRemoval() { 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); + TraceContext.removeTraceContextHeaders(TraceHeaderDisplay.BINARY, headerMap, BinaryHeaderMapAccessor.INSTANCE); assertThat(headerMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isNull(); } @@ -200,7 +201,7 @@ void testTraceContextHeadersCopy() { original.put(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); original.put(TraceContext.TRACESTATE_HEADER_NAME, "foo=bar,baz=qux"); Map copy = new HashMap<>(); - TraceContext.copyTraceContextTextHeaders(original, TextHeaderMapAccessor.INSTANCE, copy, TextHeaderMapAccessor.INSTANCE); + TraceContext.copyTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, original, TextHeaderMapAccessor.INSTANCE, copy, TextHeaderMapAccessor.INSTANCE); assertThat(copy.get(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotNull(); assertThat(copy.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotNull(); assertThat(copy.get(TraceContext.TRACESTATE_HEADER_NAME)).isNotNull(); 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 b0974ace02..a9f93690c9 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,6 +19,7 @@ package co.elastic.apm.agent.httpclient.v4; import co.elastic.apm.agent.httpclient.v4.helper.RequestHeaderAccessor; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -31,6 +32,7 @@ import java.security.ProtectionDomain; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.implementationVersionLte; +import static co.elastic.apm.agent.tracer.TraceHeaderDisplay.REGULAR; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.nameContains; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -48,8 +50,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 (tracer.containsTraceContextTextHeaders(original, RequestHeaderAccessor.INSTANCE) && !tracer.containsTraceContextTextHeaders(redirect, RequestHeaderAccessor.INSTANCE)) { - tracer.copyTraceContextTextHeaders(original, RequestHeaderAccessor.INSTANCE, redirect, RequestHeaderAccessor.INSTANCE); + if (tracer.containsTraceContextTextHeaders(REGULAR, original, RequestHeaderAccessor.INSTANCE) && !tracer.containsTraceContextTextHeaders(REGULAR, redirect, RequestHeaderAccessor.INSTANCE)) { + tracer.copyTraceContextTextHeaders(REGULAR, original, RequestHeaderAccessor.INSTANCE, redirect, RequestHeaderAccessor.INSTANCE); } } } 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 ad3daaa7d8..1e9bf96be7 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,6 +21,7 @@ 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.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.Tracer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; @@ -120,6 +121,6 @@ public void setOutgoingTraceContextHeaders(Span span, ProducerRecord pr } public void removeTraceContextHeader(ProducerRecord producerRecord) { - tracer.removeTraceContextHeaders(producerRecord, KafkaRecordHeaderAccessor.instance()); + tracer.removeTraceContextHeaders(TraceHeaderDisplay.REGULAR, producerRecord, KafkaRecordHeaderAccessor.instance()); } } 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 d96f90482b..bfd6ef906d 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 @@ -120,17 +120,17 @@ public Set getTraceParentHeaders(TraceHeaderDisplay display) { } @Override - public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { - return tracer.containsTraceContextTextHeaders(carrier, headerGetter); + public boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { + return tracer.containsTraceContextTextHeaders(display, carrier, headerGetter); } @Override - public void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - tracer.copyTraceContextTextHeaders(source, headerGetter, destination, headerSetter); + public void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + tracer.copyTraceContextTextHeaders(display, source, headerGetter, destination, headerSetter); } @Override - public void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { - tracer.removeTraceContextHeaders(carrier, headerRemover); + public void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { + tracer.removeTraceContextHeaders(display, carrier, headerRemover); } } 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 b9ff374600..83b9c51d6f 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 @@ -97,15 +97,15 @@ public Set getTraceParentHeaders(TraceHeaderDisplay display) { } @Override - public boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter) { + public boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { return false; } @Override - public void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + public void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { } @Override - public void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { + public void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { } } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java index 9e505815e3..2bd5ddd9a6 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java @@ -12,7 +12,7 @@ public String format(String header) { BINARY { @Override public String format(String header) { - return pattern.matcher(header).replaceAll(""); + return PATTERN.matcher(header).replaceAll(""); } }, QUEUE { @@ -22,7 +22,7 @@ public String format(String header) { } }; - final Pattern pattern = Pattern.compile("[^a-zA-Z0-9]"); + static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9]"); public abstract String format(String header); } 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 bd6f367a9f..de79dac920 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 @@ -87,9 +87,9 @@ public interface Tracer { Set getTraceParentHeaders(TraceHeaderDisplay display); - boolean containsTraceContextTextHeaders(C carrier, TextHeaderGetter headerGetter); + boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter); - void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter); + void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter); - void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover); + void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover); } From e9d3749367f9eec92b87909d20c50b5590874a5f Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 28 Mar 2023 22:21:05 +0200 Subject: [PATCH 06/11] Fix remaining methods to specify header formatting. --- .../v3/HttpClient3Instrumentation.java | 5 +++-- .../v4/ApacheHttpClientInstrumentation.java | 5 +++-- .../LegacyApacheHttpClientInstrumentation.java | 5 +++-- .../HttpAsyncRequestProducerWrapper.java | 3 ++- .../pluginapi/SpanInstrumentationTest.java | 3 ++- ...inaglePayloadSizeFilterInstrumentation.java | 5 +++-- .../co/elastic/apm/agent/grpc/GrpcHelper.java | 3 ++- .../AbstractHttpClientInstrumentationTest.java | 5 +++-- .../apm/agent/jms/JmsInstrumentationIT.java | 6 ++++-- .../OkHttp3ClientAsyncInstrumentation.java | 3 ++- .../okhttp/OkHttp3ClientInstrumentation.java | 3 ++- .../OkHttpClientAsyncInstrumentation.java | 3 ++- .../okhttp/OkHttpClientInstrumentation.java | 3 ++- .../HttpUrlConnectionInstrumentation.java | 3 ++- .../apm/agent/tracer/TraceHeaderDisplay.java | 18 ++++++++++++++++++ 15 files changed, 53 insertions(+), 20 deletions(-) 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 9165c6c1e9..a3cef6c4e1 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 @@ -25,6 +25,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.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.util.LoggerUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.method.MethodDescription; @@ -120,10 +121,10 @@ public static Object onEnter(@Advice.Argument(0) HttpMethod httpMethod, span.activate(); } - if (!tracer.containsTraceContextTextHeaders(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, 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/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 3dcd0c474b..0923547ba1 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,6 +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.tracer.TraceHeaderDisplay; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -63,10 +64,10 @@ public static Object onBeforeExecute(@Advice.Argument(0) HttpRoute route, span.activate(); } - if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, 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 380ba297f3..673cf76e59 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,6 +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.tracer.TraceHeaderDisplay; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -105,10 +106,10 @@ public static Object onBeforeExecute(@Advice.Argument(0) @Nullable HttpHost host span.activate(); } - if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, 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/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 330ffb39e8..dbc2f27fe0 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,6 +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.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.pooling.Recyclable; import org.apache.http.HttpException; import org.apache.http.HttpHost; @@ -94,7 +95,7 @@ public HttpRequest generateRequest() throws IOException, HttpException { } } - if (!asyncClientHelper.getTracer().containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!asyncClientHelper.getTracer().containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, 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/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java b/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java index c08ad9089f..81ea3c8b55 100644 --- a/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java +++ b/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java @@ -22,6 +22,7 @@ import co.elastic.apm.agent.impl.TextHeaderMapAccessor; import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.objectpool.impl.BookkeeperObjectPool; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import co.elastic.apm.api.ElasticApm; import co.elastic.apm.api.Scope; import co.elastic.apm.api.Span; @@ -245,7 +246,7 @@ private void assertContainsTracingHeaders(Span span) { final Map tracingHeaders = new HashMap<>(); span.injectTraceHeaders(tracingHeaders::put); span.injectTraceHeaders(null); - assertThat(TraceContext.containsTraceContextTextHeaders(tracingHeaders, TextHeaderMapAccessor.INSTANCE)).isTrue(); + assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, tracingHeaders, TextHeaderMapAccessor.INSTANCE)).isTrue(); } } } 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 a8aef57df3..6fefcd3c12 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 @@ -28,6 +28,7 @@ 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.TraceHeaderDisplay; import com.twitter.finagle.http.Request; import com.twitter.finagle.http.Response; import com.twitter.finagle.tracing.Trace; @@ -148,10 +149,10 @@ public static Object onBeforeExecute(@Nullable @Advice.Argument(0) Request reque } } - if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { // adds headers of potential parent exit-spans parent.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } 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 2ac71287fd..60e463c25a 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,6 +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.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; @@ -450,7 +451,7 @@ public Span clientCallStartEnter(ClientCall clientCall, clientCallListenerSpans.put(listener, span); - if (!tracer.containsTraceContextTextHeaders(headers, headerGetter)) { + if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, headers, headerGetter)) { span.propagateTraceContext(headers, headerSetter); } diff --git a/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java b/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java index 8e5aae606a..3db2c18038 100644 --- a/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java +++ b/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java @@ -28,6 +28,7 @@ import co.elastic.apm.agent.impl.transaction.Span; import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; @@ -300,7 +301,7 @@ protected Span verifyHttpSpan(String host, String path, int status, boolean requ verifyTraceContextHeaders(span, path); } else { findLoggedRequests(path).forEach(request -> - assertThat(TraceContext.containsTraceContextTextHeaders(request, HeaderAccessor.INSTANCE)).isFalse() + assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, HeaderAccessor.INSTANCE)).isFalse() ); } } @@ -318,7 +319,7 @@ private void verifyTraceContextHeaders(Span span, String path) { assertThat(headerMap).isNotEmpty(); final List loggedRequests = findLoggedRequests(path); loggedRequests.forEach(request -> { - assertThat(TraceContext.containsTraceContextTextHeaders(request, HeaderAccessor.INSTANCE)).isTrue(); + assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, HeaderAccessor.INSTANCE)).isTrue(); AtomicInteger headerCount = new AtomicInteger(); HeaderAccessor.INSTANCE.forEach( W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, 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..4e5d8d2991 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 @@ -32,6 +32,7 @@ import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; +import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -67,7 +68,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 +78,8 @@ @RunWith(Parameterized.class) public class JmsInstrumentationIT extends AbstractInstrumentationTest { + private static final String JMS_TRACE_PARENT_PROPERTY = TraceHeaderDisplay.QUEUE.format(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); + // Keeping a static reference for resource cleaning private final static Set staticBrokerFacade = new HashSet<>(); @@ -483,7 +485,7 @@ private void verifyMessageHeaders(Message message, Transaction receiveTransactio assertThat(String.valueOf(message.getStringProperty("passwd"))).isEqualTo("secret"); assertThat(headersMap.get("passwd")).isNull(); assertThat(headersMap.get("null_property")).isEqualTo("null"); - assertThat(String.valueOf(message.getStringProperty(JMS_TRACE_PARENT_PROPERTY))).isNotNull(); + assertThat(String.valueOf(message.getStringProperty(TraceHeaderDisplay.QUEUE.format(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)))).isNotNull(); assertThat(headersMap.get(JMS_TRACE_PARENT_PROPERTY)).isNull(); } 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 ee7a5e372c..2a12da51ad 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,6 +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.tracer.TraceHeaderDisplay; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; @@ -86,7 +87,7 @@ public static Object[] onBeforeEnqueue(final @Advice.Origin Class Date: Tue, 28 Mar 2023 23:54:43 +0200 Subject: [PATCH 07/11] Initialize tracer for JMS message property test to align with actual headers. --- .../elastic/apm/agent/jms/JmsMessagePropertyAccessorTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 { From 542f77c11882bde8e1cf3842513aaeabf2fb0d93 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Fri, 14 Apr 2023 02:11:09 +0200 Subject: [PATCH 08/11] Adjust names of methods and make Kafka use binary encoding. --- .../apm/agent/impl/ElasticApmTracer.java | 22 ++++----- .../agent/impl/transaction/TraceContext.java | 46 ++++++++++--------- .../impl/transaction/TraceContextTest.java | 8 ++-- .../v3/HttpClient3Instrumentation.java | 6 +-- ...ttpAsyncClientRedirectInstrumentation.java | 7 ++- .../v4/ApacheHttpClientInstrumentation.java | 6 +-- ...LegacyApacheHttpClientInstrumentation.java | 6 +-- .../HttpAsyncRequestProducerWrapper.java | 4 +- .../pluginapi/SpanInstrumentationTest.java | 4 +- .../apm/agent/awssdk/v1/helper/SQSHelper.java | 6 +-- .../apm/agent/awssdk/v2/helper/SQSHelper.java | 6 +-- .../AbstractSQSInstrumentationHelper.java | 4 +- ...naglePayloadSizeFilterInstrumentation.java | 6 +-- .../co/elastic/apm/agent/grpc/GrpcHelper.java | 4 +- ...AbstractHttpClientInstrumentationTest.java | 6 +-- .../agent/jms/JmsInstrumentationHelper.java | 4 +- .../agent/jms/JmsMessagePropertyAccessor.java | 6 +-- .../apm/agent/jms/JmsInstrumentationIT.java | 6 +-- .../ConsumerRecordsIteratorWrapper.java | 4 +- .../KafkaInstrumentationHeadersHelper.java | 4 +- .../OkHttp3ClientAsyncInstrumentation.java | 4 +- .../okhttp/OkHttp3ClientInstrumentation.java | 4 +- .../OkHttpClientAsyncInstrumentation.java | 4 +- .../okhttp/OkHttpClientInstrumentation.java | 4 +- .../HttpUrlConnectionInstrumentation.java | 4 +- .../apm/agent/tracer/GlobalTracer.java | 16 +++---- .../elastic/apm/agent/tracer/NoopTracer.java | 8 ++-- ...play.java => TraceHeaderNameEncoding.java} | 10 ++-- .../co/elastic/apm/agent/tracer/Tracer.java | 8 ++-- 29 files changed, 114 insertions(+), 113 deletions(-) rename apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/{TraceHeaderDisplay.java => TraceHeaderNameEncoding.java} (84%) 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 42ecaac9b6..f5fbeaab60 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 @@ -47,7 +47,7 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.util.DependencyInjectingServiceLoader; @@ -922,31 +922,31 @@ public T require(Class type) { } @Override - public Set getTraceParentHeaders(TraceHeaderDisplay display) { + public Set getTraceHeaderNames(TraceHeaderNameEncoding display) { switch (display) { case REGULAR: - return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS_REGULAR; + return TraceContext.TRACE_TEXTUAL_HEADERS_REGULAR; case BINARY: - return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS_BINARY; + return TraceContext.TRACE_TEXTUAL_HEADERS_BINARY; case QUEUE: - return TraceContext.TRACE_PARENT_TEXTUAL_HEADERS_QUEUE; + return TraceContext.TRACE_TEXTUAL_HEADERS_QUEUE; default: throw new IllegalStateException(); } } @Override - public boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { - return TraceContext.containsTraceContextTextHeaders(display, carrier, headerGetter); + public boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { + return TraceContext.containsTraceContextTextHeaders(encoding, carrier, headerGetter); } @Override - public void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - TraceContext.copyTraceContextTextHeaders(display, source, headerGetter, destination, headerSetter); + public void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + TraceContext.copyTraceContextTextHeaders(encoding, source, headerGetter, destination, headerSetter); } @Override - public void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { - TraceContext.removeTraceContextHeaders(display, carrier, headerRemover); + public void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { + TraceContext.removeTraceContextHeaders(encoding, carrier, headerRemover); } } 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 076d53b76b..47fa01d5df 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 @@ -22,7 +22,7 @@ import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.impl.Tracer; import co.elastic.apm.agent.impl.sampling.Sampler; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; @@ -102,23 +102,24 @@ public class TraceContext implements Recyclable, co.elastic.apm.agent.tracer.Tra private static final Double SAMPLE_RATE_ZERO = 0d; - public static final Set TRACE_PARENT_TEXTUAL_HEADERS_REGULAR; - public static final Set TRACE_PARENT_TEXTUAL_HEADERS_BINARY; - public static final Set TRACE_PARENT_TEXTUAL_HEADERS_QUEUE; + public static final Set TRACE_TEXTUAL_HEADERS_REGULAR; + public static final Set TRACE_TEXTUAL_HEADERS_BINARY; + public static final Set TRACE_TEXTUAL_HEADERS_QUEUE; static { Set traceParentTextualHeaders = new HashSet<>(); traceParentTextualHeaders.add(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); traceParentTextualHeaders.add(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME); - TRACE_PARENT_TEXTUAL_HEADERS_REGULAR = Collections.unmodifiableSet(traceParentTextualHeaders); + traceParentTextualHeaders.add(TRACESTATE_HEADER_NAME); + TRACE_TEXTUAL_HEADERS_REGULAR = Collections.unmodifiableSet(traceParentTextualHeaders); Set traceParentTextualHeadersBinary = new HashSet<>(); Set traceParentTextualHeadersQueue = new HashSet<>(); for (String header : traceParentTextualHeaders) { - traceParentTextualHeadersBinary.add(TraceHeaderDisplay.BINARY.format(header)); - traceParentTextualHeadersQueue.add(TraceHeaderDisplay.QUEUE.format(header)); + traceParentTextualHeadersBinary.add(TraceHeaderNameEncoding.BINARY.encode(header)); + traceParentTextualHeadersQueue.add(TraceHeaderNameEncoding.QUEUE.encode(header)); } - TRACE_PARENT_TEXTUAL_HEADERS_BINARY = Collections.unmodifiableSet(traceParentTextualHeadersBinary); - TRACE_PARENT_TEXTUAL_HEADERS_QUEUE = Collections.unmodifiableSet(traceParentTextualHeadersQueue); + TRACE_TEXTUAL_HEADERS_BINARY = Collections.unmodifiableSet(traceParentTextualHeadersBinary); + TRACE_TEXTUAL_HEADERS_QUEUE = Collections.unmodifiableSet(traceParentTextualHeadersQueue); } @@ -207,29 +208,30 @@ public boolean asChildOf(TraceContext child, Object ignore) { }; - public static boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { - return headerGetter.getFirstHeader(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier) != null; + public static boolean containsTraceContextTextHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { + // We assume that this header is always present if we found any of the other headers. + return headerGetter.getFirstHeader(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier) != null; } - public static void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { - headerRemover.remove(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); - headerRemover.remove(display.format(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); - headerRemover.remove(display.format(TRACESTATE_HEADER_NAME), carrier); + public static void removeTraceContextHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { + headerRemover.remove(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); + headerRemover.remove(encoding.encode(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); + headerRemover.remove(encoding.encode(TRACESTATE_HEADER_NAME), carrier); } - public static void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - String w3cApmTraceParent = headerGetter.getFirstHeader(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); + public static void copyTraceContextTextHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + String w3cApmTraceParent = headerGetter.getFirstHeader(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); if (w3cApmTraceParent != null) { - headerSetter.setHeader(display.format(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), w3cApmTraceParent, destination); + headerSetter.setHeader(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), w3cApmTraceParent, destination); } - String elasticApmTraceParent = headerGetter.getFirstHeader(display.format(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); + String elasticApmTraceParent = headerGetter.getFirstHeader(encoding.encode(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); if (elasticApmTraceParent != null) { - headerSetter.setHeader(display.format(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), elasticApmTraceParent, destination); + headerSetter.setHeader(encoding.encode(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), elasticApmTraceParent, destination); } // copying only the first tracestate header - String tracestate = headerGetter.getFirstHeader(display.format(TRACESTATE_HEADER_NAME), source); + String tracestate = headerGetter.getFirstHeader(encoding.encode(TRACESTATE_HEADER_NAME), source); if (tracestate != null) { - headerSetter.setHeader(display.format(TRACESTATE_HEADER_NAME), tracestate, destination); + headerSetter.setHeader(encoding.encode(TRACESTATE_HEADER_NAME), tracestate, destination); } } 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 bb4718fd0f..0f72cb6443 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 @@ -29,7 +29,7 @@ import co.elastic.apm.agent.impl.sampling.ConstantSampler; import co.elastic.apm.agent.impl.sampling.Sampler; import co.elastic.apm.agent.objectpool.TestObjectPoolFactory; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.util.HexUtils; import co.elastic.apm.agent.util.PotentiallyMultiValuedMap; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; @@ -180,7 +180,7 @@ void testTraceContextTextHeadersRemoval() { headerMap.put(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); headerMap.put(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); headerMap.put(TraceContext.TRACESTATE_HEADER_NAME, "foo=bar,baz=qux"); - TraceContext.removeTraceContextHeaders(TraceHeaderDisplay.REGULAR, headerMap, TextHeaderMapAccessor.INSTANCE); + TraceContext.removeTraceContextHeaders(TraceHeaderNameEncoding.REGULAR, headerMap, TextHeaderMapAccessor.INSTANCE); assertThat(headerMap.get(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNull(); assertThat(headerMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNull(); assertThat(headerMap.get(TraceContext.TRACESTATE_HEADER_NAME)).isNull(); @@ -190,7 +190,7 @@ void testTraceContextTextHeadersRemoval() { void testTraceContextBinaryHeadersRemoval() { Map headerMap = new HashMap<>(); headerMap.put(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01".getBytes(StandardCharsets.UTF_8)); - TraceContext.removeTraceContextHeaders(TraceHeaderDisplay.BINARY, headerMap, BinaryHeaderMapAccessor.INSTANCE); + TraceContext.removeTraceContextHeaders(TraceHeaderNameEncoding.BINARY, headerMap, BinaryHeaderMapAccessor.INSTANCE); assertThat(headerMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isNull(); } @@ -201,7 +201,7 @@ void testTraceContextHeadersCopy() { original.put(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); original.put(TraceContext.TRACESTATE_HEADER_NAME, "foo=bar,baz=qux"); Map copy = new HashMap<>(); - TraceContext.copyTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, original, TextHeaderMapAccessor.INSTANCE, copy, TextHeaderMapAccessor.INSTANCE); + TraceContext.copyTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, original, TextHeaderMapAccessor.INSTANCE, copy, TextHeaderMapAccessor.INSTANCE); assertThat(copy.get(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotNull(); assertThat(copy.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotNull(); assertThat(copy.get(TraceContext.TRACESTATE_HEADER_NAME)).isNotNull(); 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 a3cef6c4e1..4c17c08318 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 @@ -25,7 +25,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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; 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 (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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 a9f93690c9..15bc5f148c 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,6 @@ package co.elastic.apm.agent.httpclient.v4; import co.elastic.apm.agent.httpclient.v4.helper.RequestHeaderAccessor; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -32,7 +31,7 @@ import java.security.ProtectionDomain; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.implementationVersionLte; -import static co.elastic.apm.agent.tracer.TraceHeaderDisplay.REGULAR; +import static co.elastic.apm.agent.tracer.TraceHeaderNameEncoding.REGULAR; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.nameContains; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -50,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 (tracer.containsTraceContextTextHeaders(REGULAR, original, RequestHeaderAccessor.INSTANCE) && !tracer.containsTraceContextTextHeaders(REGULAR, redirect, RequestHeaderAccessor.INSTANCE)) { - tracer.copyTraceContextTextHeaders(REGULAR, original, RequestHeaderAccessor.INSTANCE, redirect, RequestHeaderAccessor.INSTANCE); + if (tracer.containsTraceHeaders(REGULAR, original, RequestHeaderAccessor.INSTANCE) && !tracer.containsTraceHeaders(REGULAR, redirect, RequestHeaderAccessor.INSTANCE)) { + tracer.copyTraceHeaders(REGULAR, 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 0923547ba1..2110bea813 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; 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 (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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 673cf76e59..05a9709ded 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; 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 (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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/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 dbc2f27fe0..041b5c97b2 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; 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 (!asyncClientHelper.getTracer().containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + if (!asyncClientHelper.getTracer().containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java b/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java index 81ea3c8b55..fcec825679 100644 --- a/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java +++ b/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java @@ -22,7 +22,7 @@ import co.elastic.apm.agent.impl.TextHeaderMapAccessor; import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.objectpool.impl.BookkeeperObjectPool; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.api.ElasticApm; import co.elastic.apm.api.Scope; import co.elastic.apm.api.Span; @@ -246,7 +246,7 @@ private void assertContainsTracingHeaders(Span span) { final Map tracingHeaders = new HashMap<>(); span.injectTraceHeaders(tracingHeaders::put); span.injectTraceHeaders(null); - assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, tracingHeaders, TextHeaderMapAccessor.INSTANCE)).isTrue(); + assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, tracingHeaders, TextHeaderMapAccessor.INSTANCE)).isTrue(); } } } 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 4c1a67241b..68cf9ab72d 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 @@ -25,7 +25,7 @@ import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.common.util.WildcardMatcher; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import com.amazonaws.AmazonWebServiceRequest; @@ -72,7 +72,7 @@ public void propagateContext(Span span, AmazonWebServiceRequest request) { public void setMessageAttributeNames(ReceiveMessageRequest receiveMessageRequest) { List messageAttributeNames = receiveMessageRequest.getMessageAttributeNames(); - for (String header : tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR)) { + for (String header : tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR)) { if (!messageAttributeNames.contains(ATTRIBUTE_NAME_ALL) && !messageAttributeNames.contains(header)) { messageAttributeNames.add(header); } @@ -140,7 +140,7 @@ protected void setMessageContext(@Nullable Message sqsMessage, @Nullable String if (coreConfiguration.isCaptureHeaders()) { for (Map.Entry entry : sqsMessage.getMessageAttributes().entrySet()) { String key = entry.getKey(); - if (!tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR).contains(key) && + if (!tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR).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 947ecd1b7e..32c067e3dc 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,7 @@ 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import software.amazon.awssdk.core.SdkRequest; @@ -127,11 +127,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) && - Collections.disjoint(receiveMessageRequest.messageAttributeNames(), tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR))) { + Collections.disjoint(receiveMessageRequest.messageAttributeNames(), tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR))) { List newMessageAttributeNames = new ArrayList<>(receiveMessageRequest.messageAttributeNames().size() + 2); newMessageAttributeNames.addAll(receiveMessageRequest.messageAttributeNames()); - newMessageAttributeNames.addAll(tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR)); + newMessageAttributeNames.addAll(tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR)); 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/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 54e598b9f5..115fd3ecc4 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,7 @@ 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -194,7 +194,7 @@ protected void setMessageContext(@Nullable MessageT sqsMessage, @Nullable String if (coreConfiguration.isCaptureHeaders()) { for (String key : getMessageAttributeKeys(sqsMessage)) { String value = getMessageAttribute(sqsMessage, key); - if (!tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR).contains(key) && + if (!tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR).contains(key) && value != null && WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) { message.addHeader(key, value); 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 6fefcd3c12..5eb3d3cab3 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 @@ -28,7 +28,7 @@ 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.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; 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 (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { // adds headers of potential parent exit-spans parent.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); } 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 60e463c25a..1a68ba0a30 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,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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; @@ -451,7 +451,7 @@ public Span clientCallStartEnter(ClientCall clientCall, clientCallListenerSpans.put(listener, span); - if (!tracer.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, headers, headerGetter)) { + if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, headers, headerGetter)) { span.propagateTraceContext(headers, headerSetter); } diff --git a/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java b/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java index 3db2c18038..a7983742fc 100644 --- a/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java +++ b/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java @@ -28,7 +28,7 @@ import co.elastic.apm.agent.impl.transaction.Span; import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; @@ -301,7 +301,7 @@ protected Span verifyHttpSpan(String host, String path, int status, boolean requ verifyTraceContextHeaders(span, path); } else { findLoggedRequests(path).forEach(request -> - assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, HeaderAccessor.INSTANCE)).isFalse() + assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, request, HeaderAccessor.INSTANCE)).isFalse() ); } } @@ -319,7 +319,7 @@ private void verifyTraceContextHeaders(Span span, String path) { assertThat(headerMap).isNotEmpty(); final List loggedRequests = findLoggedRequests(path); loggedRequests.forEach(request -> { - assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderDisplay.REGULAR, request, HeaderAccessor.INSTANCE)).isTrue(); + assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, request, HeaderAccessor.INSTANCE)).isTrue(); AtomicInteger headerCount = new AtomicInteger(); HeaderAccessor.INSTANCE.forEach( W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, 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 f5c9a46b7e..08090e4758 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 @@ -22,7 +22,7 @@ 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -254,7 +254,7 @@ public void addMessageDetails(@Nullable Message message, AbstractSpan span) { while (properties.hasMoreElements()) { String propertyName = String.valueOf(properties.nextElement()); if (!propertyName.equals(JMS_DESTINATION_NAME_PROPERTY) && - !tracer.getTraceParentHeaders(TraceHeaderDisplay.QUEUE).contains(propertyName) && + !tracer.getTraceHeaderNames(TraceHeaderNameEncoding.QUEUE).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/JmsMessagePropertyAccessor.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessor.java index b963446d09..41fed6e7ea 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 @@ -21,7 +21,7 @@ import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.AbstractHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; @@ -64,9 +64,9 @@ public String getFirstHeader(String headerName, Message message) { @Nonnull private String jmsifyHeaderName(String headerName) { - if (tracer.getTraceParentHeaders(TraceHeaderDisplay.REGULAR).contains(headerName)) { + if (tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR).contains(headerName)) { // replacing with the JMS equivalent - headerName = TraceHeaderDisplay.QUEUE.format(headerName); + headerName = TraceHeaderNameEncoding.QUEUE.encode(headerName); } return headerName; } 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 4e5d8d2991..517452901a 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 @@ -32,7 +32,7 @@ import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -78,7 +78,7 @@ @RunWith(Parameterized.class) public class JmsInstrumentationIT extends AbstractInstrumentationTest { - private static final String JMS_TRACE_PARENT_PROPERTY = TraceHeaderDisplay.QUEUE.format(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); + private static final String JMS_TRACE_PARENT_PROPERTY = TraceHeaderNameEncoding.QUEUE.encode(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); // Keeping a static reference for resource cleaning private final static Set staticBrokerFacade = new HashSet<>(); @@ -485,7 +485,7 @@ private void verifyMessageHeaders(Message message, Transaction receiveTransactio assertThat(String.valueOf(message.getStringProperty("passwd"))).isEqualTo("secret"); assertThat(headersMap.get("passwd")).isNull(); assertThat(headersMap.get("null_property")).isEqualTo("null"); - assertThat(String.valueOf(message.getStringProperty(TraceHeaderDisplay.QUEUE.format(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)))).isNotNull(); + assertThat(String.valueOf(message.getStringProperty(JMS_TRACE_PARENT_PROPERTY))).isNotNull(); assertThat(headersMap.get(JMS_TRACE_PARENT_PROPERTY)).isNull(); } 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 b58df56b05..057e4daeb4 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,7 @@ import co.elastic.apm.agent.configuration.CoreConfiguration; import co.elastic.apm.agent.configuration.MessagingConfiguration; -import co.elastic.apm.agent.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -89,7 +89,7 @@ public void endCurrentTransaction() { if (transaction.isSampled() && coreConfiguration.isCaptureHeaders()) { for (Header header : record.headers()) { String key = header.key(); - if (!tracer.getTraceParentHeaders(TraceHeaderDisplay.BINARY).contains(key) && + if (!tracer.getTraceHeaderNames(TraceHeaderNameEncoding.BINARY).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/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 1e9bf96be7..1f4744aae7 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,7 +21,7 @@ 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; @@ -121,6 +121,6 @@ public void setOutgoingTraceContextHeaders(Span span, ProducerRecord pr } public void removeTraceContextHeader(ProducerRecord producerRecord) { - tracer.removeTraceContextHeaders(TraceHeaderDisplay.REGULAR, producerRecord, KafkaRecordHeaderAccessor.instance()); + tracer.removeTraceHeaders(TraceHeaderNameEncoding.BINARY, producerRecord, KafkaRecordHeaderAccessor.instance()); } } 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 2a12da51ad..72fed54364 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.tracer.TraceHeaderDisplay; +import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; 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 Transaction startChildTransaction(@Nullable C headerCarrier, Binar } @Override - public Set getTraceParentHeaders(TraceHeaderDisplay display) { - return tracer.getTraceParentHeaders(display); + public Set getTraceHeaderNames(TraceHeaderNameEncoding display) { + return tracer.getTraceHeaderNames(display); } @Override - public boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { - return tracer.containsTraceContextTextHeaders(display, carrier, headerGetter); + public boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { + return tracer.containsTraceHeaders(encoding, carrier, headerGetter); } @Override - public void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - tracer.copyTraceContextTextHeaders(display, source, headerGetter, destination, headerSetter); + public void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + tracer.copyTraceHeaders(encoding, source, headerGetter, destination, headerSetter); } @Override - public void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { - tracer.removeTraceContextHeaders(display, carrier, headerRemover); + public void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { + tracer.removeTraceHeaders(encoding, carrier, headerRemover); } } 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 83b9c51d6f..71e18df020 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 @@ -92,20 +92,20 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, Binar } @Override - public Set getTraceParentHeaders(TraceHeaderDisplay display) { + public Set getTraceHeaderNames(TraceHeaderNameEncoding display) { return Collections.emptySet(); } @Override - public boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter) { + public boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { return false; } @Override - public void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + public void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { } @Override - public void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover) { + public void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { } } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderNameEncoding.java similarity index 84% rename from apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java rename to apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderNameEncoding.java index d4d002aabc..4411540089 100644 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderDisplay.java +++ b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderNameEncoding.java @@ -20,27 +20,27 @@ import java.util.regex.Pattern; -public enum TraceHeaderDisplay { +public enum TraceHeaderNameEncoding { REGULAR { @Override - public String format(String header) { + public String encode(String header) { return header; } }, BINARY { @Override - public String format(String header) { + public String encode(String header) { return PATTERN.matcher(header).replaceAll(""); } }, QUEUE { @Override - public String format(String header) { + public String encode(String header) { return header.replace('-', '_'); } }; static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9]"); - public abstract String format(String header); + public abstract String encode(String header); } 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 de79dac920..a65e9e485f 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 @@ -85,11 +85,11 @@ public interface Tracer { @Nullable Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader); - Set getTraceParentHeaders(TraceHeaderDisplay display); + Set getTraceHeaderNames(TraceHeaderNameEncoding display); - boolean containsTraceContextTextHeaders(TraceHeaderDisplay display, C carrier, TextHeaderGetter headerGetter); + boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter); - void copyTraceContextTextHeaders(TraceHeaderDisplay display, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter); + void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter); - void removeTraceContextHeaders(TraceHeaderDisplay display, C carrier, HeaderRemover headerRemover); + void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover); } From 883e9781870dccf79aec479c5918524cfcc143e1 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Fri, 14 Apr 2023 02:57:20 +0200 Subject: [PATCH 09/11] Move header translation to plugins. --- .../apm/agent/jms/BaseJmsInstrumentation.java | 2 +- .../agent/jms/JmsInstrumentationHelper.java | 31 +++++++++++++++++-- .../agent/jms/JmsMessagePropertyAccessor.java | 17 +++------- .../ConsumerRecordsIterableWrapper.java | 7 +++-- .../ConsumerRecordsIteratorWrapper.java | 8 +++-- .../helper/ConsumerRecordsListWrapper.java | 9 ++++-- .../KafkaInstrumentationHeadersHelper.java | 27 ++++++++++++++-- .../helper/KafkaRecordHeaderAccessor.java | 7 +++++ 8 files changed, 81 insertions(+), 27 deletions(-) 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 08090e4758..f53d7e64be 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,6 +21,7 @@ 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.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; @@ -41,6 +42,10 @@ 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 { @@ -79,14 +84,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 static JmsInstrumentationHelper get() { + return INSTANCE; + } - public JmsInstrumentationHelper(Tracer tracer) { + private JmsInstrumentationHelper(Tracer tracer) { this.tracer = tracer; coreConfiguration = tracer.getConfig(CoreConfiguration.class); messagingConfiguration = tracer.getConfig(MessagingConfiguration.class); + Set traceHeaders = tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR); + 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") @@ -254,7 +281,7 @@ public void addMessageDetails(@Nullable Message message, AbstractSpan span) { while (properties.hasMoreElements()) { String propertyName = String.valueOf(properties.nextElement()); if (!propertyName.equals(JMS_DESTINATION_NAME_PROPERTY) && - !tracer.getTraceHeaderNames(TraceHeaderNameEncoding.QUEUE).contains(propertyName) && + !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/JmsMessagePropertyAccessor.java b/apm-agent-plugins/apm-jms-plugin/apm-jms-plugin-base/src/main/java/co/elastic/apm/agent/jms/JmsMessagePropertyAccessor.java index 41fed6e7ea..b3ac36a40f 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 @@ -43,16 +43,16 @@ public static JmsMessagePropertyAccessor instance() { return INSTANCE; } - private final Tracer tracer; + private final JmsInstrumentationHelper helper; private JmsMessagePropertyAccessor() { - tracer = GlobalTracer.get(); + 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); @@ -62,18 +62,9 @@ public String getFirstHeader(String headerName, Message message) { return value; } - @Nonnull - private String jmsifyHeaderName(String headerName) { - if (tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR).contains(headerName)) { - // replacing with the JMS equivalent - headerName = TraceHeaderNameEncoding.QUEUE.encode(headerName); - } - 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-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 057e4daeb4..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.tracer.TraceHeaderNameEncoding; 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 (!tracer.getTraceHeaderNames(TraceHeaderNameEncoding.BINARY).contains(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 1f4744aae7..a4eaa8297c 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 @@ -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(TraceHeaderNameEncoding.REGULAR); + 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; 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); } From 99bbbf1302cfb2984785bc818254667f89d720ed Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Fri, 14 Apr 2023 03:20:32 +0200 Subject: [PATCH 10/11] Remove encoding and rely on plugins to decode. --- .../apm/agent/impl/ElasticApmTracer.java | 31 +------------ .../agent/impl/transaction/TraceContext.java | 43 +++++++---------- .../impl/transaction/TraceContextTest.java | 7 ++- .../v3/HttpClient3Instrumentation.java | 6 +-- ...ttpAsyncClientRedirectInstrumentation.java | 6 +-- .../v4/ApacheHttpClientInstrumentation.java | 6 +-- ...LegacyApacheHttpClientInstrumentation.java | 6 +-- .../HttpAsyncRequestProducerWrapper.java | 4 +- .../pluginapi/SpanInstrumentationTest.java | 3 +- .../apm/agent/awssdk/v1/helper/SQSHelper.java | 5 +- .../apm/agent/awssdk/v2/helper/SQSHelper.java | 5 +- .../AbstractSQSInstrumentationHelper.java | 3 +- ...naglePayloadSizeFilterInstrumentation.java | 6 +-- .../co/elastic/apm/agent/grpc/GrpcHelper.java | 4 +- ...AbstractHttpClientInstrumentationTest.java | 5 +- .../agent/jms/JmsInstrumentationHelper.java | 3 +- .../agent/jms/JmsMessagePropertyAccessor.java | 4 -- .../apm/agent/jms/JmsInstrumentationIT.java | 3 +- .../KafkaInstrumentationHeadersHelper.java | 6 +-- .../OkHttp3ClientAsyncInstrumentation.java | 4 +- .../okhttp/OkHttp3ClientInstrumentation.java | 4 +- .../OkHttpClientAsyncInstrumentation.java | 4 +- .../okhttp/OkHttpClientInstrumentation.java | 4 +- .../HttpUrlConnectionInstrumentation.java | 4 +- .../apm/agent/tracer/GlobalTracer.java | 27 ++--------- .../elastic/apm/agent/tracer/NoopTracer.java | 25 ++-------- .../agent/tracer/TraceHeaderNameEncoding.java | 46 ------------------- .../co/elastic/apm/agent/tracer/Tracer.java | 12 +---- .../agent/tracer/dispatch/HeaderUtils.java | 33 +++++++++++++ 29 files changed, 108 insertions(+), 211 deletions(-) delete mode 100644 apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderNameEncoding.java create mode 100644 apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/dispatch/HeaderUtils.java 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 f5fbeaab60..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 @@ -47,9 +47,6 @@ import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap; import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; -import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; -import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.util.DependencyInjectingServiceLoader; import co.elastic.apm.agent.util.ExecutorUtils; import co.elastic.apm.agent.tracer.Scope; @@ -922,31 +919,7 @@ public T require(Class type) { } @Override - public Set getTraceHeaderNames(TraceHeaderNameEncoding display) { - switch (display) { - case REGULAR: - return TraceContext.TRACE_TEXTUAL_HEADERS_REGULAR; - case BINARY: - return TraceContext.TRACE_TEXTUAL_HEADERS_BINARY; - case QUEUE: - return TraceContext.TRACE_TEXTUAL_HEADERS_QUEUE; - default: - throw new IllegalStateException(); - } - } - - @Override - public boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { - return TraceContext.containsTraceContextTextHeaders(encoding, carrier, headerGetter); - } - - @Override - public void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - TraceContext.copyTraceContextTextHeaders(encoding, source, headerGetter, destination, headerSetter); - } - - @Override - public void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { - TraceContext.removeTraceContextHeaders(encoding, carrier, headerRemover); + public Set getTraceHeaderNames() { + return TraceContext.TRACE_TEXTUAL_HEADERS; } } 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 47fa01d5df..be4327491c 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 @@ -22,7 +22,6 @@ import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.apm.agent.impl.Tracer; import co.elastic.apm.agent.impl.sampling.Sampler; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; @@ -102,25 +101,14 @@ 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_REGULAR; - public static final Set TRACE_TEXTUAL_HEADERS_BINARY; - public static final Set TRACE_TEXTUAL_HEADERS_QUEUE; + 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_REGULAR = Collections.unmodifiableSet(traceParentTextualHeaders); - Set traceParentTextualHeadersBinary = new HashSet<>(); - Set traceParentTextualHeadersQueue = new HashSet<>(); - for (String header : traceParentTextualHeaders) { - traceParentTextualHeadersBinary.add(TraceHeaderNameEncoding.BINARY.encode(header)); - traceParentTextualHeadersQueue.add(TraceHeaderNameEncoding.QUEUE.encode(header)); - } - TRACE_TEXTUAL_HEADERS_BINARY = Collections.unmodifiableSet(traceParentTextualHeadersBinary); - TRACE_TEXTUAL_HEADERS_QUEUE = Collections.unmodifiableSet(traceParentTextualHeadersQueue); - + TRACE_TEXTUAL_HEADERS = Collections.unmodifiableSet(traceParentTextualHeaders); } private static final ChildContextCreator FROM_PARENT_CONTEXT = new ChildContextCreator() { @@ -208,30 +196,31 @@ public boolean asChildOf(TraceContext child, Object ignore) { }; - public static boolean containsTraceContextTextHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { + 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(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier) != null; + return headerGetter.getFirstHeader(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier) != null; } - public static void removeTraceContextHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { - headerRemover.remove(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); - headerRemover.remove(encoding.encode(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), carrier); - headerRemover.remove(encoding.encode(TRACESTATE_HEADER_NAME), carrier); + public static void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { + headerRemover.remove(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier); + headerRemover.remove(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier); + headerRemover.remove(TRACE_PARENT_BINARY_HEADER_NAME, carrier); + headerRemover.remove(TRACESTATE_HEADER_NAME, carrier); } - public static void copyTraceContextTextHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - String w3cApmTraceParent = headerGetter.getFirstHeader(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); + public static void copyTraceContextTextHeaders(S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { + String w3cApmTraceParent = headerGetter.getFirstHeader(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, source); if (w3cApmTraceParent != null) { - headerSetter.setHeader(encoding.encode(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME), w3cApmTraceParent, destination); + headerSetter.setHeader(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, w3cApmTraceParent, destination); } - String elasticApmTraceParent = headerGetter.getFirstHeader(encoding.encode(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), source); + String elasticApmTraceParent = headerGetter.getFirstHeader(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, source); if (elasticApmTraceParent != null) { - headerSetter.setHeader(encoding.encode(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME), elasticApmTraceParent, destination); + headerSetter.setHeader(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, elasticApmTraceParent, destination); } // copying only the first tracestate header - String tracestate = headerGetter.getFirstHeader(encoding.encode(TRACESTATE_HEADER_NAME), source); + String tracestate = headerGetter.getFirstHeader(TRACESTATE_HEADER_NAME, source); if (tracestate != null) { - headerSetter.setHeader(encoding.encode(TRACESTATE_HEADER_NAME), tracestate, destination); + headerSetter.setHeader(TRACESTATE_HEADER_NAME, tracestate, destination); } } 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 0f72cb6443..853586115d 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 @@ -29,7 +29,6 @@ import co.elastic.apm.agent.impl.sampling.ConstantSampler; import co.elastic.apm.agent.impl.sampling.Sampler; import co.elastic.apm.agent.objectpool.TestObjectPoolFactory; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.util.HexUtils; import co.elastic.apm.agent.util.PotentiallyMultiValuedMap; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderSetter; @@ -180,7 +179,7 @@ void testTraceContextTextHeadersRemoval() { headerMap.put(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); headerMap.put(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); headerMap.put(TraceContext.TRACESTATE_HEADER_NAME, "foo=bar,baz=qux"); - TraceContext.removeTraceContextHeaders(TraceHeaderNameEncoding.REGULAR, headerMap, TextHeaderMapAccessor.INSTANCE); + TraceContext.removeTraceContextHeaders(headerMap, TextHeaderMapAccessor.INSTANCE); assertThat(headerMap.get(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNull(); assertThat(headerMap.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNull(); assertThat(headerMap.get(TraceContext.TRACESTATE_HEADER_NAME)).isNull(); @@ -190,7 +189,7 @@ void testTraceContextTextHeadersRemoval() { void testTraceContextBinaryHeadersRemoval() { Map headerMap = new HashMap<>(); headerMap.put(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01".getBytes(StandardCharsets.UTF_8)); - TraceContext.removeTraceContextHeaders(TraceHeaderNameEncoding.BINARY, headerMap, BinaryHeaderMapAccessor.INSTANCE); + TraceContext.removeTraceContextHeaders(headerMap, BinaryHeaderMapAccessor.INSTANCE); assertThat(headerMap.get(TraceContext.TRACE_PARENT_BINARY_HEADER_NAME)).isNull(); } @@ -201,7 +200,7 @@ void testTraceContextHeadersCopy() { original.put(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"); original.put(TraceContext.TRACESTATE_HEADER_NAME, "foo=bar,baz=qux"); Map copy = new HashMap<>(); - TraceContext.copyTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, original, TextHeaderMapAccessor.INSTANCE, copy, TextHeaderMapAccessor.INSTANCE); + TraceContext.copyTraceContextTextHeaders(original, TextHeaderMapAccessor.INSTANCE, copy, TextHeaderMapAccessor.INSTANCE); assertThat(copy.get(TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotNull(); assertThat(copy.get(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME)).isNotNull(); assertThat(copy.get(TraceContext.TRACESTATE_HEADER_NAME)).isNotNull(); 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 4c17c08318..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 @@ -25,7 +25,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.tracer.TraceHeaderNameEncoding; +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 (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(httpMethod, HttpClient3RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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 15bc5f148c..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,6 +19,7 @@ package co.elastic.apm.agent.httpclient.v4; import co.elastic.apm.agent.httpclient.v4.helper.RequestHeaderAccessor; +import co.elastic.apm.agent.tracer.dispatch.HeaderUtils; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; @@ -31,7 +32,6 @@ import java.security.ProtectionDomain; import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.implementationVersionLte; -import static co.elastic.apm.agent.tracer.TraceHeaderNameEncoding.REGULAR; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.nameContains; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -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 (tracer.containsTraceHeaders(REGULAR, original, RequestHeaderAccessor.INSTANCE) && !tracer.containsTraceHeaders(REGULAR, redirect, RequestHeaderAccessor.INSTANCE)) { - tracer.copyTraceHeaders(REGULAR, 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 2110bea813..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.tracer.TraceHeaderNameEncoding; +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 (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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 05a9709ded..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.tracer.TraceHeaderNameEncoding; +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 (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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/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 041b5c97b2..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.tracer.TraceHeaderNameEncoding; +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 (!asyncClientHelper.getTracer().containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java b/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java index fcec825679..c08ad9089f 100644 --- a/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java +++ b/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/agent/pluginapi/SpanInstrumentationTest.java @@ -22,7 +22,6 @@ import co.elastic.apm.agent.impl.TextHeaderMapAccessor; import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.objectpool.impl.BookkeeperObjectPool; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.api.ElasticApm; import co.elastic.apm.api.Scope; import co.elastic.apm.api.Span; @@ -246,7 +245,7 @@ private void assertContainsTracingHeaders(Span span) { final Map tracingHeaders = new HashMap<>(); span.injectTraceHeaders(tracingHeaders::put); span.injectTraceHeaders(null); - assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, tracingHeaders, TextHeaderMapAccessor.INSTANCE)).isTrue(); + assertThat(TraceContext.containsTraceContextTextHeaders(tracingHeaders, TextHeaderMapAccessor.INSTANCE)).isTrue(); } } } 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 68cf9ab72d..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 @@ -25,7 +25,6 @@ import co.elastic.apm.agent.tracer.GlobalTracer; import co.elastic.apm.agent.tracer.Span; import co.elastic.apm.agent.common.util.WildcardMatcher; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import com.amazonaws.AmazonWebServiceRequest; @@ -72,7 +71,7 @@ public void propagateContext(Span span, AmazonWebServiceRequest request) { public void setMessageAttributeNames(ReceiveMessageRequest receiveMessageRequest) { List messageAttributeNames = receiveMessageRequest.getMessageAttributeNames(); - for (String header : tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR)) { + for (String header : tracer.getTraceHeaderNames()) { if (!messageAttributeNames.contains(ATTRIBUTE_NAME_ALL) && !messageAttributeNames.contains(header)) { messageAttributeNames.add(header); } @@ -140,7 +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 (!tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR).contains(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 32c067e3dc..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.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import software.amazon.awssdk.core.SdkRequest; @@ -127,11 +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) && - Collections.disjoint(receiveMessageRequest.messageAttributeNames(), tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR))) { + Collections.disjoint(receiveMessageRequest.messageAttributeNames(), tracer.getTraceHeaderNames())) { List newMessageAttributeNames = new ArrayList<>(receiveMessageRequest.messageAttributeNames().size() + 2); newMessageAttributeNames.addAll(receiveMessageRequest.messageAttributeNames()); - newMessageAttributeNames.addAll(tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR)); + 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/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 115fd3ecc4..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.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -194,7 +193,7 @@ protected void setMessageContext(@Nullable MessageT sqsMessage, @Nullable String if (coreConfiguration.isCaptureHeaders()) { for (String key : getMessageAttributeKeys(sqsMessage)) { String value = getMessageAttribute(sqsMessage, key); - if (!tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR).contains(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-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 5eb3d3cab3..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 @@ -28,7 +28,7 @@ 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.TraceHeaderNameEncoding; +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 (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, request, RequestHeaderAccessor.INSTANCE)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), request, RequestHeaderAccessor.INSTANCE)) { if (span != null) { span.propagateTraceContext(request, RequestHeaderAccessor.INSTANCE); - } else if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, 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-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 1a68ba0a30..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.tracer.TraceHeaderNameEncoding; 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; @@ -451,7 +451,7 @@ public Span clientCallStartEnter(ClientCall clientCall, clientCallListenerSpans.put(listener, span); - if (!tracer.containsTraceHeaders(TraceHeaderNameEncoding.REGULAR, headers, headerGetter)) { + if (!HeaderUtils.containsAny(tracer.getTraceHeaderNames(), headers, headerGetter)) { span.propagateTraceContext(headers, headerSetter); } diff --git a/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java b/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java index a7983742fc..8e5aae606a 100644 --- a/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java +++ b/apm-agent-plugins/apm-httpclient-core/src/test/java/co/elastic/apm/agent/httpclient/AbstractHttpClientInstrumentationTest.java @@ -28,7 +28,6 @@ import co.elastic.apm.agent.impl.transaction.Span; import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; @@ -301,7 +300,7 @@ protected Span verifyHttpSpan(String host, String path, int status, boolean requ verifyTraceContextHeaders(span, path); } else { findLoggedRequests(path).forEach(request -> - assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, request, HeaderAccessor.INSTANCE)).isFalse() + assertThat(TraceContext.containsTraceContextTextHeaders(request, HeaderAccessor.INSTANCE)).isFalse() ); } } @@ -319,7 +318,7 @@ private void verifyTraceContextHeaders(Span span, String path) { assertThat(headerMap).isNotEmpty(); final List loggedRequests = findLoggedRequests(path); loggedRequests.forEach(request -> { - assertThat(TraceContext.containsTraceContextTextHeaders(TraceHeaderNameEncoding.REGULAR, request, HeaderAccessor.INSTANCE)).isTrue(); + assertThat(TraceContext.containsTraceContextTextHeaders(request, HeaderAccessor.INSTANCE)).isTrue(); AtomicInteger headerCount = new AtomicInteger(); HeaderAccessor.INSTANCE.forEach( W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, 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 f53d7e64be..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 @@ -23,7 +23,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.tracer.TraceHeaderNameEncoding; import co.elastic.apm.agent.tracer.Tracer; import co.elastic.apm.agent.tracer.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; @@ -101,7 +100,7 @@ private JmsInstrumentationHelper(Tracer tracer) { this.tracer = tracer; coreConfiguration = tracer.getConfig(CoreConfiguration.class); messagingConfiguration = tracer.getConfig(MessagingConfiguration.class); - Set traceHeaders = tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR); + Set traceHeaders = tracer.getTraceHeaderNames(); for (String traceHeader : traceHeaders) { String jmsTraceHeader = traceHeader.replace('-', '_'); if (!jmsTraceHeaders.add(jmsTraceHeader)) { 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 b3ac36a40f..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 @@ -20,14 +20,10 @@ import co.elastic.apm.agent.sdk.logging.Logger; import co.elastic.apm.agent.sdk.logging.LoggerFactory; -import co.elastic.apm.agent.tracer.GlobalTracer; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; -import co.elastic.apm.agent.tracer.Tracer; 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; 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 517452901a..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 @@ -32,7 +32,6 @@ import co.elastic.apm.agent.impl.transaction.TraceContext; import co.elastic.apm.agent.impl.transaction.Transaction; import co.elastic.apm.agent.common.util.WildcardMatcher; -import co.elastic.apm.agent.tracer.TraceHeaderNameEncoding; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -78,7 +77,7 @@ @RunWith(Parameterized.class) public class JmsInstrumentationIT extends AbstractInstrumentationTest { - private static final String JMS_TRACE_PARENT_PROPERTY = TraceHeaderNameEncoding.QUEUE.encode(TraceContext.ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME); + 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-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 a4eaa8297c..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.tracer.TraceHeaderNameEncoding; 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; @@ -61,7 +61,7 @@ public static KafkaInstrumentationHeadersHelper get() { public KafkaInstrumentationHeadersHelper(Tracer tracer) { this.tracer = tracer; Pattern pattern = Pattern.compile("[^a-zA-Z0-9]"); - Set traceHeaders = tracer.getTraceHeaderNames(TraceHeaderNameEncoding.REGULAR); + Set traceHeaders = tracer.getTraceHeaderNames(); for (String traceHeader : traceHeaders) { String binaryTraceHeader = pattern.matcher(traceHeader).replaceAll(""); if (!binaryTraceHeaders.add(binaryTraceHeader)) { @@ -142,6 +142,6 @@ public void setOutgoingTraceContextHeaders(Span span, ProducerRecord pr } public void removeTraceContextHeader(ProducerRecord producerRecord) { - tracer.removeTraceHeaders(TraceHeaderNameEncoding.BINARY, producerRecord, KafkaRecordHeaderAccessor.instance()); + HeaderUtils.remove(binaryTraceHeaders, producerRecord, KafkaRecordHeaderAccessor.instance()); } } 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 72fed54364..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.tracer.TraceHeaderNameEncoding; +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 getTraceHeaderNames() { + return tracer.getTraceHeaderNames(); + } + @Nullable @Override public AbstractSpan getActive() { @@ -113,24 +116,4 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, TextH public Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader) { return tracer.startChildTransaction(headerCarrier, binaryHeadersGetter, initiatingClassLoader); } - - @Override - public Set getTraceHeaderNames(TraceHeaderNameEncoding display) { - return tracer.getTraceHeaderNames(display); - } - - @Override - public boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { - return tracer.containsTraceHeaders(encoding, carrier, headerGetter); - } - - @Override - public void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - tracer.copyTraceHeaders(encoding, source, headerGetter, destination, headerSetter); - } - - @Override - public void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { - tracer.removeTraceHeaders(encoding, carrier, headerRemover); - } } 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 71e18df020..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 @@ -19,9 +19,7 @@ package co.elastic.apm.agent.tracer; import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; -import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; -import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; @@ -61,6 +59,11 @@ public ObjectPoolFactory getObjectPoolFactory() { throw new IllegalStateException(); } + @Override + public Set getTraceHeaderNames() { + return Collections.emptySet(); + } + @Nullable @Override public AbstractSpan getActive() { @@ -90,22 +93,4 @@ public Transaction startChildTransaction(@Nullable C headerCarrier, TextH public Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader) { return null; } - - @Override - public Set getTraceHeaderNames(TraceHeaderNameEncoding display) { - return Collections.emptySet(); - } - - @Override - public boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter) { - return false; - } - - @Override - public void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter) { - } - - @Override - public void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover) { - } } diff --git a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderNameEncoding.java b/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderNameEncoding.java deleted file mode 100644 index 4411540089..0000000000 --- a/apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/TraceHeaderNameEncoding.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package co.elastic.apm.agent.tracer; - -import java.util.regex.Pattern; - -public enum TraceHeaderNameEncoding { - REGULAR { - @Override - public String encode(String header) { - return header; - } - }, - BINARY { - @Override - public String encode(String header) { - return PATTERN.matcher(header).replaceAll(""); - } - }, - QUEUE { - @Override - public String encode(String header) { - return header.replace('-', '_'); - } - }; - - static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9]"); - - public abstract String encode(String header); -} 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 a65e9e485f..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 @@ -20,9 +20,7 @@ import co.elastic.apm.agent.tracer.dispatch.BinaryHeaderGetter; import co.elastic.apm.agent.tracer.dispatch.HeaderGetter; -import co.elastic.apm.agent.tracer.dispatch.HeaderRemover; import co.elastic.apm.agent.tracer.dispatch.TextHeaderGetter; -import co.elastic.apm.agent.tracer.dispatch.TextHeaderSetter; import co.elastic.apm.agent.tracer.pooling.ObjectPoolFactory; import javax.annotation.Nullable; @@ -41,6 +39,8 @@ public interface Tracer { ObjectPoolFactory getObjectPoolFactory(); + Set getTraceHeaderNames(); + @Nullable AbstractSpan getActive(); @@ -84,12 +84,4 @@ public interface Tracer { */ @Nullable Transaction startChildTransaction(@Nullable C headerCarrier, BinaryHeaderGetter binaryHeadersGetter, @Nullable ClassLoader initiatingClassLoader); - - Set getTraceHeaderNames(TraceHeaderNameEncoding display); - - boolean containsTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, TextHeaderGetter headerGetter); - - void copyTraceHeaders(TraceHeaderNameEncoding encoding, S source, TextHeaderGetter headerGetter, D destination, TextHeaderSetter headerSetter); - - void removeTraceHeaders(TraceHeaderNameEncoding encoding, C carrier, HeaderRemover headerRemover); } 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); + } + } +} From b5d4ccd72d0eefbf5aae52b4fea65e1aa09b597e Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Fri, 14 Apr 2023 03:30:30 +0200 Subject: [PATCH 11/11] Remove explicit handling of "binary" header names which is now the plugins responsibility. --- .../agent/impl/transaction/TraceContext.java | 8 ++--- .../impl/transaction/TraceContextTest.java | 36 ++++++++----------- 2 files changed, 17 insertions(+), 27 deletions(-) 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 be4327491c..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 @@ -85,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 @@ -170,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); } @@ -204,7 +203,6 @@ public static boolean containsTraceContextTextHeaders(C carrier, TextHeaderG public static void removeTraceContextHeaders(C carrier, HeaderRemover headerRemover) { headerRemover.remove(W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier); headerRemover.remove(ELASTIC_TRACE_PARENT_TEXTUAL_HEADER_NAME, carrier); - headerRemover.remove(TRACE_PARENT_BINARY_HEADER_NAME, carrier); headerRemover.remove(TRACESTATE_HEADER_NAME, carrier); } @@ -590,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()); @@ -598,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/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);