-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathJavaGatewayInterfaceInstrumentationTest.groovy
More file actions
122 lines (112 loc) · 3.67 KB
/
JavaGatewayInterfaceInstrumentationTest.groovy
File metadata and controls
122 lines (112 loc) · 3.67 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import com.ibm.connector2.cics.ECIInteraction
import com.ibm.ctg.client.JavaGateway
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.api.DDSpanTypes
import datadog.trace.bootstrap.CallDepthThreadLocalMap
import datadog.trace.bootstrap.instrumentation.api.Tags
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
class JavaGatewayInterfaceInstrumentationTest extends InstrumentationSpecification {
ServerSocket serverSocket
Thread serverThread
int port
def setup() {
// Start a server that accepts and immediately closes connections
serverSocket = new ServerSocket(0)
port = serverSocket.getLocalPort()
serverThread = new Thread({
try {
while (!serverSocket.isClosed()) {
Socket clientSocket = serverSocket.accept()
clientSocket.close()
}
} catch (IOException ignored) {
// expected when server socket closes
}
})
serverThread.start()
// Wait for server to be ready to accept connections
// Try to connect to ensure the server thread has entered accept()
int maxAttempts = 50
int attemptDelayMs = 10
for (int i = 0; i < maxAttempts; i++) {
try {
Socket testSocket = new Socket()
testSocket.connect(new InetSocketAddress("127.0.0.1", port), 100)
testSocket.close()
break // Successfully connected, server is ready
} catch (IOException e) {
if (i == maxAttempts - 1) {
throw new RuntimeException("Server failed to start accepting connections after ${maxAttempts * attemptDelayMs}ms", e)
}
Thread.sleep(attemptDelayMs)
}
}
}
def cleanup() {
serverSocket?.close()
serverThread?.join(1000)
}
def "flow without parent creates new span"() {
when:
try {
new JavaGateway("127.0.0.1", port) // use IPv4 address so we can make sure peer.ipv4 is in the tags
} catch (IOException ignored) {
// expected - connection will be closed by server
}
then:
TEST_WRITER.waitForTraces(1)
assertTraces(1) {
trace(1) {
span(0) {
operationName "gateway.flow"
spanType DDSpanTypes.RPC
errored true
tags {
"$Tags.COMPONENT" "cics-client"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
"rpc.system" "cics"
"$Tags.PEER_HOSTNAME" "127.0.0.1"
"$Tags.PEER_PORT" port
"$Tags.PEER_HOST_IPV4" "127.0.0.1"
errorTags IOException, String
defaultTags()
}
}
}
}
}
def "flow with parent span merges into parent"() {
when:
try {
runUnderTrace("parent") {
// Simulate being inside ECIInteraction.execute() (cics.execute operation)
CallDepthThreadLocalMap.incrementCallDepth(ECIInteraction)
try {
new JavaGateway("127.0.0.1", port) // use IPv4 address so we can make sure peer.ipv4 is in the tags
} finally {
CallDepthThreadLocalMap.decrementCallDepth(ECIInteraction)
}
}
} catch (IOException ignored) {
// expected - connection will be closed by server
}
then:
assertTraces(1) {
trace(1) {
span(0) {
operationName "parent"
errored true
tags {
// Component and rpc.system are NOT set because we didn't create a new span
// We only added connection tags to the existing parent span
"$Tags.PEER_HOSTNAME" { it == null || it == "127.0.0.1" }
"$Tags.PEER_PORT" port
"$Tags.PEER_HOST_IPV4" "127.0.0.1"
errorTags IOException, String
defaultTags()
}
}
}
}
}
}