1616
1717
1818from pinpointPy import Common , Defines , pinpoint
19+ import random
1920
2021
2122class PinpointCommonPlugin (Common .PinTrace ):
@@ -36,3 +37,92 @@ def onEnd(self, traceId, ret):
3637 def onException (self , traceId , e ):
3738 pinpoint .add_exception (str (e ), traceId )
3839 raise e
40+
41+
42+ class AsyncCommonPlugin (Common .AsyncPinTrace ):
43+
44+ # -> tuple[int, Any, dict[str, Any]]:
45+ def onBefore (self , parentId , * args , ** kwargs ):
46+ traceId , args , kwargs = super ().onBefore (parentId , * args , ** kwargs )
47+ pinpoint .add_trace_header (
48+ Defines .PP_INTERCEPTOR_NAME , self .getUniqueName (), traceId )
49+ pinpoint .add_trace_header (
50+ Defines .PP_SERVER_TYPE , Defines .PP_METHOD_CALL , traceId )
51+ return traceId , args , kwargs
52+
53+ def onEnd (self , traceId , ret ):
54+ super ().onEnd (traceId , ret )
55+
56+ def onException (self , traceId , e ):
57+ pinpoint .add_exception (str (e ), traceId )
58+
59+
60+ class HookTargetPlugins (Common .PinTrace ):
61+ def onBefore (self , parentId : int , * args , ** kwargs ):
62+
63+ traceId , args , kwargs = super ().onBefore (parentId , * args , ** kwargs )
64+ pinpoint .add_trace_header (
65+ Defines .PP_INTERCEPTOR_NAME , self .getUniqueName (), traceId )
66+ pinpoint .add_trace_header (
67+ Defines .PP_SERVER_TYPE , Defines .P_INVOCATION_CALL_TYPE , traceId )
68+
69+ async_id = random .randint (0 , 9999 )
70+ pinpoint .add_trace_header (
71+ Defines .PP_ASYNC_CALL_ID , f'{ async_id } ' , traceId )
72+
73+ sequence_id = pinpoint .get_sequence_id (traceId )
74+
75+ tid = pinpoint .get_context (Defines .PP_TRANSCATION_ID , traceId )
76+ seq_id = pinpoint .get_context (Defines .PP_SPAN_ID , traceId )
77+ app_name = pinpoint .get_context (Defines .PP_APP_NAME , traceId )
78+ app_id = pinpoint .get_context (Defines .PP_APP_ID , traceId )
79+
80+ if 'target' in kwargs :
81+ origin_target = kwargs ['target' ]
82+
83+ def pp_new_entry_func (* args , ** kwargs ):
84+ # start trace
85+ thread_trace_id = pinpoint .with_trace (0 )
86+ self .setCurrentTraceNodeId (thread_trace_id )
87+ pinpoint .add_trace_header (
88+ Defines .PP_APP_NAME , app_name , thread_trace_id )
89+ pinpoint .add_context (
90+ Defines .PP_APP_NAME , app_name , thread_trace_id )
91+
92+ pinpoint .add_trace_header (
93+ Defines .PP_APP_ID , app_id , thread_trace_id )
94+ pinpoint .add_context (
95+ Defines .PP_APP_ID , app_id , thread_trace_id )
96+
97+ pinpoint .add_trace_header (
98+ Defines .PP_SPAN_ID , seq_id , thread_trace_id )
99+ pinpoint .add_context (
100+ Defines .PP_SPAN_ID , seq_id , thread_trace_id )
101+
102+ pinpoint .add_trace_header (
103+ Defines .PP_TRANSCATION_ID , tid , thread_trace_id )
104+ pinpoint .add_context (
105+ Defines .PP_TRANSCATION_ID , tid , thread_trace_id )
106+
107+ pinpoint .add_trace_header (
108+ Defines .PP_SERVER_TYPE , Defines .PYTHON , thread_trace_id )
109+ pinpoint .set_async_context (
110+ thread_trace_id , async_id , sequence_id )
111+
112+ if callable (origin_target ):
113+ # todo add
114+ @PinpointCommonPlugin (origin_target .__name__ )
115+ def call_origin_target (* args , ** kwargs ):
116+ origin_target (* args , ** kwargs )
117+
118+ call_origin_target (* args , ** kwargs )
119+
120+ pinpoint .end_trace (thread_trace_id )
121+
122+ kwargs ['target' ] = pp_new_entry_func
123+
124+ return traceId , args , kwargs
125+
126+ def onEnd (self , traceId , ret ):
127+ super ().onEnd (traceId , ret )
128+ return ret
0 commit comments