-
Notifications
You must be signed in to change notification settings - Fork 566
/
Copy pathScouter.java
667 lines (589 loc) · 17 KB
/
Scouter.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
package scouterx.weaver;
/*
* Copyright 2015 the original author or authors.
* @https://github.com/scouter-project/scouter
*
* 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.
*/
/**
* Created by Gun Lee([email protected]) on 2021/10/21
*/
public class Scouter {
/**
* check if scouter java agent is activated
* @return
*/
public static boolean isScouterJavaAgentActivated() {
Object scouterJavaAgentActivated = Weaving.isScouterJavaAgentActivated();
if (scouterJavaAgentActivated instanceof Boolean) {
return (boolean) scouterJavaAgentActivated;
} else {
return false;
}
}
/**
* get already started transfer context. if the trace is not started, it returns empty transfer(trace) context.
* @return
*/
public static TransferCtx getTransferCtxOnTheSameThread() {
Object ctx = Weaving.getCtxOnTheSameThread();
if (ctx == null) {
return TransferCtx.EMPTY;
}
Object txid = Weaving.getTxidByCtx(ctx);
return new TransferCtx(ctx, ScouterTxid.of(txid));
}
/**
* get already started transfer context. if the trace is not started, it returns empty transfer(trace) context.
* @param customTxid
* @return
*/
public static TransferCtx getTransferCtxByCustomTxid(String customTxid) {
Object ctx = Weaving.getCtxByCustomTxid(customTxid);
if (ctx == null) {
return TransferCtx.EMPTY;
}
Object txid = Weaving.getTxidByCtx(ctx);
return new TransferCtx(ctx, ScouterTxid.of(txid));
}
/**
* start trace(Xlog) for the serviceName and generate new transferCtx of the transaction for transferring a trace context via service flow.
* @param serviceName
* @return
*/
public static TransferCtx startServiceAndGetCtxTransfer(String serviceName) {
try {
Object ctx = Weaving.startServiceAndGetCtx(serviceName);
if (ctx == null) {
return TransferCtx.EMPTY;
}
Object txid = Weaving.getTxidByCtx(ctx);
return new TransferCtx(ctx, ScouterTxid.of(txid));
} catch (Exception e) {
e.printStackTrace();
return TransferCtx.EMPTY;
}
}
/**
* start trace(Xlog) for the serviceName with connecting custom txid with scouter
* and generate new transferCtx of the transaction for transferring a trace context via service flow.
* @param serviceName
* @return
*/
public static TransferCtx startServiceWithCustomTxidAndGetCtxTransfer(String serviceName, String customTxid) {
try {
Object ctx = Weaving.startServiceWithCustomTxidAndGetCtx(serviceName, customTxid);
if (ctx == null) {
return TransferCtx.EMPTY;
}
Object txid = Weaving.getTxidByCtx(ctx);
return new TransferCtx(ctx, ScouterTxid.of(txid));
} catch (Exception e) {
e.printStackTrace();
return TransferCtx.EMPTY;
}
}
/**
* end trace(Xlog) and internally measure an elapsed time of service.
* error is marked on the xlog if thr is not null
* @param ctxTransfer
* @param thr
*/
public static void endServiceByCtxTransfer(TransferCtx ctxTransfer, Throwable thr) {
if (ctxTransfer == null || ctxTransfer.isEmpty()) {
return;
}
try {
Weaving.endServiceByCtx(ctxTransfer.ctx, thr);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* end trace(Xlog) and internally measure an elapsed time of service.
* error is marked on the xlog if thr is not null
* @param customTxid
* @param thr
*/
public static void endServiceByCustomTxid(String customTxid, Throwable thr) {
try {
Weaving.endServiceByCustomTxid(customTxid, thr);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* end trace(Xlog) and internally measure an elapsed time of service.
* error is marked on the xlog if thr is not null
* @param thr
*/
public static void endServiceOnTheSameThread(Throwable thr) {
try {
Weaving.endServiceOnTheSameThread(thr);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* start method profiling
* @param ctxTransfer
* @param name
* @return
*/
public static MethodCtx startMethodByCtxTransfer(TransferCtx ctxTransfer, String name) {
if (ctxTransfer == null || ctxTransfer.isEmpty()) {
return MethodCtx.EMPTY;
}
try {
return new MethodCtx(Weaving.startMethodByCtx(ctxTransfer.ctx, name));
} catch (Exception e) {
e.printStackTrace();
return MethodCtx.EMPTY;
}
}
/**
* start method profiling
* @param name
* @return
*/
public static MethodCtx startMethodOnTheSameThread(String name) {
try {
return new MethodCtx(Weaving.startMethodOnTheSameThread(name));
} catch (Exception e) {
e.printStackTrace();
return MethodCtx.EMPTY;
}
}
/**
* start method profiling
* @param customTxid
* @param name
* @return
*/
public static MethodCtx startMethodByCustomTxid(String customTxid, String name) {
try {
return new MethodCtx(Weaving.startMethodByCustomTxid(customTxid, name));
} catch (Exception e) {
e.printStackTrace();
return MethodCtx.EMPTY;
}
}
/**
* end method profiling and mark this method's elapsed time on the profile.
* @param methodTransfer
* @param thr
*/
public static void endMethodByMethodTransfer(MethodCtx methodTransfer, Throwable thr) {
if (methodTransfer == null || methodTransfer.isEmpty()) {
return;
}
try {
Weaving.endMethodByMethodTransfer(methodTransfer.lctx, thr);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add message profile
* @param ctxTransfer
* @param message
*/
public static void addMessageProfileByCtxTransfer(TransferCtx ctxTransfer, String message) {
if (ctxTransfer == null || ctxTransfer.isEmpty()) {
return;
}
try {
Weaving.addMessageProfileByCtx(ctxTransfer.ctx, message);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add message profile
* @param customTxid
* @param message
*/
public static void addMessageProfileByCustomTxid(String customTxid, String message) {
try {
Weaving.addMessageProfileByCustomTxid(customTxid, message);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add message profile
* @param message
*/
public static void addMessageProfileOnTheSameThread(String message) {
try {
Weaving.addMessageProfileOnTheSameThread(message);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add hashed(dictionary encoded) message profile.
* @param ctxTransfer
* @param message
* @param elapsedMs
* @param anyValue
*/
public static void addHashedMessageProfileByCtxTransfer(TransferCtx ctxTransfer, String message, int elapsedMs, int anyValue) {
if (ctxTransfer == null || ctxTransfer.isEmpty()) {
return;
}
try {
Weaving.addHashedMessageProfileByCtx(ctxTransfer.ctx, message, elapsedMs, anyValue);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add hashed(dictionary encoded) message profile.
* @param customTxid
* @param message
* @param elapsedMs
* @param anyValue
*/
public static void addHashedMessageProfileByCustomTxid(String customTxid, String message, int elapsedMs, int anyValue) {
try {
Weaving.addHashedMessageProfileByCustomTxid(customTxid, message, elapsedMs, anyValue);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add hashed(dictionary encoded) message profile.
* @param message
* @param elapsedMs
* @param anyValue
*/
public static void addHashedMessageProfileOnTheSameThread(String message, int elapsedMs, int anyValue) {
try {
Weaving.addHashedMessageProfileOnTheSameThread(message, elapsedMs, anyValue);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add hashed(dictionary encoded) with parameterized message profile.
* @param ctxTransfer
* @param message
* @param level
* @param elapsedMs
* @param params
*/
public static void addParameterizedMessageProfileByCtxTransfer(TransferCtx ctxTransfer, String message, ProfileLevel level, int elapsedMs, String... params) {
if (ctxTransfer == null || ctxTransfer.isEmpty()) {
return;
}
try {
Weaving.addParameterizedMessageProfileByCtx(ctxTransfer.ctx, message, level.getLevel(), elapsedMs, params);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add hashed(dictionary encoded) with parameterized message profile.
* @param customTxid
* @param message
* @param level
* @param elapsedMs
* @param params
*/
public static void addParameterizedMessageProfileByCustomTxid(String customTxid, String message, ProfileLevel level, int elapsedMs, String... params) {
try {
Weaving.addParameterizedMessageProfileByCustomTxid(customTxid, message, level.getLevel(), elapsedMs, params);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* add hashed(dictionary encoded) with parameterized message profile.
* @param message
* @param level
* @param elapsedMs
* @param params
*/
public static void addParameterizedMessageProfileOnTheSameThread(String message, ProfileLevel level, int elapsedMs, String... params) {
try {
Weaving.addParameterizedMessageProfileOnTheSameThread(message, level.getLevel(), elapsedMs, params);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* get ScouterTxid
* @return
*/
public static ScouterTxid getTxidOnTheSameThread() {
Object txid = Weaving.getTxidOnTheSameThread();
return ScouterTxid.of(txid);
}
/**
* get ScouterTxid
* @param customTxid
* @return
*/
public static ScouterTxid getTxidByCustomTxid(String customTxid) {
Object txid = Weaving.getTxidByCustomTxid(customTxid);
return ScouterTxid.of(txid);
}
/**
* link custom txid onto the scouter trace context.
* @param customTxid
*/
public static void linkCustomTxidOnTheSameThread(String customTxid) {
Weaving.linkCustomTxidOnTheSameThread(customTxid);
}
/**
* link custom txid onto the scouter trace context.
* @param customTxid
* @param ctxTransfer
*/
public static void linkCustomTxidByCtxTransfer(String customTxid, TransferCtx ctxTransfer) {
if (ctxTransfer == null || ctxTransfer.isEmpty()) {
return;
}
Weaving.linkCustomTxidByCtx(customTxid, ctxTransfer.ctx);
}
/**
* add value onto xlog service column
* @param txid
* @param value
*/
public static void setXlogServiceDictionaryValue(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogServiceValue(txid.getTxid(), value);
}
/**
* add value onto xlog ip column
* @param txid
* @param value
*/
public static void setXlogIpValue(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogIpValue(txid.getTxid(), value);
}
/**
* add value onto xlog user agent column
* @param txid
* @param value
*/
public static void setXlogUaDictionaryValue(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogUaValue(txid.getTxid(), value);
}
/**
* add value onto xlog error column
* @param txid
* @param value
*/
public static void setXlogErrorDictionaryValue(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogErrorValue(txid.getTxid(), value);
}
/**
* add value onto xlog login column
* @param txid
* @param value
*/
public static void setXlogLoginDictionaryValue(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogLoginValue(txid.getTxid(), value);
}
/**
* add value onto xlog desc column
* @param txid
* @param value
*/
public static void setXlogDescDictionaryValue(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogDescValue(txid.getTxid(), value);
}
/**
* add value onto xlog text1 column
* @param txid
* @param value
*/
public static void setXlogText1Value(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogText1Value(txid.getTxid(), value);
}
/**
* add value onto xlog text2 column
* @param txid
* @param value
*/
public static void setXlogText2Value(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogText2Value(txid.getTxid(), value);
}
/**
* add value onto xlog text3 column
* @param txid
* @param value
*/
public static void setXlogText3Value(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogText3Value(txid.getTxid(), value);
}
/**
* add value onto xlog text4 column
* @param txid
* @param value
*/
public static void setXlogText4Value(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogText4Value(txid.getTxid(), value);
}
/**
* add value onto xlog text5 column
* @param txid
* @param value
*/
public static void setXlogText5Value(ScouterTxid txid, String value) {
if (txid == null || txid.isEmpty() || value == null) {
return;
}
Weaving.setXlogText5Value(txid.getTxid(), value);
}
public enum ProfileLevel {
DEBUG((byte)0),
INFO((byte)1),
WARN((byte)2),
ERROR((byte)3),
FATAL((byte)4),
;
private final byte level;
ProfileLevel(byte level) {
this.level = level;
}
public byte getLevel() {
return this.level;
}
}
/**
* Created by Gun Lee([email protected]) on 2021/10/21
*/
private static class Weaving {
protected static Object isScouterJavaAgentActivated() {
return false;
}
protected static Object getCtxOnTheSameThread() {
return null;
}
protected static Object getCtxByCustomTxid(String customTxid) {
return null;
}
protected static Object startServiceAndGetCtx(String serviceName) {
return null;
}
protected static Object startServiceWithCustomTxidAndGetCtx(String serviceName, String customTxid) {
return null;
}
protected static void endServiceByCtx(Object ctx, Throwable thr) {
}
protected static void endServiceByCustomTxid(String customTxid, Throwable thr) {
}
protected static void endServiceOnTheSameThread(Throwable thr) {
}
protected static Object startMethodByCtx(Object ctx, String name) {
return null;
}
protected static Object startMethodByCustomTxid(String customTxid, String name) {
return null;
}
protected static Object startMethodOnTheSameThread(String name) {
return null;
}
protected static void endMethodByMethodTransfer(Object methodTransfer, Throwable thr) {
}
protected static void addMessageProfileByCtx(Object ctx, String message) {
}
protected static void addMessageProfileByCustomTxid(String customTxid, String message) {
}
protected static void addMessageProfileOnTheSameThread(String message) {
}
protected static void addHashedMessageProfileByCtx(Object ctx, String message, int elapsedMs, int anyValue) {
}
protected static void addHashedMessageProfileByCustomTxid(String customTxid, String message, int elapsedMs, int anyValue) {
}
protected static void addHashedMessageProfileOnTheSameThread(String message, int elapsedMs, int anyValue) {
}
protected static void addParameterizedMessageProfileByCtx(Object ctx, String message, byte level, int elapsedMs, String... params) {
}
protected static void addParameterizedMessageProfileByCustomTxid(String customTxid, String message, byte level, int elapsedMs, String... params) {
}
protected static void addParameterizedMessageProfileOnTheSameThread(String message, byte level, int elapsedMs, String... params) {
}
//txid getters
protected static Object getTxidOnTheSameThread() {
return null;
}
protected static Object getTxidByCtx(Object ctx) {
return null;
}
protected static Object getTxidByCustomTxid(String customTxid) {
return null;
}
//link custom txid
protected static void linkCustomTxidOnTheSameThread(String customTxid) {
}
protected static void linkCustomTxidByCtx(String customTxid, Object ctx) {
}
//xlog info setters
protected static void setXlogServiceValue(long txid, String value) {
}
protected static void setXlogIpValue(long txid, String value) {
}
protected static void setXlogUaValue(long txid, String value) {
}
protected static void setXlogErrorValue(long txid, String value) {
}
protected static void setXlogLoginValue(long txid, String value) {
}
protected static void setXlogDescValue(long txid, String value) {
}
protected static void setXlogText1Value(long txid, String value) {
}
protected static void setXlogText2Value(long txid, String value) {
}
protected static void setXlogText3Value(long txid, String value) {
}
protected static void setXlogText4Value(long txid, String value) {
}
protected static void setXlogText5Value(long txid, String value) {
}
}
}