Skip to content

Commit

Permalink
fix(py): unwanted trace_id
Browse files Browse the repository at this point in the history
- fix collector-agent compiled problem
- patch for #626
  • Loading branch information
eeliu committed Jun 28, 2024
1 parent 2416b5a commit 3ad8f77
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 24 deletions.
5 changes: 3 additions & 2 deletions collector-agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM golang:1.18-alpine3.17 as builder
WORKDIR /apps
USER root
RUN apk add --no-cache make protobuf-dev
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
RUN apk add --no-cache make protobuf-dev git
RUN go install google.golang.org/protobuf/cmd/[email protected] && go install google.golang.org/grpc/cmd/[email protected]

COPY ./ /apps
RUN make

Expand Down
2 changes: 1 addition & 1 deletion collector-agent/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pinpoint-apm/pinpoint-c-agent/collector-agent

go 1.15
go 1.18

require (
github.com/golang/protobuf v1.5.3
Expand Down
3 changes: 1 addition & 2 deletions plugins/PY/pinpointPy/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _isSample(cls, *args, **kwargs):
return cls.isSample(*args, **kwargs)

def onEnd(self, traceId, ret):
parentId: int = pinpoint.end_trace(trace_id=traceId)
parentId = pinpoint.end_trace(traceId)
get_trace_context().set_parent_id(parentId)

def onException(self, traceId, e):
Expand Down Expand Up @@ -246,7 +246,6 @@ def onBefore(self, parentId: int, *args, **kwargs):
if header.ParentTid != '':
tid = header.ParentTid
pinpoint.add_trace_header(Defines.PP_PARENT_SPAN_ID, tid, traceId)
pinpoint.add_trace_header(Defines.PP_NEXT_SPAN_ID, sid, traceId)
else:
tid = pinpoint.gen_tid()

Expand Down
2 changes: 0 additions & 2 deletions plugins/PY/pinpointPy/Fastapi/PinTranscation.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ def onBefore(self, parentId, *args, **kwargs):
if header.ParentTid != '':
tid = header.ParentTid
pinpoint.add_trace_header(Defines.PP_PARENT_SPAN_ID, tid, traceId)
# nsid = pinpoint.gen_sid()
pinpoint.add_trace_header(Defines.PP_NEXT_SPAN_ID, sid, traceId)
else:
tid = pinpoint.gen_tid()

Expand Down
2 changes: 1 addition & 1 deletion plugins/PY/pinpointPy/RequestPlugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def onBefore(self, parentId, *args, **kwargs):
request = args[0]
###############################################################
pinpoint.add_trace_header(
Defines.PP_INTERCEPTOR_NAME, 'BaseFlaskrequest', trace_id)
Defines.PP_INTERCEPTOR_NAME, 'BaseFlaskRequest', trace_id)
pinpoint.add_trace_header(Defines.PP_REQ_URI, request.path, trace_id)
pinpoint.add_trace_header(
Defines.PP_REQ_CLIENT, request.remote_addr, trace_id, trace_id)
Expand Down
3 changes: 2 additions & 1 deletion plugins/PY/pinpointPy/TraceContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def set_trace_context(new_trace_context: TraceContext):
class thread_local_context (TraceContext):
def get_parent_id(self):
global _local_id
if getattr(_local_id, '_pinpoint_id_', None):
# fix bug: -1 break _pinpoint_id_ rule
if getattr(_local_id, '_pinpoint_id_', 0) > 0:
return True, _local_id._pinpoint_id_
else:
return False, -1
Expand Down
2 changes: 1 addition & 1 deletion plugins/PY/pinpointPy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def use_thread_local_context():

__all__ = ['monkey_patch_for_pinpoint', 'use_thread_local_context'
'set_agent', 'app_id', 'app_name', 'gen_tid', 'get_logger', 'PinTransaction', 'GenPinHeader', 'PinHeader']
__version__ = "1.1.2"
__version__ = "1.3.1"
__author__ = '[email protected]'
7 changes: 5 additions & 2 deletions plugins/PY/pinpointPy/libs/_requests/NextSpanPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ def onBefore(self, parentId, *args, **kwargs):

