-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
PluginTestAgent.java
806 lines (647 loc) · 29.1 KB
/
PluginTestAgent.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
* Copyright 2014 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.test;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import com.navercorp.pinpoint.common.util.AnnotationKeyUtils;
import com.navercorp.pinpoint.profiler.util.ApiUtils;
import org.apache.thrift.TBase;
import com.google.common.base.Objects;
import com.navercorp.pinpoint.bootstrap.AgentOption;
import com.navercorp.pinpoint.bootstrap.context.ServerMetaDataHolder;
import com.navercorp.pinpoint.bootstrap.context.ServiceInfo;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod;
import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations;
import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedAnnotation;
import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedSql;
import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedTrace;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.bootstrap.plugin.test.TraceType;
import com.navercorp.pinpoint.common.service.AnnotationKeyRegistryService;
import com.navercorp.pinpoint.common.trace.AnnotationKey;
import com.navercorp.pinpoint.common.trace.LoggingInfo;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.profiler.DefaultAgent;
import com.navercorp.pinpoint.profiler.context.Span;
import com.navercorp.pinpoint.profiler.context.SpanEvent;
import com.navercorp.pinpoint.profiler.context.storage.StorageFactory;
import com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.receiver.CommandDispatcher;
import com.navercorp.pinpoint.profiler.sender.DataSender;
import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender;
import com.navercorp.pinpoint.profiler.util.JavaAssistUtils;
import com.navercorp.pinpoint.profiler.util.RuntimeMXBeanUtils;
import com.navercorp.pinpoint.thrift.dto.TAnnotation;
import com.navercorp.pinpoint.thrift.dto.TIntStringStringValue;
import com.navercorp.pinpoint.thrift.dto.TSpan;
import com.navercorp.pinpoint.thrift.dto.TSpanEvent;
/**
* @author emeroad
* @author koo.taejin
* @author hyungil.jeong
* @author jaehong.kim
*/
public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier {
private TestableServerMetaDataListener serverMetaDataListener;
private AnnotationKeyRegistryService annotationKeyRegistryService;
private final List<Short> ignoredServiceTypes = new ArrayList<Short>();
public PluginTestAgent(AgentOption agentOption) {
super(agentOption, new DefaultInterceptorRegistryBinder());
this.annotationKeyRegistryService = agentOption.getAnnotationKeyRegistryService();
PluginTestVerifierHolder.setInstance(this);
}
@Override
protected DataSender createUdpStatDataSender(int port, String threadName, int writeQueueSize, int timeout, int sendBufferSize) {
return new ListenableDataSender<TBase<?, ?>>();
}
@Override
protected DataSender createUdpSpanDataSender(int port, String threadName, int writeQueueSize, int timeout, int sendBufferSize) {
ListenableDataSender<TBase<?, ?>> sender = new ListenableDataSender<TBase<?, ?>>();
sender.setListener(new OrderedSpanRecorder());
return sender;
}
@Override
protected StorageFactory createStorageFactory() {
return new SimpleSpanStorageFactory(super.getSpanDataSender());
}
@Override
protected EnhancedDataSender createTcpDataSender(CommandDispatcher commandDispatcher) {
return new TestTcpDataSender();
}
@Override
protected ServerMetaDataHolder createServerMetaDataHolder() {
List<String> vmArgs = RuntimeMXBeanUtils.getVmArgs();
ServerMetaDataHolder serverMetaDataHolder = new ResettableServerMetaDataHolder(vmArgs);
this.serverMetaDataListener = new TestableServerMetaDataListener();
serverMetaDataHolder.addListener(this.serverMetaDataListener);
return serverMetaDataHolder;
}
@Override
public void verifyServerType(String serviceTypeName) {
ServiceType expectedType = findServiceType(serviceTypeName);
ServiceType actualType = getAgentInformation().getServerType();
if (!expectedType.equals(actualType)) {
throw new AssertionError("ResolvedExpectedTrace server type: " + expectedType.getName() + "[" + expectedType.getCode() + "] but was " + actualType + "[" + actualType.getCode() + "]");
}
}
@Override
public void verifyServerInfo(String expected) {
String actualName = this.serverMetaDataListener.getServerMetaData().getServerInfo();
if (!actualName.equals(expected)) {
throw new AssertionError("ResolvedExpectedTrace server name [" + expected + "] but was [" + actualName + "]");
}
}
@Override
public void verifyConnector(String protocol, int port) {
Map<Integer, String> connectorMap = this.serverMetaDataListener.getServerMetaData().getConnectors();
String actualProtocol = connectorMap.get(port);
if (actualProtocol == null || !actualProtocol.equals(protocol)) {
throw new AssertionError("ResolvedExpectedTrace protocol [" + protocol + "] at port [" + port + "] but was [" + actualProtocol + "]");
}
}
@Override
public void verifyService(String name, List<String> libs) {
List<ServiceInfo> serviceInfos = this.serverMetaDataListener.getServerMetaData().getServiceInfos();
for (ServiceInfo serviceInfo : serviceInfos) {
if (serviceInfo.getServiceName().equals(name)) {
List<String> actualLibs = serviceInfo.getServiceLibs();
if (actualLibs.size() != libs.size()) {
throw new AssertionError("ResolvedExpectedTrace service [" + name + "] with libraries [" + libs + "] but was [" + actualLibs + "]");
}
for (String lib : libs) {
if (!actualLibs.contains(lib)) {
throw new AssertionError("ResolvedExpectedTrace service [" + name + "] with libraries [" + libs + "] but was [" + actualLibs + "]");
}
}
// OK
return;
}
}
throw new AssertionError("ResolvedExpectedTrace service [" + name + "] with libraries [" + libs + "] but there is no such service");
}
private boolean isIgnored(Object obj) {
short serviceType = -1;
if (obj instanceof TSpan) {
serviceType = ((TSpan) obj).getServiceType();
} else if (obj instanceof TSpanEvent) {
serviceType = ((TSpanEvent) obj).getServiceType();
}
return ignoredServiceTypes.contains(serviceType);
}
@Override
public void verifyTraceCount(int expected) {
int actual = 0;
for (Object obj : getRecorder()) {
if (!isIgnored(obj)) {
actual++;
}
}
if (expected != actual) {
throw new AssertionError("ResolvedExpectedTrace count: " + expected + ", actual: " + actual);
}
}
private ServiceType findServiceType(String name) {
ServiceType serviceType = getServiceTypeRegistryService().findServiceTypeByName(name);
if (serviceType == ServiceType.UNDEFINED) {
throw new AssertionError("No such service type: " + name);
}
return serviceType;
}
private Class<?> resolveSpanClass(TraceType type) {
switch (type) {
case ROOT:
return Span.class;
case EVENT:
return SpanEvent.class;
}
throw new IllegalArgumentException(type.toString());
}
@Override
public void verifyDiscreteTrace(ExpectedTrace... expectations) {
verifyDiscreteTraceBlock(expectations, null);
}
public void verifyDiscreteTraceBlock(ExpectedTrace[] expectations, Integer asyncId) {
if (expectations == null || expectations.length == 0) {
throw new IllegalArgumentException("No expectations");
}
ExpectedTrace expected = expectations[0];
ResolvedExpectedTrace resolved = resolveExpectedTrace(expected, asyncId);
int i = 0;
Iterator<?> iterator = getRecorder().iterator();
while (iterator.hasNext()) {
ActualTrace actual = wrap(iterator.next());
try {
verifySpan(resolved, actual);
} catch (AssertionError e) {
continue;
}
iterator.remove();
verifyAsyncTraces(expected, actual);
if (++i == expectations.length) {
return;
}
expected = expectations[i];
resolved = resolveExpectedTrace(expected, asyncId);
}
throw new AssertionError("Failed to match " + i + "th expectation: " + resolved);
}
@Override
public void verifyTrace(ExpectedTrace... expectations) {
if (expectations == null || expectations.length == 0) {
throw new IllegalArgumentException("No expectations");
}
for (ExpectedTrace expected : expectations) {
ResolvedExpectedTrace resolved = resolveExpectedTrace(expected, null);
Object actual = popSpan();
if (actual == null) {
throw new AssertionError("Expected a " + resolved.type.getSimpleName() + " but there is no trace");
}
ActualTrace wrapped = wrap(actual);
verifySpan(resolved, wrapped);
verifyAsyncTraces(expected, wrapped);
}
}
private void verifyAsyncTraces(ExpectedTrace expected, ActualTrace wrapped) throws AssertionError {
ExpectedTrace[] asyncTraces = expected.getAsyncTraces();
if (asyncTraces != null && asyncTraces.length > 0) {
Integer asyncId = wrapped.getNextAsyncId();
if (asyncId == null) {
throw new AssertionError("Expected async traces triggered but nextAsyncId is not present: " + wrapped);
}
verifyDiscreteTraceBlock(asyncTraces, asyncId);
}
}
private ResolvedExpectedTrace resolveExpectedTrace(ExpectedTrace expected, Integer asyncId) throws AssertionError {
final ServiceType serviceType = findServiceType(expected.getServiceType());
final Class<?> spanClass = resolveSpanClass(expected.getType());
final int apiId = getApiId(expected);
return new ResolvedExpectedTrace(spanClass, serviceType, apiId, expected.getRpc(), expected.getEndPoint(), expected.getRemoteAddr(), expected.getDestinationId(), expected.getAnnotations(), asyncId);
}
private int getApiId(ExpectedTrace expected) {
final Member method = expected.getMethod();
if (method == null) {
if (expected.getMethodSignature() == null) {
// return null;
throw new RuntimeException("Method or MethodSignature is null");
} else {
return findApiId(expected.getMethodSignature());
}
} else {
return findApiId(method);
}
}
@Override
public void ignoreServiceType(String... serviceTypes) {
for (String serviceType : serviceTypes) {
ServiceType t = findServiceType(serviceType);
ignoredServiceTypes.add(t.getCode());
}
}
private static void appendAnnotations(StringBuilder builder, List<TAnnotation> annotations) {
boolean first = true;
if (annotations != null) {
for (TAnnotation a : annotations) {
if (first) {
first = false;
} else {
builder.append(", ");
}
builder.append(toString(a));
}
}
}
private static String toString(TAnnotation a) {
return a.getKey() + "=" + a.getValue().getFieldValue();
}
private interface ActualTrace {
Short getServiceType();
Integer getApiId();
Integer getAsyncId();
Integer getNextAsyncId();
String getRpc();
String getEndPoint();
String getRemoteAddr();
String getDestinationId();
List<TAnnotation> getAnnotations();
Class<?> getType();
}
private static final class SpanFacade implements ActualTrace {
private final Span span;
public SpanFacade(Span span) {
this.span = span;
}
@Override
public Short getServiceType() {
return span.isSetServiceType() ? span.getServiceType() : null;
}
@Override
public Integer getApiId() {
return span.isSetApiId() ? span.getApiId() : null;
}
@Override
public Integer getAsyncId() {
return null;
}
@Override
public Integer getNextAsyncId() {
return null;
}
@Override
public String getRpc() {
return span.getRpc();
}
@Override
public String getEndPoint() {
return span.getEndPoint();
}
@Override
public String getRemoteAddr() {
return span.getRemoteAddr();
}
@Override
public String getDestinationId() {
return null;
}
@Override
public List<TAnnotation> getAnnotations() {
return span.getAnnotations();
}
@Override
public Class<?> getType() {
return Span.class;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("(serviceType: ");
builder.append(getServiceType());
builder.append(", apiId: ");
builder.append(getApiId());
builder.append(", rpc: ");
builder.append(getRpc());
builder.append(", endPoint: ");
builder.append(getEndPoint());
builder.append(", remoteAddr: ");
builder.append(getRemoteAddr());
builder.append(", [");
appendAnnotations(builder, getAnnotations());
builder.append("])");
return builder.toString();
}
}
private static final class SpanEventFacade implements ActualTrace {
private final SpanEvent span;
public SpanEventFacade(SpanEvent span) {
this.span = span;
}
@Override
public Short getServiceType() {
return span.isSetServiceType() ? span.getServiceType() : null;
}
@Override
public Integer getApiId() {
return span.isSetApiId() ? span.getApiId() : null;
}
@Override
public Integer getAsyncId() {
return span.isSetAsyncId() ? span.getAsyncId() : null;
}
@Override
public Integer getNextAsyncId() {
return span.isSetNextAsyncId() ? span.getNextAsyncId() : null;
}
@Override
public String getRpc() {
return span.getRpc();
}
@Override
public String getEndPoint() {
return span.getEndPoint();
}
@Override
public String getRemoteAddr() {
return null;
}
@Override
public String getDestinationId() {
return span.getDestinationId();
}
@Override
public List<TAnnotation> getAnnotations() {
return span.getAnnotations();
}
@Override
public Class<?> getType() {
return SpanEvent.class;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("(serviceType: ");
builder.append(getServiceType());
builder.append(", apiId: ");
builder.append(getApiId());
builder.append(", rpc: ");
builder.append(getRpc());
builder.append(", endPoint: ");
builder.append(getEndPoint());
builder.append(", destinationId: ");
builder.append(getDestinationId());
builder.append(", [");
appendAnnotations(builder, getAnnotations());
builder.append("], asyncId: ");
builder.append(getAsyncId());
builder.append("nextAsyncId: ");
builder.append(getNextAsyncId());
builder.append(')');
return builder.toString();
}
}
private static final class ResolvedExpectedTrace {
private final Class<?> type;
private final ServiceType serviceType;
private final Integer asyncId;
private final Integer apiId;
private final String rpc;
private final String endPoint;
private final String remoteAddr;
private final String destinationId;
private final ExpectedAnnotation[] annotations;
public ResolvedExpectedTrace(Class<?> type, ServiceType serviceType, Integer apiId, String rpc, String endPoint, String remoteAddr, String destinationId, ExpectedAnnotation[] annotations, Integer asyncId) {
this.type = type;
this.serviceType = serviceType;
this.apiId = apiId;
this.rpc = rpc;
this.endPoint = endPoint;
this.remoteAddr = remoteAddr;
this.destinationId = destinationId;
this.annotations = annotations;
this.asyncId = asyncId;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(type.getSimpleName());
builder.append("(serviceType: ");
builder.append(serviceType.getCode());
builder.append(", apiId: ");
builder.append(apiId);
builder.append(", rpc: ");
builder.append(rpc);
builder.append(", endPoint: ");
builder.append(endPoint);
builder.append(", remoteAddr: ");
builder.append(remoteAddr);
builder.append(", destinationId: ");
builder.append(destinationId);
builder.append(", annotations: ");
builder.append(Arrays.deepToString(annotations));
builder.append(", asyncId: ");
builder.append(asyncId);
builder.append(")");
return builder.toString();
}
}
private ActualTrace wrap(Object obj) {
if (obj instanceof Span) {
return new SpanFacade((Span) obj);
} else if (obj instanceof SpanEvent) {
return new SpanEventFacade((SpanEvent) obj);
}
throw new IllegalArgumentException("Unexpected type: " + obj.getClass());
}
private static boolean equals(Object expected, Object actual) {
// if expected is null, no need to compare.
return expected == null ? true : (expected.equals(actual));
}
private void verifySpan(ResolvedExpectedTrace expected, ActualTrace actual) {
if (!expected.type.equals(actual.getType())) {
throw new AssertionError("Expected an instance of " + expected.type.getSimpleName() + " but was " + actual.getType().getName() + ". expected: " + expected + ", was: " + actual);
}
if (!equals(expected.serviceType.getCode(), actual.getServiceType())) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with serviceType[" + expected.serviceType.getCode() + "] but was [" + actual.getServiceType() + "]. expected: " + expected + ", was: " + actual);
}
if (!equals(expected.apiId, actual.getApiId())) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with apiId[" + expected.apiId + "] but was [" + actual.getApiId() + "]. expected: " + expected + ", was: " + actual);
}
if (!equals(expected.rpc, actual.getRpc())) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with rpc[" + expected.rpc + "] but was [" + actual.getRpc() + "]. expected: " + expected + ", was: " + actual);
}
if (!equals(expected.endPoint, actual.getEndPoint())) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with endPoint[" + expected.endPoint + "] but was [" + actual.getEndPoint() + "]. expected: " + expected + ", was: " + actual);
}
if (!equals(expected.remoteAddr, actual.getRemoteAddr())) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with remoteAddr[" + expected.remoteAddr + "] but was [" + actual.getRemoteAddr() + "]. expected: " + expected + ", was: " + actual);
}
if (!equals(expected.destinationId, actual.getDestinationId())) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with destinationId[" + expected.destinationId + "] but was [" + actual.getDestinationId() + "]. expected: " + expected + ", was: " + actual);
}
if (!equals(expected.asyncId, actual.getAsyncId())) {
throw new AssertionError("Expected a " + expected.type.getSimpleName() + " with asyncId[" + expected.asyncId + "] but was [" + actual.getAsyncId() + "]. expected: " + expected + ", was: " + actual);
}
List<TAnnotation> actualAnnotations = actual.getAnnotations();
int len = expected.annotations == null ? 0 : expected.annotations.length;
int actualLen = actualAnnotations == null ? 0 : actualAnnotations.size();
if (actualLen != len) {
throw new AssertionError("Expected [" + len + "] annotations but was [" + actualLen + "], expected: " + expected + ", was: " + actual);
}
for (int i = 0; i < len; i++) {
ExpectedAnnotation expect = expected.annotations[i];
AnnotationKey expectedAnnotationKey = annotationKeyRegistryService.findAnnotationKeyByName(expect.getKeyName());
TAnnotation actualAnnotation = actualAnnotations.get(i);
if (expectedAnnotationKey.getCode() != actualAnnotation.getKey()) {
throw new AssertionError("Expected " + i + "th annotation [" + expectedAnnotationKey.getCode() + "=" + expect.getValue() + "] but was [" + toString(actualAnnotation) + "], expected: " + expected + ", was: " + actual);
}
if (expectedAnnotationKey == AnnotationKey.SQL_ID && expect instanceof ExpectedSql) {
verifySql((ExpectedSql) expect, actualAnnotation);
} else {
Object expectedValue = expect.getValue();
if (expectedValue == Expectations.anyAnnotationValue()) {
continue;
}
if (AnnotationKeyUtils.isCachedArgsKey(expectedAnnotationKey.getCode())) {
expectedValue = getTestTcpDataSender().getStringId(expectedValue.toString());
}
if (!Objects.equal(expectedValue, actualAnnotation.getValue().getFieldValue())) {
throw new AssertionError("Expected " + i + "th annotation [" + expectedAnnotationKey.getCode() + "=" + expect.getValue() + "] but was [" + toString(actualAnnotation) + "], expected: " + expected + ", was: " + actual);
}
}
}
}
private void verifySql(ExpectedSql expected, TAnnotation actual) {
int id = getTestTcpDataSender().getSqlId(expected.getQuery());
TIntStringStringValue value = actual.getValue().getIntStringStringValue();
if (value.getIntValue() != id) {
String actualQuery = getTestTcpDataSender().getSql(value.getIntValue());
throw new AssertionError("Expected sql [" + id + ": " + expected.getQuery() + "] but was [" + value.getIntValue() + ": " + actualQuery + "], expected: " + expected + ", was: " + actual);
}
if (!Objects.equal(value.getStringValue1(), expected.getOutput())) {
throw new AssertionError("Expected sql with output [" + expected.getOutput() + "] but was [" + value.getStringValue1() + "], expected: " + expected + ", was: " + actual);
}
if (!Objects.equal(value.getStringValue2(), expected.getBindValuesAsString())) {
throw new AssertionError("Expected sql with bindValues [" + expected.getBindValuesAsString() + "] but was [" + value.getStringValue2() + "], expected: " + expected + ", was: " + actual);
}
}
private int findApiId(Member method) throws AssertionError {
String desc;
if (method instanceof Method) {
desc = getMethodInfo((Method) method);
} else if (method instanceof Constructor) {
desc = getMethodInfo((Constructor<?>) method);
} else {
throw new IllegalArgumentException("method: " + method);
}
return findApiId(desc);
}
private String getMethodInfo(Method method) {
Class<?>[] parameterTypes = method.getParameterTypes();
String[] parameterTypeNames = JavaAssistUtils.toPinpointParameterType(parameterTypes);
return ApiUtils.toMethodDescriptor(method.getDeclaringClass().getName(), method.getName(), parameterTypeNames);
}
private String getMethodInfo(Constructor<?> constructor) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
String[] parameterTypeNames = JavaAssistUtils.getParameterType(parameterTypes);
return ApiUtils.toMethodDescriptor(constructor.getDeclaringClass().getName(), constructor.getName(), parameterTypeNames);
}
private int findApiId(String desc) throws AssertionError {
try {
return getTestTcpDataSender().getApiId(desc);
} catch (NoSuchElementException e) {
throw new AssertionError("Cannot find apiId of [" + desc + "]");
}
}
private TestTcpDataSender getTestTcpDataSender() {
return (TestTcpDataSender) getTcpDataSender();
}
private OrderedSpanRecorder getRecorder() {
return (OrderedSpanRecorder) ((ListenableDataSender<?>) getSpanDataSender()).getListener();
}
private Object popSpan() {
while (true) {
Object obj = getRecorder().pop();
if (obj == null) {
return null;
}
if (!isIgnored(obj)) {
return obj;
}
}
}
@Override
public void printCache(PrintStream out) {
getRecorder().print(out);
getTestTcpDataSender().printDatas(out);
}
@Override
public void printCache() {
printCache(System.out);
}
@Override
public void initialize(boolean createTraceObject) {
if (createTraceObject) {
getTraceContext().newTraceObject();
}
getRecorder().clear();
getTestTcpDataSender().clear();
ignoredServiceTypes.clear();
}
@Override
public void cleanUp(boolean detachTraceObject) {
if (detachTraceObject) {
getTraceContext().removeTraceObject();
}
getRecorder().clear();
getTestTcpDataSender().clear();
ignoredServiceTypes.clear();
}
@Override
public void verifyIsLoggingTransactionInfo(LoggingInfo loggingInfo) {
Object actual = popSpan();
Span span = null;
if (actual instanceof Span) {
span = (Span) actual;
} else if (actual instanceof SpanEvent) {
span = ((SpanEvent) actual).getSpan();
} else {
throw new IllegalArgumentException("Unexpected type: " + getActual(actual));
}
if (span.getLoggingTransactionInfo() != loggingInfo.getCode()) {
LoggingInfo loggingTransactionInfo = LoggingInfo.searchByCode(span.getLoggingTransactionInfo());
if (loggingTransactionInfo != null) {
throw new AssertionError("Expected a Span isLoggingTransactionInfo value with [" + loggingInfo.getName() + "] but was [" + loggingTransactionInfo.getName() + "]. expected: " + loggingInfo.getName() + ", was: " + loggingTransactionInfo.getName());
} else {
throw new AssertionError("Expected a Span isLoggingTransactionInfo value with [" + loggingInfo.getName() + "] but loggingTransactionInfo value invalid.");
}
}
}
private String getActual(Object actual) {
if (actual == null) {
return "actual is null";
}
return actual.getClass().getName();
}
}