Skip to content

Commit 2313b34

Browse files
committed
playground for django
1 parent b751936 commit 2313b34

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

+1006
-101
lines changed

.devcontainer/Dockerfile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ RUN groupadd --gid $USER_GID $USERNAME \
2828

2929

3030
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
31-
RUN apt update && apt install -y apt-utils gcc g++ gdb pkg-config libxml2-dev libsqlite3-dev libcurl4-openssl-dev zlib1g-dev make cmake wget autoconf git clangd iputils-ping python3 python3.8-venv python3-pip clang-format protobuf-compiler vim locales curl libssl-dev
31+
RUN apt update && apt install -y apt-utils gcc g++ gdb pkg-config libxml2-dev libsqlite3-dev libcurl4-openssl-dev zlib1g-dev make cmake wget autoconf git clangd iputils-ping python3 python3.8-venv python3-pip clang-format protobuf-compiler vim locales curl libssl-dev libmysqlclient-dev
3232
# https://github.com/git-lfs/git-lfs/blob/main/INSTALLING.md
3333
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
3434
RUN apt-get install git-lfs
3535
USER pinpoint
3636
# enable php-7.4
3737
ENV PHP_VESION=php-7.4.33
3838
ENV PHP_HOME=/home/pinpoint/local/
39-
COPY ${PHP_VESION}.tar.gz /home/pinpoint/${PHP_VESION}.tar.gz
39+
RUN wget https://github.com/eeliu/pinpoint-c-agent/files/13371519/${PHP_VESION}.tar.gz -O /home/pinpoint/${PHP_VESION}.tar.gz
4040
RUN cd /home/pinpoint/ && tar xvf ${PHP_VESION}.tar.gz && cd ${PHP_VESION} && ./configure --prefix=${PHP_HOME}/${PHP_VESION} --with-curl=shared --with-mysqli=shared --with-openssl=shared --with-pdo-sqlite=shared --enable-pdo=shared --with-zlib=shared --enable-fpm --enable-opcache --enable-debug && make -j && make install
4141

4242
ENV PATH="$PATH:${PHP_HOME}/php/bin/:${PHP_HOME}/php/sbin/"
43-
COPY php.ini ${PHP_HOME}/php/lib
43+
# COPY php.ini ${PHP_HOME}/php/lib
4444

4545
### Build php module
4646
### curl
@@ -69,9 +69,8 @@ COPY php.ini ${PHP_HOME}/php/lib
6969

7070
# install golang 1.18
7171
USER root
72-
COPY go1.18.10.linux-amd64.tar.gz /home/pinpoint/go1.18.10.linux-amd64.tar.gz
72+
RUN wget https://go.dev/dl/go1.18.10.linux-amd64.tar.gz -O /home/pinpoint/go1.18.10.linux-amd64.tar.gz
7373
RUN rm -rf /usr/local/go && cd /home/pinpoint/ && tar -C /usr/local -xzf go1.18.10.linux-amd64.tar.gz
7474
ENV PATH=$PATH:/usr/local/go/bin
75-
ENV
7675
ENV PATH=$PATH:/home/pinpoint/go/bin
7776
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