def onEnd(self, traceId, ret):
###############################################################
pinpoint.add_trace_header(Defines.PP_NEXT_SPAN_ID, pinpoint.get_context(
Defines.PP_NEXT_SPAN_ID, traceId), traceId)
nextSpan = pinpoint.get_context(
Defines.PP_NEXT_SPAN_ID, traceId)
if nextSpan:
pinpoint.add_trace_header(
Defines.PP_NEXT_SPAN_ID, nextSpan, traceId)
if ret:
pinpoint.add_trace_header_v2(
Defines.PP_HTTP_STATUS_CODE, str(ret.status_code), traceId)
Expand Down
6 changes: 5 additions & 1 deletion plugins/PY/pinpointPy/libs/_requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ def monkey_patch():

__all__ = ['monkey_patch']

__version__ = '0.0.4'
__version__ = '0.0.5'
__author__ = '[email protected]'

# changes
# 0.0.5
# - fix https://github.com/pinpoint-apm/pinpoint-c-agent/issues/626
90 changes: 89 additions & 1 deletion plugins/PY/pinpointPy/libs/_requests/test_case.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from pinpointPy.tests import TestCase
import unittest
from pinpointPy import PinTransaction, Defines
from pinpointPy import PinTransaction, Defines, GenPinHeader, PinHeader
from pinpointPy.tests import GenTestHeader, create_http_bin_response
from pinpointPy.libs._requests import monkey_patch


class TestHeader(GenPinHeader):
def GetHeader(self, *args, **kwargs) -> PinHeader:
_header = PinHeader()
_header.Url = "/test"
_header.Host = "127.0.0.1"
_header.RemoteAddr = "127.0.0.1"
_header.ParentType = "1700"
_header.ParentName = "parent"
_header.ParentHost = "127.0.0.1"
return _header


class Test_Case(TestCase):

@classmethod
Expand All @@ -21,6 +33,17 @@ def test_post(self):
httpbin = create_http_bin_response(body.text)
self.assertIn(Defines.PP_HEADER_PINPOINT_SPANID, httpbin.headers)

body = requests.post(url, json=proto)
httpbin = create_http_bin_response(body.text)
self.assertIn(Defines.PP_HEADER_PINPOINT_SPANID, httpbin.headers)

body = requests.post(url, data={
'a': 12,
'b': 'sdf'
})
httpbin = create_http_bin_response(body.text)
self.assertIn(Defines.PP_HEADER_PINPOINT_SPANID, httpbin.headers)

@PinTransaction("testcase", GenTestHeader())
def test_get(self):
import requests
Expand All @@ -42,6 +65,71 @@ def test_request(self):
httpbin = create_http_bin_response(body.text)
self.assertIn(Defines.PP_HEADER_PINPOINT_SPANID, httpbin.headers)

def test_request_no_transaction(self):
import requests
from threading import Thread
from multiprocessing import Process

from pinpointPy.TraceContext import get_trace_context

def threaded_function():
sample, _ = get_trace_context().get_parent_id()
self.assertFalse(sample)
body = requests.request(
"POST", 'http://httpbin/anything/threaded_function', data='abc')
httpbin = create_http_bin_response(body.text)
print(httpbin.headers)

def process_function():
sample, traceId = get_trace_context().get_parent_id()
# self.assertFalse(sample)
print(f'process_function:{sample},{traceId}')
body = requests.request(
"POST", 'http://httpbin/anything/threaded_function', data='abc')
httpbin = create_http_bin_response(body.text)

# body = requests.request(
# "POST", 'xxx', data='abc')
# httpbin = create_http_bin_response(body.text)

sample, traceId = get_trace_context().get_parent_id()
# self.assertFalse(sample)
print(f'process_function:{sample},{traceId}')
print(httpbin.headers)

@PinTransaction("testcase", TestHeader())
def test_body():

body = requests.request(
"POST", 'http://httpbin/anything', data={'abc': 'sdf'})
httpbin = create_http_bin_response(body.text)

