Skip to content

Commit dfe64d1

Browse files
committed
build(testapps): playground for
- add testapps-fastapi - redis/httpx/mysql - change plugin style close #547
1 parent be96657 commit dfe64d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+686
-208
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ testapps/PHP/composer.phar
7575
wheelhouse/
7676
.clangd
7777
collector-agent/pinpoint-grpc-idl/
78+
*.so

collector-agent/start-collector-agent.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export PP_COLLECTOR_AGENT_STAT_PORT=9992
88
export PP_COLLECTOR_AGENT_ISDOCKER=false
99
# export PP_LOG_DIR=/tmp/
1010
export PP_Log_Level=DEBUG
11-
export PP_ADDRESS=0.0.0.0@9999
11+
export PP_ADDRESS=0.0.0.0@10000
1212
export GO_PATH=/home/pinpoint/go/bin
1313
export PATH=$PATH:$GO_PATH
1414
make && ./collector-agent

plugins/PY/pinpointPy/Common.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,39 @@
1919

2020
# Created by eeliu at 3/5/20
2121

22-
from pinpointPy import Defines
23-
from pinpointPy import pinpoint
22+
from pinpointPy import Defines, pinpoint, logger
2423
from abc import ABCMeta, abstractmethod
2524

26-
class PinTrace(object):
27-
E_PER_REQ = 1
28-
E_FUNCTION = 2
2925

26+
class Trace:
3027
def __init__(self, name):
3128
self.name = name
3229

