This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from michaelhyatt/distributed-http-tracing-2.0
Distributed http tracing 2.0
- Loading branch information
Showing
14 changed files
with
281 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
mule4-agent/src/main/java/co/elastic/apm/mule4/agent/tracing/HttpTracingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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) { | ||
return extractAttributes(notification).getDataType().getType() == HttpRequestAttributes.class; | ||
} | ||
|
||
public static boolean hasRemoteParent(PipelineMessageNotification notification) { | ||
|
||
if (!isHttpEvent(notification)) | ||
return false; | ||
|
||
return getHttpAttributes(notification).containsKey(ELASTIC_APM_TRACEPARENT); | ||
} | ||
|
||
public static String getTracingHeaderValue(String x, PipelineMessageNotification notification) { | ||
return getHttpAttributes(notification).get(ELASTIC_APM_TRACEPARENT); | ||
} | ||
|
||
private static MultiMap<String, String> getHttpAttributes(PipelineMessageNotification notification) { | ||
|
||
HttpRequestAttributes attributes = (HttpRequestAttributes) extractAttributes(notification).getValue(); | ||
|
||
return attributes.getHeaders(); | ||
} | ||
|
||
private static TypedValue<Object> extractAttributes(PipelineMessageNotification notification) { | ||
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 | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
mule4-agent/src/test/java/co/elastic/apm/mule4/agent/DistributedTracingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package co.elastic.apm.mule4.agent; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.util.EntityUtils; | ||
import org.junit.Test; | ||
|
||
import co.elastic.apm.agent.impl.transaction.Transaction; | ||
import co.elastic.apm.mule4.agent.config.BaseAbstractApmMuleTestCase; | ||
import co.elastic.apm.mule4.agent.tracing.HttpTracingUtils; | ||
|
||
public class DistributedTracingTest extends BaseAbstractApmMuleTestCase { | ||
|
||
private static final String TRACE_ID1 = "0af7651916cd43dd8448eb211c80319c"; | ||
private static final String TRACE_PARENT1 = "00-" + TRACE_ID1 + "-b9c7c989f97918e1-01"; | ||
|
||
@Test | ||
public void testTraceIdPropagation() throws Exception { | ||
|
||
HttpGet getRequest = new HttpGet("http://localhost:8998/request"); | ||
getRequest.addHeader(HttpTracingUtils.ELASTIC_APM_TRACEPARENT, TRACE_PARENT1); | ||
|
||
HttpClient httpClient = HttpClientBuilder.create().build(); | ||
|
||
HttpResponse response = httpClient.execute(getRequest); | ||
|
||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
assertEquals(TRACE_PARENT1, EntityUtils.toString(response.getEntity())); | ||
List<Transaction> transactions = getTransactions(); | ||
assertEquals(1, transactions.size()); | ||
assertEquals(1, getSpans().size()); | ||
assertEquals(TRACE_ID1, getTransaction().getTraceContext().getTraceId().toString()); | ||
|
||
} | ||
|
||
@Test | ||
public void testNoTraceIdPropagation() throws Exception { | ||
|
||
HttpGet getRequest = new HttpGet("http://localhost:8998/request"); | ||
|
||
HttpClient httpClient = HttpClientBuilder.create().build(); | ||
|
||
HttpResponse response = httpClient.execute(getRequest); | ||
|
||
Thread.sleep(1000); | ||
|
||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
assertEquals("", EntityUtils.toString(response.getEntity())); | ||
List<Transaction> transactions = getTransactions(); | ||
assertEquals(1, transactions.size()); | ||
assertEquals(1, getSpans().size()); | ||
assertNotEquals(TRACE_ID1, getTransaction().getTraceContext().getTraceId().toString()); | ||
assertNotEquals("", getTransaction().getTraceContext().getTraceId().toString()); | ||
|
||
} | ||
|
||
@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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.