44import static datadog .trace .bootstrap .instrumentation .api .AgentSpanLink .DEFAULT_FLAGS ;
55import static datadog .trace .bootstrap .instrumentation .api .AgentSpanLink .SAMPLED_FLAG ;
66import static datadog .trace .bootstrap .instrumentation .api .SpanAttributes .EMPTY ;
7+ import static datadog .trace .core .propagation .HttpCodecTestHelper .TRACE_PARENT_KEY ;
8+ import static datadog .trace .core .propagation .HttpCodecTestHelper .TRACE_STATE_KEY ;
79import static org .junit .jupiter .api .Assertions .assertEquals ;
810import static org .junit .jupiter .api .Assertions .assertNull ;
911import static org .junit .jupiter .api .Assertions .assertTrue ;
2224import datadog .trace .core .propagation .ExtractedContext ;
2325import datadog .trace .core .propagation .HttpCodec ;
2426import datadog .trace .core .propagation .HttpCodecTestHelper ;
27+ import java .io .IOException ;
28+ import java .util .ArrayList ;
2529import java .util .HashMap ;
2630import java .util .List ;
2731import java .util .Map ;
2832import java .util .stream .Collectors ;
2933import java .util .stream .IntStream ;
3034import org .junit .jupiter .api .AfterEach ;
35+ import org .junit .jupiter .api .BeforeEach ;
3136import org .junit .jupiter .api .Test ;
3237import org .junit .jupiter .params .ParameterizedTest ;
3338import org .tabletest .junit .TableTest ;
@@ -36,12 +41,15 @@ class DDSpanLinkTest extends DDCoreJavaSpecification {
3641
3742 private static final int SPAN_LINK_TAG_MAX_LENGTH = 25_000 ;
3843 private static final ObjectMapper JSON_MAPPER = new ObjectMapper ();
39- // W3C Trace Context standard header names (W3CHttpCodec is package-private)
40- private static final String TRACE_PARENT_KEY = "traceparent" ;
41- private static final String TRACE_STATE_KEY = "tracestate" ;
4244
43- private ListWriter writer = new ListWriter ();
44- private CoreTracer tracer = tracerBuilder ().writer (writer ).build ();
45+ private ListWriter writer ;
46+ private CoreTracer tracer ;
47+
48+ @ BeforeEach
49+ void setup () {
50+ writer = new ListWriter ();
51+ tracer = tracerBuilder ().writer (writer ).build ();
52+ }
4553
4654 @ AfterEach
4755 void cleanupTest () {
@@ -53,7 +61,6 @@ void cleanupTest() {
5361 "sampled | true | '01' | '1' " ,
5462 "not sampled | false | '00' | '-1' "
5563 })
56- @ ParameterizedTest (name = "create span link from extracted context [{index}]" )
5764 void createSpanLinkFromExtractedContext (boolean sampled , String traceFlags , String sample ) {
5865 String traceId = "11223344556677889900aabbccddeeff" ;
5966 String spanId = "123456789abcdef0" ;
@@ -91,12 +98,7 @@ void testSpanLinkEncodingTagMaxSize() throws Exception {
9198 span .finish ();
9299 writer .waitForTraces (1 );
93100 String spanLinksTag = (String ) writer .get (0 ).get (0 ).getTag (SPAN_LINKS );
94- List <TestSpanLinkJson > decodedSpanLinks =
95- JSON_MAPPER .readValue (
96- spanLinksTag ,
97- JSON_MAPPER
98- .getTypeFactory ()
99- .constructCollectionType (List .class , TestSpanLinkJson .class ));
101+ List <SpanLinkAsTag > decodedSpanLinks = deserializeSpanLinks (spanLinksTag );
100102
101103 assertTrue (spanLinksTag .length () < SPAN_LINK_TAG_MAX_LENGTH );
102104 assertTrue (decodedSpanLinks .size () < tooManyLinkCount );
@@ -139,7 +141,7 @@ void testSpanLinksEncodingOmittedEmptyKeys() throws Exception {
139141 @ ParameterizedTest (name = "add span link at any time [{index}]" )
140142 void addSpanLinkAtAnyTime (boolean beforeStart , boolean afterStart ) throws Exception {
141143 AgentTracer .SpanBuilder builder = tracer .buildSpan ("test" , "operation" );
142- List <SpanLink > links = new java . util . ArrayList <>();
144+ List <SpanLink > links = new ArrayList <>();
143145
144146 if (beforeStart ) {
145147 SpanLink link = createLink (0 );
@@ -155,16 +157,13 @@ void addSpanLinkAtAnyTime(boolean beforeStart, boolean afterStart) throws Except
155157 span .finish ();
156158 writer .waitForTraces (1 );
157159 String spanLinksTag = (String ) writer .get (0 ).get (0 ).getTag (SPAN_LINKS );
158- List <TestSpanLinkJson > decodedSpanLinks =
160+ List <SpanLinkAsTag > decodedSpanLinks =
159161 spanLinksTag == null
160162 ? java .util .Collections .emptyList ()
161- : JSON_MAPPER .readValue (
162- spanLinksTag ,
163- JSON_MAPPER
164- .getTypeFactory ()
165- .constructCollectionType (List .class , TestSpanLinkJson .class ));
163+ : deserializeSpanLinks (spanLinksTag );
166164
167- assertEquals ((beforeStart ? 1 : 0 ) + (afterStart ? 1 : 0 ), decodedSpanLinks .size ());
165+ int expectedLinkCount = (beforeStart ? 1 : 0 ) + (afterStart ? 1 : 0 );
166+ assertEquals (expectedLinkCount , decodedSpanLinks .size ());
168167 for (int i = 0 ; i < decodedSpanLinks .size (); i ++) {
169168 assertLink (links .get (i ), decodedSpanLinks .get (i ));
170169 }
@@ -195,7 +194,7 @@ private SpanLink createLink(int index) {
195194 SpanAttributes .fromMap (attributes ));
196195 }
197196
198- private void assertLink (SpanLink expected , TestSpanLinkJson actual ) {
197+ private void assertLink (SpanLink expected , SpanLinkAsTag actual ) {
199198 assertEquals (expected .traceId ().toHexString (), actual .trace_id );
200199 assertEquals (DDSpanId .toHexString (expected .spanId ()), actual .span_id );
201200 if (expected .traceFlags () == DEFAULT_FLAGS ) {
@@ -215,7 +214,13 @@ private void assertLink(SpanLink expected, TestSpanLinkJson actual) {
215214 }
216215 }
217216
218- static class TestSpanLinkJson {
217+ static List <SpanLinkAsTag > deserializeSpanLinks (String json ) throws IOException {
218+ return JSON_MAPPER .readValue (
219+ json ,
220+ JSON_MAPPER .getTypeFactory ().constructCollectionType (List .class , SpanLinkAsTag .class ));
221+ }
222+
223+ static class SpanLinkAsTag {
219224 public String trace_id ;
220225 public String span_id ;
221226 public Byte flags ;
0 commit comments