/
SkywalkingSpanActivationTest.java
292 lines (246 loc) · 12.2 KB
/
SkywalkingSpanActivationTest.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package org.skywalking.apm.toolkit.activation.opentracing;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;
import java.util.HashMap;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.skywalking.apm.agent.core.context.ContextCarrier;
import org.skywalking.apm.agent.core.context.ContextSnapshot;
import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
import org.skywalking.apm.agent.core.context.trace.TraceSegmentRef;
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.skywalking.apm.agent.test.helper.SegmentHelper;
import org.skywalking.apm.agent.test.tools.AgentServiceRule;
import org.skywalking.apm.agent.test.tools.SegmentStorage;
import org.skywalking.apm.agent.test.tools.SegmentStoragePoint;
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner;
import org.skywalking.apm.toolkit.activation.opentracing.continuation.ActivateInterceptor;
import org.skywalking.apm.toolkit.activation.opentracing.continuation.ConstructorInterceptor;
import org.skywalking.apm.toolkit.activation.opentracing.span.ConstructorWithSpanBuilderInterceptor;
import org.skywalking.apm.toolkit.activation.opentracing.span.SpanFinishInterceptor;
import org.skywalking.apm.toolkit.activation.opentracing.span.SpanLogInterceptor;
import org.skywalking.apm.toolkit.activation.opentracing.span.SpanSetOperationNameInterceptor;
import org.skywalking.apm.toolkit.activation.opentracing.tracer.SkywalkingTracerExtractInterceptor;
import org.skywalking.apm.toolkit.activation.opentracing.tracer.SkywalkingTracerInjectInterceptor;
import org.skywalking.apm.toolkit.opentracing.SkywalkingSpanBuilder;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.skywalking.apm.agent.test.tools.SegmentRefAssert.assertPeerHost;
import static org.skywalking.apm.agent.test.tools.SegmentRefAssert.assertSegmentId;
import static org.skywalking.apm.agent.test.tools.SegmentRefAssert.assertSpanId;
import static org.skywalking.apm.agent.test.tools.SpanAssert.assertComponent;
import static org.skywalking.apm.agent.test.tools.SpanAssert.assertLogSize;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(TracingSegmentRunner.class)
public class SkywalkingSpanActivationTest {
@SegmentStoragePoint
private SegmentStorage storage;
@Rule
public AgentServiceRule serviceRule = new AgentServiceRule();
private MockEnhancedInstance enhancedInstance = new MockEnhancedInstance();
private ConstructorWithSpanBuilderInterceptor constructorWithSpanBuilderInterceptor;
private Tracer.SpanBuilder spanBuilder = new SkywalkingSpanBuilder("test");
private SpanLogInterceptor spanLogInterceptor;
private Object[] logArgument;
private HashMap<String, Object> event = new HashMap<String, Object>() {
{
put("a", "A");
}
};
private Class[] logArgumentType;
private SpanSetOperationNameInterceptor setOperationNameInterceptor;
private Object[] setOperationNameArgument;
private Class[] setOperationNameArgumentType;
private SpanFinishInterceptor spanFinishInterceptor;
private SkywalkingTracerInjectInterceptor injectInterceptor;
private SkywalkingTracerExtractInterceptor extractInterceptor;
private ConstructorInterceptor constructorInterceptor;
private ActivateInterceptor activateInterceptor;
@Before
public void setUp() {
spanBuilder = new SkywalkingSpanBuilder("test").withTag(Tags.COMPONENT.getKey(), "test");
constructorWithSpanBuilderInterceptor = new ConstructorWithSpanBuilderInterceptor();
spanLogInterceptor = new SpanLogInterceptor();
logArgument = new Object[] {111111111L, event};
logArgumentType = new Class[] {long.class, HashMap.class};
setOperationNameInterceptor = new SpanSetOperationNameInterceptor();
setOperationNameArgument = new Object[] {"testOperationName"};
setOperationNameArgumentType = new Class[] {String.class};
spanFinishInterceptor = new SpanFinishInterceptor();
injectInterceptor = new SkywalkingTracerInjectInterceptor();
extractInterceptor = new SkywalkingTracerExtractInterceptor();
constructorInterceptor = new ConstructorInterceptor();
activateInterceptor = new ActivateInterceptor();
}
@Test
public void testCreateLocalSpan() throws Throwable {
startSpan();
stopSpan();
TraceSegment tracingSegment = assertTraceSemgnets();
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(tracingSegment);
assertThat(spans.size(), is(1));
assertThat(spans.get(0).isEntry(), is(false));
assertThat(spans.get(0).isExit(), is(false));
}
@Test
public void testCreateEntrySpan() throws Throwable {
spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);
startSpan();
stopSpan();
TraceSegment tracingSegment = assertTraceSemgnets();
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(tracingSegment);
assertThat(spans.size(), is(1));
assertSpanCommonsAttribute(spans.get(0));
assertThat(spans.get(0).isEntry(), is(true));
}
@Test
public void testCreateExitSpanWithoutPeer() throws Throwable {
spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
startSpan();
stopSpan();
TraceSegment tracingSegment = assertTraceSemgnets();
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(tracingSegment);
assertThat(spans.size(), is(1));
assertSpanCommonsAttribute(spans.get(0));
assertThat(spans.get(0).isEntry(), is(false));
assertThat(spans.get(0).isExit(), is(false));
}
@Test
public void testCreateExitSpanWithPeer() throws Throwable {
spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.withTag(Tags.PEER_HOST_IPV4.getKey(), "127.0.0.1").withTag(Tags.PEER_PORT.getKey(), "8080");
startSpan();
stopSpan();
TraceSegment tracingSegment = assertTraceSemgnets();
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(tracingSegment);
assertThat(spans.size(), is(1));
assertSpanCommonsAttribute(spans.get(0));
assertThat(spans.get(0).isEntry(), is(false));
assertThat(spans.get(0).isExit(), is(true));
}
private TraceSegment assertTraceSemgnets() {
List<TraceSegment> segments = storage.getTraceSegments();
assertThat(segments.size(), is(1));
return segments.get(0);
}
@Test
public void testInject() throws Throwable {
spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.withTag(Tags.PEER_HOST_IPV4.getKey(), "127.0.0.1").withTag(Tags.PEER_PORT.getKey(), 8080);
startSpan();
String extractValue = (String)injectInterceptor.afterMethod(enhancedInstance, "extract",
null, null, null);
ContextCarrier contextCarrier = new ContextCarrier().deserialize(extractValue);
assertTrue(contextCarrier.isValid());
assertThat(contextCarrier.getPeerHost(), is("#127.0.0.1:8080"));
assertThat(contextCarrier.getSpanId(), is(0));
assertThat(contextCarrier.getEntryOperationName(), is("#testOperationName"));
stopSpan();
}
@Test
public void testExtractWithValidateContext() throws Throwable {
spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.withTag(Tags.PEER_HOST_IPV4.getKey(), "127.0.0.1").withTag(Tags.PEER_PORT.getKey(), 8080);
startSpan();
extractInterceptor.afterMethod(enhancedInstance, "extract",
new Object[] {"#AQA*#AQA*4WcWe0tQNQA*|3|1|#127.0.0.1:8080|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*"}, new Class[] {String.class}, null);
stopSpan();
TraceSegment tracingSegment = assertTraceSemgnets();
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(tracingSegment);
assertThat(tracingSegment.getRefs().size(), is(1));
TraceSegmentRef ref = tracingSegment.getRefs().get(0);
assertSegmentId(ref, "1.1.15006458883500001");
assertSpanId(ref, 3);
assertPeerHost(ref, "127.0.0.1:8080");
assertThat(spans.size(), is(1));
assertSpanCommonsAttribute(spans.get(0));
}
@Test
public void testExtractWithInValidateContext() throws Throwable {
spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.withTag(Tags.PEER_HOST_IPV4.getKey(), "127.0.0.1").withTag(Tags.PEER_PORT.getKey(), 8080);
startSpan();
extractInterceptor.afterMethod(enhancedInstance, "extract",
new Object[] {"#AQA*#AQA*4WcWe0tQNQA*|3|#192.168.1.8:18002|#/portal/|#/testEntrySpan|#AQA*#AQA*Et0We0tQNQA*"}, new Class[] {String.class}, null);
stopSpan();
TraceSegment tracingSegment = assertTraceSemgnets();
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(tracingSegment);
assertNull(tracingSegment.getRefs());
assertSpanCommonsAttribute(spans.get(0));
}
@Test
public void testContinuation() throws Throwable {
startSpan();
final MockEnhancedInstance continuationHolder = new MockEnhancedInstance();
constructorInterceptor.onConstruct(continuationHolder, null);
assertTrue(continuationHolder.getSkyWalkingDynamicField() instanceof ContextSnapshot);
new Thread() {
@Override public void run() {
MockEnhancedInstance enhancedInstance = new MockEnhancedInstance();
try {
startSpan(enhancedInstance);
activateInterceptor.afterMethod(continuationHolder, "activate", null, null, null);
} catch (Throwable throwable) {
throwable.printStackTrace();
} finally {
try {
stopSpan(enhancedInstance);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}.start();
Thread.sleep(1000L);
stopSpan();
List<TraceSegment> segments = storage.getTraceSegments();
assertThat(segments.size(), is(2));
TraceSegment traceSegment = segments.get(0);
assertThat(traceSegment.getRefs().size(), is(1));
traceSegment = segments.get(1);
assertNull(traceSegment.getRefs());
}
private void assertSpanCommonsAttribute(AbstractTracingSpan span) {
assertThat(span.getOperationName(), is("testOperationName"));
assertComponent(span, "test");
assertLogSize(span, 1);
}
private void stopSpan() throws Throwable {
stopSpan(enhancedInstance);
}
private void stopSpan(EnhancedInstance enhancedInstance) throws Throwable {
spanFinishInterceptor.afterMethod(enhancedInstance, "finish", null, null, null);
}
private void startSpan() throws Throwable {
startSpan(enhancedInstance);
}
private void startSpan(MockEnhancedInstance enhancedInstance) throws Throwable {
constructorWithSpanBuilderInterceptor.onConstruct(enhancedInstance, new Object[] {spanBuilder});
spanLogInterceptor.afterMethod(enhancedInstance, "log", logArgument, logArgumentType, null);
setOperationNameInterceptor.afterMethod(enhancedInstance, "setOperationName",
setOperationNameArgument, setOperationNameArgumentType, null);
}
private class MockEnhancedInstance implements EnhancedInstance {
public Object object;
@Override public Object getSkyWalkingDynamicField() {
return object;
}
@Override public void setSkyWalkingDynamicField(Object value) {
this.object = value;
}
}
private class MockContinuationThread extends Thread {
@Override
public void run() {
super.run();
}
}
}