Skip to content

Commit c733985

Browse files
[Feature] Process propagation (#67)
Co-authored-by: kezhenxu94 <kezhenxu94@163.com>
1 parent 21011a9 commit c733985

13 files changed

Lines changed: 448 additions & 8 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ from time import sleep
8888
from skywalking import Component
8989
from skywalking.decorators import trace, runnable
9090
from skywalking.trace.context import SpanContext, get_context
91+
from skywalking.trace.ipc.process import SwProcess
9192

9293
@trace() # the operation name is the method name('some_other_method') by default
9394
def some_other_method():
@@ -103,10 +104,16 @@ def some_method():
103104
def some_method():
104105
some_other_method()
105106

106-
from threading import Thread
107+
from threading import Thread
107108
t = Thread(target=some_method)
108109
t.start()
109110

111+
# When another process is started, agents will also be started in other processes,
112+
# supporting only the process mode of spawn.
113+
p1 = SwProcess(target=some_method)
114+
p1.start()
115+
p1.join()
116+
110117

111118
context: SpanContext = get_context()
112119
with context.new_entry_span(op=str('https://github.com/apache/skywalking')) as span:

skywalking/agent/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def stop():
8282
__finished.set()
8383

8484

85+
def started():
86+
return __started
87+
88+
8589
def connected():
8690
return __protocol.connected()
8791

skywalking/config.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
17+
import inspect
1818
import os
1919
import uuid
20-
from typing import List
20+
from typing import TYPE_CHECKING
21+
22+
if TYPE_CHECKING:
23+
from typing import List
2124

2225
service_name = os.getenv('SW_AGENT_NAME') or 'Python Service Name' # type: str
2326
service_instance = os.getenv('SW_AGENT_INSTANCE') or str(uuid.uuid1()).replace('-', '') # type: str
@@ -35,17 +38,17 @@
3538
ignore_suffix = os.getenv('SW_IGNORE_SUFFIX') or '.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,' \
3639
'.mp4,.html,.svg ' # type: str
3740
flask_collect_http_params = True if os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') and \
38-
os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') == 'True' else False # type: bool
41+
os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') == 'True' else False # type: bool
3942
http_params_length_threshold = int(os.getenv('SW_HTTP_PARAMS_LENGTH_THRESHOLD') or '1024') # type: int
4043
django_collect_http_params = True if os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') and \
41-
os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') == 'True' else False # type: bool
44+
os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') == 'True' else False # type: bool
4245
correlation_element_max_number = int(os.getenv('SW_CORRELATION_ELEMENT_MAX_NUMBER') or '3') # type: int
4346
correlation_value_max_length = int(os.getenv('SW_CORRELATION_VALUE_MAX_LENGTH') or '128') # type: int
4447
trace_ignore = True if os.getenv('SW_TRACE_IGNORE') and \
45-
os.getenv('SW_TRACE_IGNORE') == 'True' else False # type: bool
48+
os.getenv('SW_TRACE_IGNORE') == 'True' else False # type: bool
4649
trace_ignore_path = (os.getenv('SW_TRACE_IGNORE_PATH') or '').split(',') # type: List[str]
4750
elasticsearch_trace_dsl = True if os.getenv('SW_ELASTICSEARCH_TRACE_DSL') and \
48-
os.getenv('SW_ELASTICSEARCH_TRACE_DSL') == 'True' else False # type: bool
51+
os.getenv('SW_ELASTICSEARCH_TRACE_DSL') == 'True' else False # type: bool
4952

5053

5154
def init(
@@ -69,3 +72,23 @@ def init(
6972

7073
global authentication
7174
authentication = token or authentication
75+
76+
77+
def serialize():
78+
from skywalking import config
79+
return {
80+
key: value for key, value in config.__dict__.items() if not (
81+
key.startswith('_') or key == 'TYPE_CHECKING'
82+
or inspect.isfunction(value)
83+
or inspect.ismodule(value)
84+
or inspect.isbuiltin(value)
85+
or inspect.isclass(value)
86+
)
87+
}
88+
89+
90+
def deserialize(data):
91+
from skywalking import config
92+
for key, value in data.items():
93+
if key in config.__dict__:
94+
config.__dict__[key] = value

skywalking/trace/ipc/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#

skywalking/trace/ipc/process.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
from multiprocessing import Process
19+
20+
from skywalking import config, agent
21+
22+
23+
class SwProcess(Process):
24+
25+
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, *,
26+
daemon=None):
27+
super(SwProcess, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs, daemon=daemon)
28+
self._sw_config = config.serialize()
29+
30+
def run(self):
31+
if agent.started() is False:
32+
config.deserialize(self._sw_config)
33+
agent.start()
34+
super(SwProcess, self).run()

tests/plugin/docker/Dockerfile.tool

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ FROM openjdk:8
1919

2020
WORKDIR /tests
2121

22-
ARG COMMIT_HASH=3c9d7099f05dc4a4b937c8a47506e56c130b6221
22+
ARG COMMIT_HASH=8a48c49b4420df5c9576d2aea178b2ebcb7ecd09
2323

2424
ADD https://github.com/apache/skywalking-agent-test-tool/archive/${COMMIT_HASH}.tar.gz .
2525

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
version: '2.1'
19+
20+
services:
21+
collector:
22+
extends:
23+
service: collector
24+
file: ../docker/docker-compose.base.yml
25+
26+
provider:
27+
extends:
28+
service: agent
29+
file: ../docker/docker-compose.base.yml
30+
ports:
31+
- 9091:9091
32+
volumes:
33+
- ./services/provider.py:/app/provider.py
34+
command: ['bash', '-c', 'pip install flask && python3 /app/provider.py']
35+
depends_on:
36+
collector:
37+
condition: service_healthy
38+
healthcheck:
39+
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9091"]
40+
interval: 5s
41+
timeout: 60s
42+
retries: 120
43+
44+
consumer:
45+
extends:
46+
service: agent
47+
file: ../docker/docker-compose.base.yml
48+
ports:
49+
- 9090:9090
50+
volumes:
51+
- ./services/consumer.py:/app/consumer.py
52+
command: ['bash', '-c', 'pip install flask && python3 /app/consumer.py']
53+
depends_on:
54+
collector:
55+
condition: service_healthy
56+
provider:
57+
condition: service_healthy
58+
59+
networks:
60+
beyond:
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
segmentItems:
19+
- serviceName: provider
20+
segmentSize: 2
21+
segments:
22+
- segmentId: not null
23+
spans:
24+
- operationName: /users
25+
operationId: 0
26+
parentSpanId: -1
27+
spanId: 0
28+
spanLayer: Http
29+
tags:
30+
- key: http.method
31+
value: POST
32+
- key: url
33+
value: http://provider:9091/users
34+
- key: status.code
35+
value: '200'
36+
refs:
37+
- parentEndpoint: /users
38+
networkAddress: 'provider:9091'
39+
refType: CrossProcess
40+
parentSpanId: 0
41+
parentTraceSegmentId: not null
42+
parentServiceInstance: not null
43+
parentService: consumer
44+
traceId: not null
45+
startTime: gt 0
46+
endTime: gt 0
47+
componentId: 7001
48+
spanType: Entry
49+
peer: not null
50+
skipAnalysis: false
51+
- segmentId: not null
52+
spans:
53+
- operationName: /users
54+
operationId: 0
55+
parentSpanId: -1
56+
spanId: 0
57+
spanLayer: Http
58+
tags:
59+
- key: http.method
60+
value: POST
61+
- key: url
62+
value: http://provider:9091/users
63+
- key: status.code
64+
value: '200'
65+
refs:
66+
- parentEndpoint: /users
67+
networkAddress: 'provider:9091'
68+
refType: CrossProcess
69+
parentSpanId: 1
70+
parentTraceSegmentId: not null
71+
parentServiceInstance: not null
72+
parentService: consumer
73+
traceId: not null
74+
startTime: gt 0
75+
endTime: gt 0
76+
componentId: 7001
77+
spanType: Entry
78+
peer: not null
79+
skipAnalysis: false
80+
- serviceName: consumer
81+
segmentSize: 2
82+
segments:
83+
- segmentId: not null
84+
spans:
85+
- operationName: /users
86+
operationId: 0
87+
parentSpanId: -1
88+
spanId: 0
89+
spanLayer: Http
90+
startTime: gt 0
91+
endTime: gt 0
92+
componentId: 7002
93+
isError: false
94+
spanType: Exit
95+
peer: not null
96+
skipAnalysis: false
97+
tags:
98+
- key: http.method
99+
value: POST
100+
- key: url
101+
value: 'http://provider:9091/users'
102+
- key: status.code
103+
value: '200'
104+
- segmentId: not null
105+
spans:
106+
- operationName: /users
107+
operationId: 0
108+
parentSpanId: 0
109+
spanId: 1
110+
spanLayer: Http
111+
startTime: gt 0
112+
endTime: gt 0
113+
componentId: 7002
114+
isError: false
115+
spanType: Exit
116+
peer: provider:9091
117+
skipAnalysis: false
118+
tags:
119+
- key: http.method
120+
value: POST
121+
- key: url
122+
value: 'http://provider:9091/users'
123+
- key: status.code
124+
value: '200'
125+
- operationName: /users
126+
operationId: 0
127+
parentSpanId: -1
128+
spanId: 0
129+
spanLayer: Http
130+
startTime: gt 0
131+
endTime: gt 0
132+
componentId: 7001
133+
isError: false
134+
spanType: Entry
135+
peer: not null
136+
skipAnalysis: false
137+
tags:
138+
- key: http.method
139+
value: GET
140+
- key: url
141+
value: 'http://0.0.0.0:9090/users'
142+
- key: status.code
143+
value: '200'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#

0 commit comments

Comments
 (0)