body = requests.request(
"POST", 'http://httpbin/anything', data='abc')
httpbin = create_http_bin_response(body.text)
print(httpbin.headers)
thread = Thread(target=threaded_function)
thread.start()
thread.join()
sample, _ = get_trace_context().get_parent_id()
self.assertTrue(sample)
p = Process(target=process_function)
p.start()
p.join()

test_body()

sample, parentId = get_trace_context().get_parent_id()
print(f'{sample},{parentId}')
requests.request(
"POST", 'http://httpbin/anything', data='abc')
sample, parentId = get_trace_context().get_parent_id()
requests.request(
"POST", 'http://httpbin/anything', data='abc')
sample, parentId = get_trace_context().get_parent_id()
print(f'{sample}, {parentId}')
self.assertFalse(sample)


if __name__ == '__main__':
unittest.main()
6 changes: 5 additions & 1 deletion plugins/PY/pinpointPy/libs/_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ def monkey_patch():

__all__ = ['monkey_patch']

__version__ = '0.0.3'
__version__ = '0.0.4'
__author__ = '[email protected]'

# changes
# 0.0.4
# - fix https://github.com/pinpoint-apm/pinpoint-c-agent/issues/626
2 changes: 0 additions & 2 deletions plugins/PY/pinpointPy/libs/_sqlalchemy/sqlalchemyPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
from urllib.parse import urlparse

from pinpointPy.Common import Trace, PinTrace
from pinpointPy.Fastapi.AsyCommon import AsyncPinTrace
from pinpointPy.TraceContext import _reqTraceCtx


class CreateEnginePlugin(Trace):
Expand Down
7 changes: 4 additions & 3 deletions plugins/PY/pinpointPy/libs/_urllib/RequestPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ def onBefore(self, parentId, *args, **kwargs):

def onEnd(self, traceId, ret):
###############################################################

pinpoint.add_trace_header(
Defines.PP_NEXT_SPAN_ID, pinpoint.get_context(Defines.PP_NEXT_SPAN_ID, traceId), traceId)
nextSpan = pinpoint.get_context(Defines.PP_NEXT_SPAN_ID, traceId)
if nextSpan:
pinpoint.add_trace_header(
Defines.PP_NEXT_SPAN_ID, nextSpan, traceId)
# pinpoint.add_trace_header_v2(
# Defines.PP_HTTP_STATUS_CODE, str(ret.status_code), traceId)
pinpoint.add_trace_header_v2(Defines.PP_RETURN, str(ret), traceId)
Expand Down
6 changes: 5 additions & 1 deletion plugins/PY/pinpointPy/libs/_urllib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ def monkey_patch():

__all__ = ['monkey_patch']

__version__ = '0.0.5'
__version__ = '0.0.6'
__author__ = '[email protected]'

# changes
# 0.0.6
# - fix https://github.com/pinpoint-apm/pinpoint-c-agent/issues/626
2 changes: 1 addition & 1 deletion plugins/PY/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bottle==0.12.25
fastapi==0.109.1
fastapi==0.111.0
flask==3.0.0
httpx==0.25.1
mysql-connector-python==8.0.31
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
Path(cwd, './common/src')]

setup(name='pinpointPy',
version="1.2.1", # don't forget update __version__ in pinpointPy/__init__.py
version="1.3.1", # don't forget update __version__ in pinpointPy/__init__.py
author="cd_pinpoint members",
author_email='[email protected]',
license='Apache License 2.0',
Expand All @@ -61,6 +61,8 @@

"""
# Changed
## 1.3.1
- fix bug https://github.com/pinpoint-apm/pinpoint-c-agent/issues/626
## 1.3.0
- support error analysis
## 1.2.1
Expand Down
2 changes: 1 addition & 1 deletion setup_pypi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
Path(cwd, './common/src')]

setup(name='pinpointPy',
version="1.3.5", # don't forget update __version__ in pinpointPy/__init__.py
version="1.3.6", # don't forget update __version__ in pinpointPy/__init__.py
author="cd_pinpoint members",
author_email='[email protected]',
license='Apache License 2.0',
Expand Down

0 comments on commit 3ad8f77

Please sign in to comment.