Skip to content

Commit 7426624

Browse files
authored
Migrate dd-trace-core groovy files to java part 2 (#11062)
* Migrate dd-trace-core groovy files to java part 2 migrate DDSpecification and DDCoreSpecification that are used by most of the test in this module. we are keeping the groovy version to be able to incrementally migrate tests. a first small test (DDSpanLinkTest )is migrated to prove the viability of the strategy. * spotbugs * address comments * revisit the life cycle remove PER_CLASS mode setupSpec -> beforeAll
1 parent a31f629 commit 7426624

7 files changed

Lines changed: 805 additions & 198 deletions

File tree

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

Lines changed: 0 additions & 197 deletions
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package datadog.trace.core;
2+
3+
import datadog.metrics.api.statsd.StatsDClient;
4+
import datadog.trace.api.DDSpanId;
5+
import datadog.trace.api.DDTraceId;
6+
import datadog.trace.api.datastreams.NoopPathwayContext;
7+
import datadog.trace.api.sampling.PrioritySampling;
8+
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
9+
import datadog.trace.common.writer.ListWriter;
10+
import datadog.trace.core.CoreTracer.CoreTracerBuilder;
11+
import datadog.trace.core.propagation.PropagationTags;
12+
import datadog.trace.core.tagprocessor.TagsPostProcessorFactory;
13+
import datadog.trace.test.util.DDJavaSpecification;
14+
import datadog.trace.util.AgentTaskScheduler;
15+
import java.util.ArrayList;
16+
import java.util.Collections;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.concurrent.TimeUnit;
21+
import org.junit.jupiter.api.AfterAll;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeAll;
24+
25+
public abstract class DDCoreJavaSpecification extends DDJavaSpecification {
26+
27+
protected static List<CoreTracer> unclosedTracers = new ArrayList<>();
28+
29+
protected static class AutoCloseableCoreTracerBuilder extends CoreTracerBuilder {
30+
@Override
31+
public CoreTracer build() {
32+
CoreTracer tracer = super.build();
33+
unclosedTracers.add(tracer);
34+
return tracer;
35+
}
36+
}
37+
38+
protected boolean useNoopStatsDClient() {
39+
return true;
40+
}
41+
42+
protected boolean useStrictTraceWrites() {
43+
return true;
44+
}
45+
46+
@BeforeAll
47+
static void beforeAll() {
48+
TagsPostProcessorFactory.withAddInternalTags(false);
49+
TagsPostProcessorFactory.withAddRemoteHostname(false);
50+
}
51+
52+
@AfterAll
53+
static void afterAll() {
54+
TagsPostProcessorFactory.reset();
55+
}
56+
57+
@AfterEach
58+
void cleanupCore() {
59+
for (CoreTracer tracer : unclosedTracers) {
60+
try {
61+
tracer.close();
62+
} catch (Throwable ignored) {
63+
}
64+
}
65+
unclosedTracers.clear();
66+
AgentTaskScheduler.shutdownAndReset(10, TimeUnit.SECONDS);
67+
}
68+
69+
protected CoreTracerBuilder tracerBuilder() {
70+
CoreTracerBuilder builder = new AutoCloseableCoreTracerBuilder();
71+
if (useNoopStatsDClient()) {
72+
builder = builder.statsDClient(StatsDClient.NO_OP);
73+
}
74+
return builder.strictTraceWrites(useStrictTraceWrites());
75+
}
76+
77+
protected DDSpan buildSpan(long timestamp, CharSequence spanType, Map<String, Object> tags) {
78+
return buildSpan(
79+
timestamp,
80+
spanType,
81+
PropagationTags.factory().empty(),
82+
tags,
83+
PrioritySampling.SAMPLER_KEEP,
84+
null);
85+
}
86+
87+
protected DDSpan buildSpan(
88+
long timestamp, String tag, String value, PropagationTags propagationTags) {
89+
Map<String, Object> tags = new HashMap<>();
90+
tags.put(tag, value);
91+
return buildSpan(timestamp, "fakeType", propagationTags, tags, PrioritySampling.UNSET, null);
92+
}
93+
94+
protected DDSpan buildSpan(
95+
long timestamp,
96+
CharSequence spanType,
97+
PropagationTags propagationTags,
98+
Map<String, Object> tags,
99+
byte prioritySampling,
100+
Object ciVisibilityContextData) {
101+
CoreTracer tracer = tracerBuilder().writer(new ListWriter()).build();
102+
DDSpanContext context =
103+
new DDSpanContext(
104+
DDTraceId.ONE,
105+
1L,
106+
DDSpanId.ZERO,
107+
null,
108+
null,
109+
"fakeService",
110+
"fakeOperation",
111+
"fakeResource",
112+
prioritySampling,
113+
null,
114+
Collections.emptyMap(),
115+
null,
116+
false,
117+
spanType,
118+
0,
119+
tracer.createTraceCollector(DDTraceId.ONE),
120+
null,
121+
null,
122+
ciVisibilityContextData,
123+
NoopPathwayContext.INSTANCE,
124+
false,
125+
propagationTags,
126+
ProfilingContextIntegration.NoOp.INSTANCE,
127+
true);
128+
129+
DDSpan span = DDSpan.create("test", timestamp, context, null);
130+
for (Map.Entry<String, Object> entry : tags.entrySet()) {
131+
span.setTag(entry.getKey(), entry.getValue());
132+
}
133+
134+
tracer.close();
135+
return span;
136+
}
137+
}

0 commit comments

Comments
 (0)