Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public String apply(String key) {

@Override
public void forEachKey(Message<?> carrier, AgentPropagation.KeyClassifier classifier) {
if (carrier == null) {
return;
}
final MessageHeaders messageHeaders = carrier.getHeaders();
if (messageHeaders == null || messageHeaders.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static class ContextPropagationAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) Message<?> message, @Advice.Local("ctxScope") ContextScope scope) {
if (activeSpan() == null) {
if (message != null && activeSpan() == null) {
// no local active span, so extract from message to avoid disconnected trace
scope = defaultPropagator().extract(rootContext(), message, GETTER).attach();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package datadog.trace.instrumentation.springmessaging;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;

import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.messaging.support.GenericMessage;

class SpringMessageExtractAdapterTest {

@Test
void ignoresNullMessage() {
List<String> seen = new ArrayList<>();

SpringMessageExtractAdapter.GETTER.forEachKey(null, collectInto(seen));

assertEquals(emptyList(), seen);
}

@Test
void ignoresMessageWithoutHeaders() {
List<String> seen = new ArrayList<>();

SpringMessageExtractAdapter.GETTER.forEachKey(
new GenericMessage<>("payload", new HashMap<String, Object>()), collectInto(seen));

assertEquals(emptyList(), seen);
}

@Test
void normalizesKeysAndSkipsNonStringHeaders() {
Map<String, Object> headers = new HashMap<>();
headers.put("X-Datadog-Trace-Id", "123");
headers.put("AWSTraceHeader", "Root=1-abc");
headers.put("not-a-string", 42);
List<String> seen = new ArrayList<>();

SpringMessageExtractAdapter.GETTER.forEachKey(
new GenericMessage<>("payload", headers), collectInto(seen));

// header order is not guaranteed, so compare as sorted entries
seen.sort(String::compareTo);
assertEquals(asList("x-amzn-trace-id=Root=1-abc", "x-datadog-trace-id=123"), seen);
}

private static AgentPropagation.KeyClassifier collectInto(List<String> seen) {
return (key, value) -> {
seen.add(key + "=" + value);
return true;
};
}
}
Loading