-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathBaseApplication.java
More file actions
108 lines (87 loc) · 2.92 KB
/
BaseApplication.java
File metadata and controls
108 lines (87 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package datadog.smoketest.loginjection;
import datadog.trace.api.CorrelationIdentifier;
import datadog.trace.api.GlobalTracer;
import datadog.trace.api.Trace;
import datadog.trace.api.TraceConfig;
import datadog.trace.api.Tracer;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
public abstract class BaseApplication {
public static final long TIMEOUT_IN_NANOS = TimeUnit.SECONDS.toNanos(30);
public abstract void doLog(String message);
public void run() throws InterruptedException {
doLog("BEFORE FIRST SPAN");
firstTracedMethod();
doLog("AFTER FIRST SPAN");
secondTracedMethod();
if (!waitForCondition(() -> !getLogInjectionEnabled())) {
throw new RuntimeException("Logs injection config was never updated");
}
thirdTracedMethod();
if (!waitForCondition(() -> getLogInjectionEnabled())) {
throw new RuntimeException("Logs injection config was never updated a second time");
}
forthTracedMethod();
// Logs for "AFTER SECOND SPAN" and "AFTER THIRD SPAN" don't exist to avoid the race between
// traces and logs. The tester waits for traces before changing config
doLog("AFTER FORTH SPAN");
// Sleep to allow the trace to be reported
Thread.sleep(400);
}
private static boolean getLogInjectionEnabled() {
try {
Tracer tracer = GlobalTracer.get();
Method captureTraceConfig = tracer.getClass().getMethod("captureTraceConfig");
return ((TraceConfig) captureTraceConfig.invoke(tracer)).isLogsInjectionEnabled();
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@Trace
public void firstTracedMethod() {
doLog("INSIDE FIRST SPAN");
System.out.println(
"FIRSTTRACEID "
+ CorrelationIdentifier.getTraceId()
+ " "
+ CorrelationIdentifier.getSpanId());
}
@Trace
public void secondTracedMethod() {
doLog("INSIDE SECOND SPAN");
System.out.println(
"SECONDTRACEID "
+ CorrelationIdentifier.getTraceId()
+ " "
+ CorrelationIdentifier.getSpanId());
}
@Trace
public void thirdTracedMethod() {
doLog("INSIDE THIRD SPAN");
System.out.println(
"THIRDTRACEID "
+ CorrelationIdentifier.getTraceId()
+ " "
+ CorrelationIdentifier.getSpanId());
}
@Trace
public void forthTracedMethod() {
doLog("INSIDE FORTH SPAN");
System.out.println(
"FORTHTRACEID "
+ CorrelationIdentifier.getTraceId()
+ " "
+ CorrelationIdentifier.getSpanId());
}
private static boolean waitForCondition(Supplier<Boolean> condition) throws InterruptedException {
long startTime = System.nanoTime();
while (System.nanoTime() - startTime < TIMEOUT_IN_NANOS) {
if (condition.get()) {
return true;
}
Thread.sleep(100);
}
return false;
}
}