30+
def onBefore(self, *args, **kwargs):
31+
raise NotImplementedError("onBefore")
32+
33+
def onEnd(self, ret):
34+
raise NotImplementedError("onEnd")
35+
36+
def __call__(self, func):
37+
38+
def pinpointTrace(*args, **kwargs):
39+
try:
40+
args, kwargs = self.onBefore(*args, **kwargs)
41+
ret = func(*args, **kwargs)
42+
except Exception as e:
43+
self.onException(e)
44+
logger.info(f"{func.__name__} catch {e}")
45+
raise e
46+
finally:
47+
return self.onEnd(ret)
48+
return pinpointTrace
49+
50+
51+
class PinTrace(Trace):
52+
E_PER_REQ = 1
53+
E_FUNCTION = 2
54+
3355
def isSample(self, args):
3456
'''
3557
if not root, no trace
@@ -42,28 +64,27 @@ def isSample(self, args):
4264

4365
def onBefore(self, *args, **kwargs):
4466
pinpoint.with_trace()
45-
return args,kwargs
67+
return args, kwargs
4668

4769
def onEnd(self, ret):
4870
pinpoint.end_trace()
4971

5072
def onException(self, e):
51-
raise NotImplementedError()
73+
raise NotImplementedError("onException")
5274

5375
def __call__(self, func):
5476
self.func_name = func.__name__
5577

5678
def pinpointTrace(*args, **kwargs):
5779
if not self.isSample((args, kwargs)):
5880
return func(*args, **kwargs)
59-
60-
ret = None
6181
try:
6282
args, kwargs = self.onBefore(*args, **kwargs)
6383
ret = func(*args, **kwargs)
6484
return ret
6585
except Exception as e:
6686
self.onException(e)
87+
logger.info(f"{func.__name__} catch {e}")
6788
raise e
6889
finally:
6990
self.onEnd(ret)
@@ -72,6 +93,7 @@ def pinpointTrace(*args, **kwargs):
7293
def getFuncUniqueName(self):
7394
return self.name
7495

96+
7597
class PinHeader:
7698
def __init__(self) -> None:
7799
# Path field in pinpoint-web
@@ -117,7 +139,8 @@ def GetHeader(self,parenthost,parentname,url,...)->PinHeader:
117139
@abstractmethod
118140
def GetHeader(self, *args, **kwargs) -> PinHeader:
119141
return PinHeader()
120-
142+
143+
121144
class PinTransaction(PinTrace):
122145

123146
def __init__(self, name: str, userGenHeaderCb: GenPinHeader):

plugins/PY/pinpointPy/CommonPlugin.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
# ------------------------------------------------------------------------------
1616

1717

18-
from pinpointPy import Common
19-
from pinpointPy import Defines
20-
from pinpointPy import pinpoint
18+
from pinpointPy import Common, Defines, pinpoint
2119

2220

2321
class PinpointCommonPlugin(Common.PinTrace):
2422

25-
def onBefore(self,*args, **kwargs):
23+
def onBefore(self, *args, **kwargs):
2624
super().onBefore(*args, **kwargs)
2725
###############################################################
28-
pinpoint.add_trace_header(Defines.PP_INTERCEPTOR_NAME, self.getFuncUniqueName())
29-
pinpoint.add_trace_header(Defines.PP_SERVER_TYPE, Defines.PP_METHOD_CALL)
26+
pinpoint.add_trace_header(
27+
Defines.PP_INTERCEPTOR_NAME, self.getFuncUniqueName())
28+
pinpoint.add_trace_header(
29+
Defines.PP_SERVER_TYPE, Defines.PP_METHOD_CALL)
3030
arg = self.get_arg(*args, **kwargs)
3131
pinpoint.add_trace_header_v2(Defines.PP_ARGS, arg)
3232
###############################################################
33-
return args,kwargs
33+
return args, kwargs
3434

35-
def onEnd(self,ret):
35+
def onEnd(self, ret):
3636
###############################################################
3737
pinpoint.add_trace_header_v2(Defines.PP_RETURN, str(ret))
3838
###############################################################

plugins/PY/pinpointPy/Fastapi/AsyCommon.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,52 @@
1616
# See the License for the specific language governing permissions and -
1717
# limitations under the License. -
1818
# ------------------------------------------------------------------------------
19-
20-
2119
import asyncio
2220

2321
from starlette_context import context
2422

2523
from pinpointPy import pinpoint
2624

2725

28-
class AsynPinTrace(object):
26+
class TraceContext:
27+
@staticmethod
28+
def get_parent_id():
29+
id = context.get('_pinpoint_id_', 0)
30+
if id == 0:
31+
return False, None
32+
else:
33+
return True, id
34+
35+
@staticmethod
36+
def set_parent_id(id: int):
37+
context['_pinpoint_id_'] = id
38+
39+
40+
class AsyncPinTrace:
2941

3042
def __init__(self, name):
3143
self.name = name
3244

33-
def getCurrentId(self):
34-
id = context['_pinpoint_id_']
35-
if not id:
36-
raise 'not found traceId'
37-
else:
38-
return id
39-
4045
def onBefore(self, parentId, *args, **kwargs):
4146
traceId = pinpoint.with_trace(parentId)
42-
# update global id
43-
context['_pinpoint_id_'] = traceId
47+
TraceContext.set_parent_id(traceId)
4448
return traceId, args, kwargs
4549

4650
@staticmethod
4751
def isSample(*args, **kwargs):
48-
try:
49-
parentid = context.get('_pinpoint_id_', 0)
50-
if parentid == 0:
51-
return False, None
52-
return True, parentid
53-
except Exception as e:
54-
return False, None
52+
ret, id = TraceContext.get_parent_id()
53+
if ret:
54+
return True, id, args, kwargs
55+
else:
56+
return False, None, args, kwargs
5557

5658
@classmethod
5759
def _isSample(cls, *args, **kwargs):
5860
return cls.isSample(*args, **kwargs)
5961

6062
def onEnd(self, parentId, ret):
6163
parentId = pinpoint.end_trace(parentId)
62-
context['_pinpoint_id_'] = parentId
64+
TraceContext.set_parent_id(parentId)
6365

6466
def onException(self, traceId, e):
6567
raise NotImplementedError()
@@ -69,20 +71,21 @@ def __call__(self, func):
6971

7072
async def pinpointTrace(*args, **kwargs):
7173
ret = None
72-
sampled, parentId = self._isSample(args, kwargs)
74+
# avoiding variable missing
75+
# use and return
76+
sampled, parentId, nArgs, nKwargs = self._isSample(*args, **kwargs)
7377
if not sampled:
74-
return await func(*args, **kwargs)
75-
76-
traceId, args, kwargs = self.onBefore(parentId, *args, **kwargs)
78+
return await func(*nArgs, **nKwargs)
79+
traceId, nArgs, nKwargs = self.onBefore(
80+
parentId, *nArgs, **nKwargs)
7781
try:
78-
ret = await func(*args, **kwargs)
82+
ret = await func(*nArgs, **nKwargs)
7983
return ret
8084
except Exception as e:
8185
self.onException(traceId, e)
8286
raise e
8387
finally:
8488
self.onEnd(traceId, ret)
85-
8689
return pinpointTrace
8790

8891
def getFuncUniqueName(self):
@@ -91,7 +94,7 @@ def getFuncUniqueName(self):
9194

9295
if __name__ == '__main__':
9396

94-
@AsynPinTrace('main')
97+
@AsyncPinTrace('main')
9598
async def run(i):
9699
if i == 0:
97100
return

plugins/PY/pinpointPy/Fastapi/AsyCommonPlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
# ------------------------------------------------------------------------------
1616

1717

18-
from pinpointPy.Fastapi.AsyCommon import AsynPinTrace
18+
from pinpointPy.Fastapi.AsyCommon import AsyncPinTrace
1919
from pinpointPy import Defines
2020
from pinpointPy import pinpoint
2121

2222

23-
class CommonPlugin(AsynPinTrace):
23+
class CommonPlugin(AsyncPinTrace):
2424

2525
def onBefore(self,parentId, *args, **kwargs):
2626
traceId,args,kwargs = super().onBefore(parentId,*args, **kwargs)

plugins/PY/pinpointPy/Fastapi/AsyRequestPlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
# ------------------------------------------------------------------------------
1919

2020

21-
from pinpointPy.Fastapi.AsyCommon import AsynPinTrace
21+
from pinpointPy.Fastapi.AsyCommon import AsyncPinTrace
2222
from pinpointPy import pinpoint
2323
from pinpointPy import Defines
2424

2525

26-
class AsyRequestPlugin(AsynPinTrace):
26+
class AsyRequestPlugin(AsyncPinTrace):
2727
def __init__(self, name):
2828
super().__init__(name)
2929

plugins/PY/pinpointPy/Fastapi/FastAPIRequestPlugin.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pinpointPy.Fastapi.AsyRequestPlugin import AsyRequestPlugin
2222
from pinpointPy import Defines, pinpoint
2323
import sys
24+
from fastapi import Response
2425

2526

2627
class FastAPIRequestPlugin(AsyRequestPlugin):
@@ -40,11 +41,24 @@ def onBefore(self, parentId, *args, **kwargs):
4041
self.request = request
4142
return traceId, args, kwargs
4243

43-
def onEnd(self, traceId, response):
44-
ut = self.request.scope['root_path'] + self.request.scope['route'].path
44+
def onEnd(self, traceId, response: Response):
45+
# fix bug in in fastapi/docs
46+
ut = '/'
47+
if 'root_path' in self.request.scope:
48+
ut += self.request.scope['root_path']
49+
if 'route' in self.request.scope:
50+
ut += self.request.scope['route'].path
51+
4552
pinpoint.add_trace_header(Defines.PP_URL_TEMPLATED, ut, traceId)
4653

4754
if 'unittest' in sys.modules.keys():
4855
response.headers["UT"] = ut
4956

57+
if response:
58+
pinpoint.add_trace_header_v2(Defines.PP_HTTP_STATUS_CODE, str(
59+
response.status_code), traceId)
60+
if response.status_code >= 400:
61+
pinpoint.mark_as_error(
62+
f'status_code:{response.status_code}', 'FastAPIRequestPlugin', 0, traceId)
63+
5064
return super().onEnd(traceId, response)

plugins/PY/pinpointPy/Fastapi/PinTranscation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# limitations under the License. -
1818
# ------------------------------------------------------------------------------
1919

20-
from pinpointPy.Fastapi.AsyCommon import AsynPinTrace
20+
from pinpointPy.Fastapi.AsyCommon import AsyncPinTrace
2121
from pinpointPy import Defines,pinpoint
2222
from pinpointPy.Common import GenPinHeader
2323
from starlette_context import request_cycle_context
@@ -36,7 +36,7 @@ async def pinpointTrace(*args, **kwargs):
3636
return pinpointTrace
3737

3838

39-
class PinTransaction(AsynPinTrace):
39+
class PinTransaction(AsyncPinTrace):
4040

4141
def __init__(self, name: str, userGenHeaderCb: GenPinHeader):
4242
"""pinpointPy user entry point

plugins/PY/pinpointPy/Fastapi/_MotorMongo/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
# create by eelu
2121

2222
from pinpointPy.Interceptor import intercept_once
23+
from pinpointPy import logger
2324

2425

2526
@intercept_once
2627
def monkey_patch():
2728
try:
2829
from pymongo import monitoring
29-
from .motorComandPlugins import CommandLogger
30+
from .motorCommandPlugins import CommandLogger
3031
monitoring.register(CommandLogger())
3132
except ImportError as e:
3233
# do nothing
33-
print(e)
34+
logger.debug(f"not found pymongo. {e}")
3435

3536

3637
__all__ = ['monkey_patch']

0 commit comments

Comments
 (0)