Skip to content

Commit 9e0bebb

Browse files
committed
address comments
1 parent 322cdd1 commit 9e0bebb

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

dd-trace-core/src/test/java/datadog/trace/core/DDSpanLinkTest.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static datadog.trace.bootstrap.instrumentation.api.AgentSpanLink.DEFAULT_FLAGS;
55
import static datadog.trace.bootstrap.instrumentation.api.AgentSpanLink.SAMPLED_FLAG;
66
import 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;
79
import static org.junit.jupiter.api.Assertions.assertEquals;
810
import static org.junit.jupiter.api.Assertions.assertNull;
911
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -22,12 +24,15 @@
2224
import datadog.trace.core.propagation.ExtractedContext;
2325
import datadog.trace.core.propagation.HttpCodec;
2426
import datadog.trace.core.propagation.HttpCodecTestHelper;
27+
import java.io.IOException;
28+
import java.util.ArrayList;
2529
import java.util.HashMap;
2630
import java.util.List;
2731
import java.util.Map;
2832
import java.util.stream.Collectors;
2933
import java.util.stream.IntStream;
3034
import org.junit.jupiter.api.AfterEach;
35+
import org.junit.jupiter.api.BeforeEach;
3136
import org.junit.jupiter.api.Test;
3237
import org.junit.jupiter.params.ParameterizedTest;
3338
import 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;

dd-trace-core/src/test/java/datadog/trace/core/propagation/HttpCodecTestHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
/** Helper class used only for tests to bridge package-private classes */
88
public class HttpCodecTestHelper {
9+
// W3C Trace Context standard header names (W3CHttpCodec is package-private)
10+
public static final String TRACE_PARENT_KEY = W3CHttpCodec.TRACE_PARENT_KEY;
11+
public static final String TRACE_STATE_KEY = W3CHttpCodec.TRACE_STATE_KEY;
12+
913
public static HttpCodec.Extractor W3CHttpCodecNewExtractor(
1014
Config config, Supplier<TraceConfig> traceConfigSupplier) {
1115
return W3CHttpCodec.newExtractor(config, traceConfigSupplier);

utils/test-utils/src/main/java/datadog/trace/test/util/DDJavaSpecification.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ public class DDJavaSpecification {
5151
private static Field configInstanceField;
5252
private static Constructor<?> configConstructor;
5353

54-
static {
55-
allowContextTesting();
56-
installConfigTransformer();
57-
makeConfigInstanceModifiable();
58-
}
59-
6054
private static Boolean contextTestingAllowed;
6155
private static volatile boolean isConfigInstanceModifiable = false;
6256
static volatile boolean configModificationFailed = false;
@@ -69,6 +63,13 @@ public class DDJavaSpecification {
6963
protected boolean assertThreadsEachCleanup = true;
7064
private volatile boolean ignoreThreadCleanup;
7165

66+
@BeforeAll
67+
static void beforeAll() {
68+
allowContextTesting();
69+
installConfigTransformer();
70+
makeConfigInstanceModifiable();
71+
}
72+
7273
static void allowContextTesting() {
7374
if (contextTestingAllowed == null) {
7475
try {

0 commit comments

Comments
 (0)