Skip to content

Commit

Permalink
build(testapps): playground for (#548)
Browse files Browse the repository at this point in the history
- add testapps-fastapi
  - redis/httpx/mysql
- change plugin style

close #547
  • Loading branch information
eeliu committed Mar 28, 2024
1 parent 4fdec47 commit bbae9a0
Show file tree
Hide file tree
Showing 57 changed files with 1,011 additions and 214 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ USER root
COPY go1.18.10.linux-amd64.tar.gz /home/pinpoint/go1.18.10.linux-amd64.tar.gz
RUN rm -rf /usr/local/go && cd /home/pinpoint/ && tar -C /usr/local -xzf go1.18.10.linux-amd64.tar.gz
ENV PATH=$PATH:/usr/local/go/bin
ENV PATH=$PATH:$GO_PATH/bin
ENV
ENV PATH=$PATH:/home/pinpoint/go/bin
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ testapps/PHP/composer.phar
wheelhouse/
.clangd
collector-agent/pinpoint-grpc-idl/
*.so
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "collector-agent/pinpoint-grpc-idl"]
path = collector-agent/pinpoint-grpc-idl
url = https://github.com/pinpoint-apm/pinpoint-grpc-idl.git
[submodule "testapps/test_db"]
path = testapps/test_db
url = https://github.com/datacharmer/test_db.git
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ python3|[English](DOC/PY/Readme.md) [中文](DOC/PY/Readme-CN.md) [한국어](DO
c/cpp|[English](DOC/C-CPP/Readme.md)
golang|[go-aop-agent](https://github.com/pinpoint-apm/go-aop-agent)

#### How to use testapps(playground)

##### Requirement

- [ ] [docker compose plugin](https://docs.docker.com/compose/install/linux/)
- [ ] pinpoint platform [Quick-start guide](https://pinpoint-apm.gitbook.io/pinpoint/getting-started/quickstart)
- [ ] map `dev-pinpoint` host to your pinpoint-collector address
```
pinpoint@pinpoint:~$ cat /etc/hosts
# 192.168.10.11 is the address of pinpoint-collector
192.168.10.11 dev-pinpoint
```
##### Playground
```
$ git clone --recurse-submodules https://github.com/pinpoint-apm/pinpoint-c-agent.git
$ git checkout dev
$ cd pinpoint-c-agent/testapps && docker compose up --build
$ ## testapp-fastapi
$ curl http://localhost:8186/docs#/
$ ## testapp-php yii2 framework
$ curl http://localhost:8185/index.php
$ ## testapp-flask
$ curl http://localhost:8184/index.php
```
## Contact Us
* Submit an [issue](https://github.com/pinpoint-apm/pinpoint-c-agent/issues)
Expand Down
2 changes: 1 addition & 1 deletion collector-agent/agent/AgentRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type TErrorInfo struct {
type TSpan struct {
AppServerType int32 `json:"FT"`
AppServerTypeV2 int32 `json:":FT"`
ParentAppServerType int `json:"ptype"`
ParentAppServerType int32 `json:"ptype,string"`
ParentSpanId int64 `json:"psid,string"`
ParentApplicationName string `json:"pname"`
StartTime int64 `json:"S"`
Expand Down
3 changes: 1 addition & 2 deletions collector-agent/agent/AgentRouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGetAgentInfo(t *testing.T) {
}

func TestTspan(t *testing.T) {
msg := `{"E":1,"FT":1500,":FT":1500,"ptype":1500,"pname":"abc_d","psid":"23563","NP":"t=1617083759.535 D=0.000","S":1617083759798,"appid":"app-2",":appid":"app-2",
msg := `{"E":1,"FT":1500,":FT":1500,"ptype":"1500","pname":"abc_d","psid":"23563","NP":"t=1617083759.535 D=0.000","S":1617083759798,"appid":"app-2",":appid":"app-2",
":appname":"APP-2","appname":"APP-2","calls":[{"E":1,"calls":[{"E":1,"S":0,"clues":["-1:input parameters","14:return value"],"name":"abc"}],"S":0,"clues":["-1:input parameters","14:return value"],"name":"app\\AppDate::abc","SQL":"select* from abc"}],"client":"10.34.135.145","clues":["46:200"],"name":"PHP Request: fpm-fcgi","server":"10.34.130.152:8000","sid":"726125302","stp":"1500","tid":"app-2^1617083747^5506","uri":"/index.php?type=get_date","Ah":"123.35.36.3/host","EXP":"exp","ERR":{"msg":"error_msg","file":"file.cc","line":123}}`
var tspan TSpan

Expand Down Expand Up @@ -79,5 +79,4 @@ func TestTspan(t *testing.T) {

}

t.Log(tspan)
}
2 changes: 1 addition & 1 deletion collector-agent/agent/SpanSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (spanSender *SpanSender) makePinpointSpan(span *TSpan) (*v1.PSpan, error) {

parentInfo := v1.PParentInfo{
ParentApplicationName: span.ParentApplicationName,
ParentApplicationType: int32(span.ParentAppServerType),
ParentApplicationType: span.ParentAppServerType,
AcceptorHost: span.AcceptorHost,
}

Expand Down
2 changes: 1 addition & 1 deletion collector-agent/start-collector-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export PP_COLLECTOR_AGENT_STAT_PORT=9992
export PP_COLLECTOR_AGENT_ISDOCKER=false
# export PP_LOG_DIR=/tmp/
export PP_Log_Level=DEBUG
export PP_ADDRESS=0.0.0.0@9999
export PP_ADDRESS=0.0.0.0@10000
export GO_PATH=/home/pinpoint/go/bin
export PATH=$PATH:$GO_PATH
make && ./collector-agent
43 changes: 33 additions & 10 deletions plugins/PY/pinpointPy/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,39 @@

# Created by eeliu at 3/5/20

from pinpointPy import Defines
from pinpointPy import pinpoint
from pinpointPy import Defines, pinpoint, logger
from abc import ABCMeta, abstractmethod

class PinTrace(object):
E_PER_REQ = 1
E_FUNCTION = 2

class Trace:
def __init__(self, name):
self.name = name

def onBefore(self, *args, **kwargs):
raise NotImplementedError("onBefore")

def onEnd(self, ret):
raise NotImplementedError("onEnd")

def __call__(self, func):

def pinpointTrace(*args, **kwargs):
try:
args, kwargs = self.onBefore(*args, **kwargs)
ret = func(*args, **kwargs)
except Exception as e:
self.onException(e)
logger.info(f"{func.__name__} catch {e}")
raise e
finally:
return self.onEnd(ret)
return pinpointTrace


class PinTrace(Trace):
E_PER_REQ = 1
E_FUNCTION = 2

def isSample(self, args):
'''
if not root, no trace
Expand All @@ -42,28 +64,27 @@ def isSample(self, args):

def onBefore(self, *args, **kwargs):
pinpoint.with_trace()
return args,kwargs
return args, kwargs

def onEnd(self, ret):
pinpoint.end_trace()

def onException(self, e):
raise NotImplementedError()
raise NotImplementedError("onException")

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

def pinpointTrace(*args, **kwargs):
if not self.isSample((args, kwargs)):
return func(*args, **kwargs)

ret = None
try:
args, kwargs = self.onBefore(*args, **kwargs)
ret = func(*args, **kwargs)
return ret
except Exception as e:
self.onException(e)
logger.info(f"{func.__name__} catch {e}")
raise e
finally:
self.onEnd(ret)
Expand All @@ -72,6 +93,7 @@ def pinpointTrace(*args, **kwargs):
def getFuncUniqueName(self):
return self.name


class PinHeader:
def __init__(self) -> None:
# Path field in pinpoint-web
Expand Down Expand Up @@ -117,7 +139,8 @@ def GetHeader(self,parenthost,parentname,url,...)->PinHeader:
@abstractmethod
def GetHeader(self, *args, **kwargs) -> PinHeader:
return PinHeader()



class PinTransaction(PinTrace):

def __init__(self, name: str, userGenHeaderCb: GenPinHeader):
Expand Down
16 changes: 8 additions & 8 deletions plugins/PY/pinpointPy/CommonPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
# ------------------------------------------------------------------------------


from pinpointPy import Common
from pinpointPy import Defines
from pinpointPy import pinpoint
from pinpointPy import Common, Defines, pinpoint


class PinpointCommonPlugin(Common.PinTrace):

def onBefore(self,*args, **kwargs):
def onBefore(self, *args, **kwargs):
super().onBefore(*args, **kwargs)
###############################################################
pinpoint.add_trace_header(Defines.PP_INTERCEPTOR_NAME, self.getFuncUniqueName())
pinpoint.add_trace_header(Defines.PP_SERVER_TYPE, Defines.PP_METHOD_CALL)
pinpoint.add_trace_header(
Defines.PP_INTERCEPTOR_NAME, self.getFuncUniqueName())
pinpoint.add_trace_header(
Defines.PP_SERVER_TYPE, Defines.PP_METHOD_CALL)
arg = self.get_arg(*args, **kwargs)
pinpoint.add_trace_header_v2(Defines.PP_ARGS, arg)
###############################################################
return args,kwargs
return args, kwargs

def onEnd(self,ret):
def onEnd(self, ret):
###############################################################
pinpoint.add_trace_header_v2(Defines.PP_RETURN, str(ret))
###############################################################
Expand Down
57 changes: 30 additions & 27 deletions plugins/PY/pinpointPy/Fastapi/AsyCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,52 @@
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------


import asyncio

from starlette_context import context

from pinpointPy import pinpoint


class AsynPinTrace(object):
class TraceContext:
@staticmethod
def get_parent_id():
id = context.get('_pinpoint_id_', 0)
if id == 0:
return False, None
else:
return True, id

@staticmethod
def set_parent_id(id: int):
context['_pinpoint_id_'] = id


class AsyncPinTrace:

def __init__(self, name):
self.name = name

def getCurrentId(self):
id = context['_pinpoint_id_']
if not id:
raise 'not found traceId'
else:
return id

def onBefore(self, parentId, *args, **kwargs):
traceId = pinpoint.with_trace(parentId)
# update global id
context['_pinpoint_id_'] = traceId
TraceContext.set_parent_id(traceId)
return traceId, args, kwargs

@staticmethod
def isSample(*args, **kwargs):
try:
parentid = context.get('_pinpoint_id_', 0)
if parentid == 0:
return False, None
return True, parentid
except Exception as e:
return False, None
ret, id = TraceContext.get_parent_id()
if ret:
return True, id, args, kwargs
else:
return False, None, args, kwargs

@classmethod
def _isSample(cls, *args, **kwargs):
return cls.isSample(*args, **kwargs)

def onEnd(self, parentId, ret):
parentId = pinpoint.end_trace(parentId)
context['_pinpoint_id_'] = parentId
TraceContext.set_parent_id(parentId)

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

async def pinpointTrace(*args, **kwargs):
ret = None
sampled, parentId = self._isSample(args, kwargs)
# avoiding variable missing
# use and return
sampled, parentId, nArgs, nKwargs = self._isSample(*args, **kwargs)
if not sampled:
return await func(*args, **kwargs)

traceId, args, kwargs = self.onBefore(parentId, *args, **kwargs)
return await func(*nArgs, **nKwargs)
traceId, nArgs, nKwargs = self.onBefore(
parentId, *nArgs, **nKwargs)
try:
ret = await func(*args, **kwargs)
ret = await func(*nArgs, **nKwargs)
return ret
except Exception as e:
self.onException(traceId, e)
raise e
finally:
self.onEnd(traceId, ret)

return pinpointTrace

def getFuncUniqueName(self):
Expand All @@ -91,7 +94,7 @@ def getFuncUniqueName(self):

if __name__ == '__main__':

@AsynPinTrace('main')
@AsyncPinTrace('main')
async def run(i):
if i == 0:
return
Expand Down
4 changes: 2 additions & 2 deletions plugins/PY/pinpointPy/Fastapi/AsyCommonPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# ------------------------------------------------------------------------------


from pinpointPy.Fastapi.AsyCommon import AsynPinTrace
from pinpointPy.Fastapi.AsyCommon import AsyncPinTrace
from pinpointPy import Defines
from pinpointPy import pinpoint


class CommonPlugin(AsynPinTrace):
class CommonPlugin(AsyncPinTrace):

def onBefore(self,parentId, *args, **kwargs):
traceId,args,kwargs = super().onBefore(parentId,*args, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions plugins/PY/pinpointPy/Fastapi/AsyRequestPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
# ------------------------------------------------------------------------------


from pinpointPy.Fastapi.AsyCommon import AsynPinTrace
from pinpointPy.Fastapi.AsyCommon import AsyncPinTrace
from pinpointPy import pinpoint
from pinpointPy import Defines


class AsyRequestPlugin(AsynPinTrace):
class AsyRequestPlugin(AsyncPinTrace):
def __init__(self, name):
super().__init__(name)

Expand Down
Loading

0 comments on commit bbae9a0

Please sign in to comment.