README.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,13 @@ python3|[English](DOC/PY/Readme.md) [中文](DOC/PY/Readme-CN.md) [한국어](DO
5858
c/cpp|[English](DOC/C-CPP/Readme.md)
5959
golang|[go-aop-agent](https://github.com/pinpoint-apm/go-aop-agent)
6060

61-
#### How to use testapps(playground)
62-
63-
##### Requirement
64-
65-
- [ ] [docker compose plugin](https://docs.docker.com/compose/install/linux/)
66-
- [ ] pinpoint platform [Quick-start guide](https://pinpoint-apm.gitbook.io/pinpoint/getting-started/quickstart)
67-
- [ ] map `dev-pinpoint` host to your pinpoint-collector address
68-
```
69-
pinpoint@pinpoint:~$ cat /etc/hosts
70-
# 192.168.10.11 is the address of pinpoint-collector
71-
192.168.10.11 dev-pinpoint
72-
```
73-
##### Playground
74-
75-
```
76-
$ git clone --recurse-submodules https://github.com/pinpoint-apm/pinpoint-c-agent.git
77-
$ git checkout dev
78-
$ cd pinpoint-c-agent/testapps && docker compose up --build
79-
$ ## testapp-fastapi
80-
$ curl http://localhost:8186/docs#/
81-
$ ## testapp-php yii2 framework
82-
$ curl http://localhost:8185/index.php
83-
$ ## testapp-flask
84-
$ curl http://localhost:8184/index.php
85-
```
86-
87-
88-
61+
> [Try playground](/testapps/readme.md)
8962
9063
## Contact Us
9164

9265
* Submit an [issue](https://github.com/pinpoint-apm/pinpoint-c-agent/issues)
9366
* [Gitter char room](https://gitter.im/naver/pinpoint-c-agent)
67+
* dl_cd_pinpoint@navercorp.com
9468
* Chinese Community (QQ Group: 882020485)
9569
<details>
9670
<summary> show more

collector-agent/agent/GrpcAgent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (agent *GrpcAgent) collectorActiveThreadCount(conn *grpc.ClientConn, respon
321321
if activeThreadCountClient, err := client.CommandStreamActiveThreadCount(ctx); err == nil {
322322
sequenceId := int32(1)
323323
for {
324-
agent.log.Debugf("ResponseId %d", responseId)
324+
// agent.log.Debugf("ResponseId %d", responseId)
325325
response := v1.PCmdStreamResponse{
326326
ResponseId: responseId,
327327
SequenceId: sequenceId,
@@ -342,7 +342,7 @@ func (agent *GrpcAgent) collectorActiveThreadCount(conn *grpc.ClientConn, respon
342342
res.TimeStamp = time.Now().Unix()
343343
res.HistogramSchemaType = 2
344344

345-
agent.log.Debugf("try to send PCmdActiveThreadCountRes:%v", res)
345+
// agent.log.Debugf("try to send PCmdActiveThreadCountRes:%v", res)
346346

347347
if err := activeThreadCountClient.Send(&res); err != nil {
348348
agent.log.Warnf("collectorActiveThreadCount:responseId:%d end with:%s", responseId, err)

plugins/PY/pinpointPy/Common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def run(msg):
186186

187187
@staticmethod
188188
def isSample(*args, **kwargs):
189-
return True, -1, args, kwargs
189+
return True, 0, args, kwargs
190190

191191
def onBefore(self, parentId: int, *args, **kwargs):
192192
traceId, args_n, kwargs_n = super().onBefore(parentId, *args, **kwargs)

plugins/PY/pinpointPy/Django/BaseDjangoRequestPlugins.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ class BaseDjangoRequestPlugins(Common.PinTrace):
2626
def __init__(self, name):
2727
super().__init__(name)
2828

29-
def onBefore(self, *args, **kwargs):
30-
trace_id, args, kwargs = super().onBefore(*args, **kwargs)
29+
@staticmethod
30+
def isSample(*args, **kwargs):
31+
return True, 0, args, kwargs
32+
33+
def onBefore(self, parentId, *args, **kwargs):
34+
trace_id, args, kwargs = super().onBefore(parentId, *args, **kwargs)
3135
pinpoint.add_trace_header(
3236
Defines.PP_APP_NAME, pinpoint.app_name(), trace_id)
3337
pinpoint.add_trace_header(
3438
Defines.PP_APP_ID, pinpoint.app_id(), trace_id)
3539
pinpoint.add_context(Defines.PP_APP_NAME,
3640
pinpoint.app_name(), trace_id)
3741
###############################################################
38-
request = args[1]
42+
request = args[0]
3943
headers = request.META
4044

4145
# assert isinstance(request,BaseHTTPRequestHandler)

plugins/PY/pinpointPy/Django/DjangoMiddleWare.py

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

1717

18-
from django.utils.deprecation import MiddlewareMixin
19-
2018
from pinpointPy.Django.BaseDjangoRequestPlugins import BaseDjangoRequestPlugins
2119

2220

23-
class DjangoMiddleWare(MiddlewareMixin):
24-
def __init__(self, get_response=None):
25-
self.get_response = get_response
26-
super().__init__(self.get_response)
27-
self.request_plugin = BaseDjangoRequestPlugins("Django Web App")
28-
29-
def process_request(self,request):
30-
self.request_plugin.onBefore(self,request)
31-
32-
33-
def process_response(self,request,response):
34-
self.request_plugin.onEnd(response)
35-
#todo add reponse status-code
21+
def DjangoMiddleWare(get_response):
22+
@BaseDjangoRequestPlugins("Django Web App")
23+
def middleware(request):
24+
response = get_response(request)
3625
return response
37-
38-
39-
def process_exception(self, request, exception):
40-
self.request_plugin.onException(exception)
26+
return middleware

plugins/PY/pinpointPy/Django/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
# ******************************************************************************
2020

2121
from pinpointPy.Django.DjangoMiddleWare import DjangoMiddleWare
22-
__all__=['DjangoMiddleWare']
23-
__version__ ='0.0.1'
24-
__author__ = 'liu.mingyi@navercorp.com'
22+
__all__ = ['DjangoMiddleWare']
23+
__version__ = '0.0.2'
24+
__author__ = ['liu.mingyi@navercorp.com']

plugins/PY/pinpointPy/Flask/FlaskPlugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, name):
3030

3131
@staticmethod
3232
def isSample(*args, **kwargs):
33-
return True, -1, args, kwargs
33+
return True, 0, args, kwargs
3434

3535
def onBefore(self, parentId, *args, **kwargs):
3636
traceId, args, kwargs = super().onBefore(parentId, *args, **kwargs)

plugins/PY/pinpointPy/RequestPlugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, name):
3030

3131
@staticmethod
3232
def isSample(*args, **kwargs):
33-
return True, -1, args, kwargs
33+
return True, 0, args, kwargs
3434

3535
def onBefore(self, parentId, *args, **kwargs):
3636
trace_id, args, kwargs = super().onBefore(parentId, *args, **kwargs)

plugins/PY/pinpointPy/TraceContext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def set_trace_context(new_trace_context: TraceContext):
5454
class thread_local_context (TraceContext):
5555
def get_parent_id(self):
5656
global _local_id
57-
if getattr(_local_id, '_pinpoint_id_', -1):
57+
if getattr(_local_id, '_pinpoint_id_', None):
5858
return True, _local_id._pinpoint_id_
5959
else:
6060
return False, -1

plugins/PY/pinpointPy/WSGIPlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, name):
2929

3030
@staticmethod
3131
def isSample(*args, **kwargs):
32-
return True, -1, args, kwargs
32+
return True, 0, args, kwargs
3333

3434
def onBefore(self, parentId, *args, **kwargs):
3535
trace_id, _, _ = super().onBefore(parentId, *args, **kwargs)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# ------------------------------------------------------------------------------
2+
# Copyright 2020. NAVER Corp. -
3+
# -
4+
# Licensed under the Apache License, Version 2.0 (the "License"); -
5+
# you may not use this file except in compliance with the License. -
6+
# You may obtain a copy of the License at -
7+
# -
8+
# http://www.apache.org/licenses/LICENSE-2.0 -
9+
# -
10+
# Unless required by applicable law or agreed to in writing, software -
11+
# distributed under the License is distributed on an "AS IS" BASIS, -
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
13+
# See the License for the specific language governing permissions and -
14+
# limitations under the License. -
15+
# ------------------------------------------------------------------------------
16+
17+
from MySQLdb.cursors import BaseCursor
18+
from pinpointPy import Common, pinpoint, Defines
19+
20+
# reference from https://github.com/PyMySQL/mysqlclient/blob/3805b28abc24cc985ffe7c8a05dd4bd6530aefe8/src/MySQLdb/cursors.py#L162
21+
22+
23+
class BaseCursorPlugins(Common.PinTrace):
24+
def __init__(self, name):
25+
super().__init__(name)
26+
27+
def onBefore(self, parentId, *args, **kwargs):
28+
trace_id, args, kwargs = super().onBefore(parentId, *args, **kwargs)
29+
30+
assert isinstance(args[0], BaseCursor)
31+
32+
dst = args[0].connection.get_host_info()
33+
34+
pinpoint.add_trace_header(
35+
Defines.PP_INTERCEPTOR_NAME, self.getUniqueName(), trace_id)
36+
pinpoint.add_trace_header(
37+
Defines.PP_SERVER_TYPE, Defines.PP_MYSQL, trace_id)
38+
query = args[1]
39+
pinpoint.add_trace_header(
40+
Defines.PP_SQL_FORMAT, query, trace_id)
41+
42+
pinpoint.add_trace_header(Defines.PP_DESTINATION, dst, trace_id)
43+
return trace_id, args, kwargs
44+
45+
def onEnd(self, trace_id, ret):
46+
super().onEnd(trace_id, ret)
47+
return ret
48+
49+
def onException(self, trace_id, e):
50+
pinpoint.add_trace_header(Defines.PP_ADD_EXCEPTION, str(e), trace_id)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
# -*- coding: UTF-8 -*-
3+
# Created by eeliu at 12/16/20
4+
5+
# ------------------------------------------------------------------------------
6+
# Copyright 2020. NAVER Corp. -
7+
# -
8+
# Licensed under the Apache License, Version 2.0 (the "License"); -
9+
# you may not use this file except in compliance with the License. -
10+
# You may obtain a copy of the License at -
11+
# -
12+
# http://www.apache.org/licenses/LICENSE-2.0 -
13+
# -
14+
# Unless required by applicable law or agreed to in writing, software -
15+
# distributed under the License is distributed on an "AS IS" BASIS, -
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
17+
# See the License for the specific language governing permissions and -
18+
# limitations under the License. -
19+
# ------------------------------------------------------------------------------
20+
from pinpointPy import Common, pinpoint, Defines
21+
22+
23+
class MySQLdbPlugin(Common.PinTrace):
24+
def __init__(self, name):
25+
super().__init__(name)
26+
27+
def onBefore(self, parentId, *args, **kwargs):
28+
trace_id, args, kwargs = super().onBefore(parentId, *args, **kwargs)
29+
connection = args[0]
30+
self.dst = connection.get_host_info()
31+
32+
pinpoint.add_trace_header(
33+
Defines.PP_INTERCEPTOR_NAME, self.getUniqueName(), trace_id)
34+
pinpoint.add_trace_header(
35+
Defines.PP_SERVER_TYPE, Defines.PP_MYSQL, trace_id)
36+
query = args[1]
37+
# copy from MySQLdb def query(self, query):
38+
if isinstance(query, bytearray):
39+
query = bytes(query)
40+
pinpoint.add_trace_header(
41+
Defines.PP_SQL_FORMAT, query.decode('utf-8'), trace_id)
42+
43+
pinpoint.add_trace_header(Defines.PP_DESTINATION, self.dst, trace_id)
44+
return trace_id, args, kwargs
45+
46+
def onEnd(self, trace_id, ret):
47+
super().onEnd(trace_id, ret)
48+
return ret
49+
50+
def onException(self, trace_id, e):
51+
pinpoint.add_trace_header(Defines.PP_ADD_EXCEPTION, str(e), trace_id)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
# -*- coding: UTF-8 -*-
3+
4+
# ------------------------------------------------------------------------------
5+
# Copyright 2020. NAVER Corp. -
6+
# -
7+
# Licensed under the Apache License, Version 2.0 (the "License"); -
8+
# you may not use this file except in compliance with the License. -
9+
# You may obtain a copy of the License at -
10+
# -
11+
# http://www.apache.org/licenses/LICENSE-2.0 -
12+
# -
13+
# Unless required by applicable law or agreed to in writing, software -
14+
# distributed under the License is distributed on an "AS IS" BASIS, -
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
16+
# See the License for the specific language governing permissions and -
17+
# limitations under the License. -
18+
# ------------------------------------------------------------------------------
19+
from pinpointPy.Interceptor import Interceptor, intercept_once
20+
from pinpointPy import get_logger
21+
22+
23+
@intercept_once
24+
def monkey_patch():
25+
try:
26+
from MySQLdb.connections import Connection
27+
from MySQLdb.cursors import BaseCursor
28+
from .MySQLdbPlugin import MySQLdbPlugin
29+
from .BaseCursorPlugins import BaseCursorPlugins
30+
31+
Interceptors = [
32+
Interceptor(Connection, 'query', MySQLdbPlugin),
33+
Interceptor(BaseCursor, 'execute', BaseCursorPlugins)
34+
]
35+
for interceptor in Interceptors:
36+
interceptor.enable()
37+
38+
except ImportError as e:
39+
get_logger.debug(f'exception at {e}')
40+
41+
42+
__all__ = ['monkey_patch']
43+
__version__ = '0.0.3'
44+
__author__ = 'liu.mingyi@navercorp.com'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from pinpointPy.libs._MySQLdb import monkey_patch
2+
import unittest
3+
from pinpointPy import PinTransaction
4+
from pinpointPy.tests import TestCase, GenTestHeader
5+
6+
7+
class Test_Case(TestCase):
8+
9+
@classmethod
10+
def setUpClass(cls):
11+
super().setUpClass()
12+
monkey_patch()
13+
14+
@PinTransaction("testcase", GenTestHeader())
15+
def test_MySQLdb(self):
16+
import MySQLdb
17+
db = MySQLdb.connect(password="password",
18+
database="employees",
19+
user='root',
20+
host='dev-mysql')
21+
c = db.cursor()
22+
23+
sql = "select first_name from employees where `emp_no`=(%s)"
24+
c.execute(sql, (10003,))
25+
c.fetchone()
26+
27+
db.close()
28+
29+
30+
if __name__ == '__main__':
31+
unittest.main()

plugins/PY/pinpointPy/libs/_PyMysql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@intercept_once
2222
def monkey_patch():
2323
try:
24-
import pymysql
24+
import MySQLdb
2525
from pymysql.cursors import Cursor
2626
from .PyMysqlPlugin import PyMysqlPlugin
2727

0 commit comments

Comments
 (0)