/
spanbuilder.groovy
65 lines (56 loc) · 2.01 KB
/
spanbuilder.groovy
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
import com.launchdarkly.sdk.LDUser
import com.launchdarkly.sdk.server.LDClient
import com.tersesystems.conditionalspans.Main
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.context.Context
import java.time.LocalTime
import static io.opentelemetry.api.common.AttributeKey.booleanKey
boolean shouldStartSpan(String spanName, Context parent, HashMap<AttributeKey<?>, Object> attributes) {
return always()
//return byDebugAttribute(spanName, attributes);
//return byOddNanos(spanName);
//return byFeatureFlag(spanName);
//return byDeadline(spanName, LocalTime.now().plusMinutes(10));
}
boolean always() {
return true
}
boolean byDebugAttribute(String spanName, HashMap<AttributeKey<?>, Object> attributes) {
// println("byDebugAttribute: instance = $spanName, attributes = $attributes")
if (attributes.containsKey(booleanKey("DEBUG"))) {
println("byAttribute: ignoring debug span $spanName")
return false;
} else {
return true;
}
}
boolean byOddNanos(String spanName) {
//println("byOddNanos: spanName = $spanName")
if (System.nanoTime() % 3 == 0) {
println("byOddNanos: ignoring ${spanName}")
return false;
} else {
return true;
}
}
boolean byFeatureFlag(String spanName) {
//println("byFeatureFlag: spanName = $spanName")
Context context = Context.current();
LDClient ldClient = context.get(Main.LD_CLIENT_KEY);
LDUser user = context.get(Main.LD_USER_KEY);
if (ldClient.boolVariation("testflag", user, false)) {
return true;
} else {
println("byFeatureFlag: ignoring $spanName for $user")
return false;
}
}
boolean byDeadline(String spanName, LocalTime deadline) {
LocalTime currentTime = LocalTime.now();
if (currentTime.isAfter(deadline)) {
println("byDeadline: ignoring ${spanName} because $currentTime is after $deadline")
return false
} else {
return true
}
}