2121import scouter .agent .trace .TraceContext ;
2222import scouter .agent .trace .TraceContextManager ;
2323import scouter .agent .trace .TraceMain ;
24+ import scouter .lang .enumeration .ParameterizedMessageLevel ;
2425import scouter .lang .pack .XLogTypes ;
2526import scouter .lang .step .HashedMessageStep ;
2627import scouter .lang .step .MessageStep ;
@@ -34,22 +35,35 @@ public class TraceSupport {
3435 private static final Object dummyObj = new Object ();
3536 private static final Object [] dummyArgs = new Object [0 ];
3637
37- public static Object startServiceAndGetCtxTransfer (String serviceName ) {
38+
39+ public static Object getCtxOnTheSameThread () {
40+ return TraceContextManager .getContext ();
41+ }
42+
43+ public static TraceContext getCtxByCustomTxid (String customTxid ) {
44+ return TraceContextManager .getContextByCustomTxid (customTxid );
45+ }
46+
47+ public static Object startServiceAndGetCtx (String serviceName ) {
3848 return TraceMain .startService (serviceName , "_custom_" , serviceName , "_none_" , dummyObj , dummyArgs , XLogTypes .APP_SERVICE );
3949 }
4050
41- public static Object startServiceWithCustomTxidAndGetCtxTransfer (String serviceName , String customTxid ) {
51+ public static Object startServiceWithCustomTxidAndGetCtx (String serviceName , String customTxid ) {
4252 Object o = TraceMain .startService (serviceName , "_custom_" , serviceName , "_none_" , dummyObj , dummyArgs , XLogTypes .APP_SERVICE );
4353 TraceContext ctx = ((LocalContext ) o ).context ;
4454 TraceContextManager .linkCustomTxid (customTxid , ctx .txid );
4555 return o ;
4656 }
4757
48- public static void endServiceByCtxTransfer (Object ctxTransfer , Throwable thr ) {
49- if (ctxTransfer == null ) {
58+ public static void endServiceByCtx (Object anyCtx , Throwable thr ) {
59+ if (anyCtx == null ) {
5060 return ;
5161 }
52- TraceMain .endService (ctxTransfer , null , thr );
62+ Object ctx4EndService = anyCtx ;
63+ if (anyCtx instanceof TraceContext ) {
64+ ctx4EndService = new LocalContext ((TraceContext ) anyCtx , null );
65+ }
66+ TraceMain .endService (ctx4EndService , null , thr );
5367 }
5468
5569 public static void endServiceByCustomTxid (String customTxid , Throwable thr ) {
@@ -69,8 +83,8 @@ private static void endService0(TraceContext ctx, Throwable thr) {
6983 TraceMain .endService (new LocalContext (ctx , null ), null , thr );
7084 }
7185
72- public static Object startMethodByCtxTransfer (Object ctxTransfer , String name ) {
73- TraceContext ctx = getCtxByCtxTransfer ( ctxTransfer );
86+ public static Object startMethodByCtx (Object anyCtx , String name ) {
87+ TraceContext ctx = getCtxByAnyCtx ( anyCtx );
7488 if (ctx == null ) {
7589 return null ;
7690 }
@@ -100,8 +114,8 @@ public static void endMethodByMethodTransfer(Object methodTransfer, Throwable th
100114 TraceMain .endMethod (methodTransfer , thr );
101115 }
102116
103- public static void addMessageProfileByCtxTransfer (Object ctxTransfer , String message ) {
104- TraceContext ctx = getCtxByCtxTransfer ( ctxTransfer );
117+ public static void addMessageProfileByCtx (Object anyCtx , String message ) {
118+ TraceContext ctx = getCtxByAnyCtx ( anyCtx );
105119 addMessageProfile0 (ctx , message );
106120 }
107121
@@ -116,13 +130,16 @@ public static void addMessageProfileOnTheSameThread(String message) {
116130 }
117131
118132 private static void addMessageProfile0 (TraceContext ctx , String message ) {
133+ if (ctx == null ) {
134+ return ;
135+ }
119136 MessageStep messageStep = new MessageStep ((int ) (System .currentTimeMillis () - ctx .startTime ), message );
120137 ctx .profile .add (messageStep );
121138 }
122139
123140
124- public static void addHashedMessageProfileByCtxTransfer (Object ctxTransfer , String message , int elapsedMs , int anyValue ) {
125- TraceContext ctx = getCtxByCtxTransfer ( ctxTransfer );
141+ public static void addHashedMessageProfileByCtx (Object anyCtx , String message , int elapsedMs , int anyValue ) {
142+ TraceContext ctx = getCtxByAnyCtx ( anyCtx );
126143 addHashedMessageProfile0 (ctx , message , elapsedMs , anyValue );
127144 }
128145
@@ -137,6 +154,9 @@ public static void addHashedMessageProfileOnTheSameThread(String message, int el
137154 }
138155
139156 private static void addHashedMessageProfile0 (TraceContext ctx , String message , int elapsedMs , int anyValue ) {
157+ if (ctx == null ) {
158+ return ;
159+ }
140160 HashedMessageStep step = new HashedMessageStep ();
141161 step .hash = DataProxy .sendHashedMessage (message );
142162 step .value = anyValue ;
@@ -147,8 +167,8 @@ private static void addHashedMessageProfile0(TraceContext ctx, String message, i
147167
148168
149169
150- public static void addParameterizedMessageProfileByCtxTransfer (Object ctxTransfer , String message , byte level , int elapsedMs , String ... params ) {
151- TraceContext ctx = getCtxByCtxTransfer ( ctxTransfer );
170+ public static void addParameterizedMessageProfileByCtx (Object anyCtx , String message , byte level , int elapsedMs , String ... params ) {
171+ TraceContext ctx = getCtxByAnyCtx ( anyCtx );
152172 addParameterizedMessageProfile0 (ctx , message , level , elapsedMs , params );
153173 }
154174
@@ -163,7 +183,9 @@ public static void addParameterizedMessageProfileOnTheSameThread(String message,
163183 }
164184
165185 private static void addParameterizedMessageProfile0 (TraceContext ctx , String message , byte level , int elapsedMs , String ... params ) {
166-
186+ if (ctx == null ) {
187+ return ;
188+ }
167189 ParameterizedMessageStep step = new ParameterizedMessageStep ();
168190 step .setMessage (DataProxy .sendHashedMessage (message ), params );
169191 step .setElapsed (elapsedMs );
@@ -175,27 +197,131 @@ private static void addParameterizedMessageProfile0(TraceContext ctx, String mes
175197
176198
177199
200+ //txid getters
201+ public static Object getTxidOnTheSameThread () {
202+ TraceContext ctx = TraceContextManager .getContext ();
203+ if (ctx != null ) {
204+ return ctx .txid ;
205+ }
206+ return 0 ;
207+ }
178208
209+ public static Object getTxidByCtx (Object anyCtx ) {
210+ TraceContext ctx = getCtxByAnyCtx (anyCtx );
211+ if (ctx != null ) {
212+ return ctx .txid ;
213+ }
214+ return 0 ;
215+ }
179216
217+ public static Object getTxidByCustomTxid (String customTxid ) {
218+ TraceContext ctx = getCtxByCustomTxid (customTxid );
219+ if (ctx != null ) {
220+ return ctx .txid ;
221+ }
222+ return 0 ;
223+ }
180224
225+ //link custom txid
226+ public static void linkCustomTxidOnTheSameThread (String customTxid ) {
227+ TraceContext ctx = TraceContextManager .getContext ();
228+ if (ctx != null ) {
229+ TraceContextManager .linkCustomTxid (customTxid , ctx .txid );
230+ }
231+ }
232+
233+ public static void linkCustomTxidByCtx (String customTxid , Object anyCtx ) {
234+ TraceContext ctx = getCtxByAnyCtx (anyCtx );
235+ if (ctx != null ) {
236+ TraceContextManager .linkCustomTxid (customTxid , ctx .txid );
237+ }
238+ }
181239
182- private static TraceContext getCtxByCtxTransfer (Object ctxTransfer ) {
183- if (ctxTransfer instanceof LocalContext ) {
184- LocalContext localContext = (LocalContext ) ctxTransfer ;
185- if (localContext != null ) {
186- return localContext .context ;
240+ //xlog info setters
241+ public static void setXlogServiceValue (long txid , String value ) {
242+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
243+ if (ctx != null && value != null ) {
244+ ctx .serviceName = value ;
245+ ctx .serviceHash = DataProxy .sendServiceName (ctx .serviceName );
246+ }
247+ }
248+ public static void setXlogIpValue (long txid , String value ) {
249+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
250+ if (ctx != null ) {
251+ ctx .remoteIp = value ;
252+ }
253+ }
254+ public static void setXlogUaValue (long txid , String value ) {
255+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
256+ if (ctx != null && value != null ) {
257+ ctx .userAgent = DataProxy .sendUserAgent (value );
258+ ctx .userAgentString = value ;
259+ }
260+ }
261+ public static void setXlogErrorValue (long txid , String value ) {
262+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
263+ if (ctx != null && value != null ) {
264+ if (ctx .error == 0 ) {
265+ ctx .error = DataProxy .sendError (value );
187266 }
188- return null ;
189267 }
190- return null ;
268+ addParameterizedMessageProfile0 ( ctx , value , ParameterizedMessageLevel . ERROR . getLevel (), 0 ) ;
191269 }
192270
193- private static TraceContext getCtxByCustomTxid (String customTxid ) {
194- TraceContext ctx = TraceContextManager .getContextByCustomTxid (customTxid );
195- if (ctx == null ) {
196- return null ;
197- } else {
198- return ctx ;
271+ public static void setXlogLoginValue (long txid , String value ) {
272+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
273+ if (ctx != null ) {
274+ ctx .login = value ;
275+ }
276+ }
277+ public static void setXlogDescValue (long txid , String value ) {
278+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
279+ if (ctx != null ) {
280+ ctx .desc = value ;
281+ }
282+ }
283+ public static void setXlogText1Value (long txid , String value ) {
284+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
285+ if (ctx != null ) {
286+ ctx .text1 = value ;
287+ }
288+ }
289+ public static void setXlogText2Value (long txid , String value ) {
290+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
291+ if (ctx != null ) {
292+ ctx .text2 = value ;
293+ }
294+ }
295+ public static void setXlogText3Value (long txid , String value ) {
296+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
297+ if (ctx != null ) {
298+ ctx .text3 = value ;
299+ }
300+ }
301+ public static void setXlogText4Value (long txid , String value ) {
302+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
303+ if (ctx != null ) {
304+ ctx .text4 = value ;
305+ }
306+ }
307+ public static void setXlogText5Value (long txid , String value ) {
308+ TraceContext ctx = TraceContextManager .getContextByTxid (txid );
309+ if (ctx != null ) {
310+ ctx .text5 = value ;
199311 }
200312 }
313+
314+
315+
316+
317+ private static TraceContext getCtxByAnyCtx (Object anyCtx ) {
318+ if (anyCtx instanceof LocalContext ) {
319+ LocalContext localContext = (LocalContext ) anyCtx ;
320+ return localContext .context ;
321+
322+ } else if (anyCtx instanceof TraceContext ) {
323+ return (TraceContext ) anyCtx ;
324+ }
325+ return null ;
326+ }
201327}
0 commit comments