From 936e18534639baf1e0403e2e0746159e6d622452 Mon Sep 17 00:00:00 2001 From: user1 Date: Mon, 2 Nov 2020 15:19:31 +1100 Subject: [PATCH] Added tests and header propagation to start transactions --- mule4-agent/pom.xml | 2 +- .../apm/mule4/agent/span/SpanUtils.java | 11 ++++++++++ .../mule4/agent/tracing/HttpTracingUtils.java | 20 +++++++++++++++++++ .../mule4/agent/DistributedTracingTest.java | 13 ++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/mule4-agent/pom.xml b/mule4-agent/pom.xml index a4a13de..2b4e3a8 100644 --- a/mule4-agent/pom.xml +++ b/mule4-agent/pom.xml @@ -4,7 +4,7 @@ 4.0.0 co.elastic.apm mule4-agent - 0.2.0 + 0.3.0 Elastic Mule 4 APM agent Elastic Mule 4 APM agent jar diff --git a/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/span/SpanUtils.java b/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/span/SpanUtils.java index b8c4486..e6ae2cc 100644 --- a/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/span/SpanUtils.java +++ b/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/span/SpanUtils.java @@ -11,6 +11,7 @@ import co.elastic.apm.api.ElasticApm; import co.elastic.apm.api.Span; import co.elastic.apm.api.Transaction; +import co.elastic.apm.mule4.agent.tracing.HttpTracingUtils; import co.elastic.apm.mule4.agent.transaction.TransactionStore; /* @@ -38,6 +39,8 @@ public static void startSpan(TransactionStore transactionStore, MessageProcessor Span span = transaction.startSpan(getSpanType(notification), getSubType(notification), getAction(notification)); + checkAndPropagateParentTraceId(span, notification); + setSpanDetails(span, notification); String spanId = getSpanId(notification); @@ -45,6 +48,14 @@ public static void startSpan(TransactionStore transactionStore, MessageProcessor transactionStore.addSpan(transactionId, spanId, span); } + private static void checkAndPropagateParentTraceId(Span span, MessageProcessorNotification notification) { + + // Propagate trace.id through HTTP + if (HttpTracingUtils.isHttpRequester(notification)) + HttpTracingUtils.propagateTraceIdHeader(span, notification); + + } + public static String getSpanId(MessageProcessorNotification notification) { return notification.getInfo().getComponent().getLocation().getLocation(); } diff --git a/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/tracing/HttpTracingUtils.java b/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/tracing/HttpTracingUtils.java index 2a45865..4e93759 100644 --- a/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/tracing/HttpTracingUtils.java +++ b/mule4-agent/src/main/java/co/elastic/apm/mule4/agent/tracing/HttpTracingUtils.java @@ -1,12 +1,18 @@ package co.elastic.apm.mule4.agent.tracing; import org.mule.extension.http.api.HttpRequestAttributes; +import org.mule.runtime.api.component.ComponentIdentifier; import org.mule.runtime.api.metadata.TypedValue; +import org.mule.runtime.api.notification.MessageProcessorNotification; import org.mule.runtime.api.notification.PipelineMessageNotification; import org.mule.runtime.api.util.MultiMap; +import co.elastic.apm.api.Span; + public class HttpTracingUtils { + private static final String HTTP_REQUEST_NAMESPACE = "http://www.mulesoft.org/schema/mule/http"; + private static final String HTTP_REQUEST_NAME = "request"; public static final String ELASTIC_APM_TRACEPARENT = "elastic-apm-traceparent"; public static boolean isHttpEvent(PipelineMessageNotification notification) { @@ -36,4 +42,18 @@ private static TypedValue extractAttributes(PipelineMessageNotification return notification.getEvent().getMessage().getAttributes(); } + public static boolean isHttpRequester(MessageProcessorNotification notification) { + + ComponentIdentifier identifier = notification.getInfo().getComponent().getIdentifier(); + String name = identifier.getName(); + String namespace = identifier.getNamespaceUri(); + + return HTTP_REQUEST_NAME.equals(name) && HTTP_REQUEST_NAMESPACE.equals(namespace); + } + + public static void propagateTraceIdHeader(Span span, MessageProcessorNotification notification) { + // TODO Auto-generated method stub + + } + } diff --git a/mule4-agent/src/test/java/co/elastic/apm/mule4/agent/DistributedTracingTest.java b/mule4-agent/src/test/java/co/elastic/apm/mule4/agent/DistributedTracingTest.java index 09138e0..11d5d07 100644 --- a/mule4-agent/src/test/java/co/elastic/apm/mule4/agent/DistributedTracingTest.java +++ b/mule4-agent/src/test/java/co/elastic/apm/mule4/agent/DistributedTracingTest.java @@ -60,6 +60,19 @@ public void testNoTraceIdPropagation() throws Exception { } + @Test + public void testTraceIdPropagationThroughHttp() throws Exception { + + HttpGet getRequest = new HttpGet("http://localhost:8998"); + getRequest.addHeader(HttpTracingUtils.ELASTIC_APM_TRACEPARENT, TRACE_PARENT1); + + HttpClient httpClient = HttpClientBuilder.create().build(); + + HttpResponse response = httpClient.execute(getRequest); + + + } + @Override protected String getConfigFile() { return "DistributedTracingTest.xml";