You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/07/02 13:44:04 UTC
[skywalking-python] 01/12: add flask plugin
This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 pushed a commit to branch flask-plugin
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git
commit e07e6c96d064eb34782b6ad129a386724c58791b
Merge: 55ad3bb 22ba15c
Author: huawei <hu...@bit-s.cn>
AuthorDate: Fri Jun 26 11:05:20 2020 +0800
add flask plugin
.gitignore | 1 +
CHANGELOG.md | 7 ++
Makefile | 13 ++--
README.md | 6 +-
docs/FAQ.md | 18 ++++-
docs/PluginTest.md | 6 +-
setup.py | 22 +++++-
skywalking/agent/__init__.py | 2 +-
skywalking/plugins/sw_flask/__init__.py | 63 ++++++++++++++++
skywalking/plugins/sw_http_server/__init__.py | 79 +++++++++++++-------
skywalking/plugins/sw_requests/__init__.py | 12 +++-
skywalking/trace/context/__init__.py | 11 +++
skywalking/trace/span/__init__.py | 2 +-
tests/plugin/docker/Dockerfile.agent | 2 +-
tests/plugin/requests_scenario/expected.data.yml | 52 --------------
.../{requests_scenario => sw_flask}/__init__.py | 1 -
.../docker-compose.yml | 26 ++++++-
tests/plugin/sw_flask/expected.data.yml | 84 ++++++++++++++++++++++
.../plugin/{http => sw_flask/services}/__init__.py | 1 -
.../provider.py => sw_flask/services/consumer.py} | 19 +++--
.../__init__.py => sw_flask/services/provider.py} | 20 ++++++
.../test_request.py => sw_flask/test_flask.py} | 6 +-
.../expected.data.yml => sw_http/__init__.py} | 21 ------
tests/plugin/{http => sw_http}/docker-compose.yml | 26 ++++++-
tests/plugin/sw_http/expected.data.yml | 84 ++++++++++++++++++++++
.../services}/__init__.py | 1 -
.../provider.py => sw_http/services/consumer.py} | 19 +++--
.../plugin/{http => sw_http/services}/provider.py | 0
.../{http/test_request.py => sw_http/test_http.py} | 6 +-
.../__init__.py | 1 -
.../docker-compose.yml | 26 ++++++-
tests/plugin/sw_http_wsgi/expected.data.yml | 84 ++++++++++++++++++++++
.../services}/__init__.py | 1 -
.../services/consumer.py} | 19 +++--
.../{http => sw_http_wsgi/services}/provider.py | 20 ++----
.../test_http_wsgi.py} | 6 +-
.../{requests_scenario => sw_requests}/__init__.py | 1 -
.../docker-compose.yml | 26 ++++++-
tests/plugin/sw_requests/expected.data.yml | 84 ++++++++++++++++++++++
.../services}/__init__.py | 1 -
.../services/consumer.py} | 16 ++---
.../{http => sw_requests/services}/provider.py | 0
tests/plugin/{http => sw_requests}/test_request.py | 6 +-
43 files changed, 716 insertions(+), 185 deletions(-)
diff --cc skywalking/plugins/sw_flask/__init__.py
index 0000000,0000000..7472501
new file mode 100644
--- /dev/null
+++ b/skywalking/plugins/sw_flask/__init__.py
@@@ -1,0 -1,0 +1,63 @@@
++#
++# Licensed to the Apache Software Foundation (ASF) under one or more
++# contributor license agreements. See the NOTICE file distributed with
++# this work for additional information regarding copyright ownership.
++# The ASF licenses this file to You under the Apache License, Version 2.0
++# (the "License"); you may not use this file except in compliance with
++# the License. You may obtain a copy of the License at
++#
++# http://www.apache.org/licenses/LICENSE-2.0
++#
++# Unless required by applicable law or agreed to in writing, software
++# distributed under the License is distributed on an "AS IS" BASIS,
++# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
++# See the License for the specific language governing permissions and
++# limitations under the License.
++#
++import logging
++import traceback
++
++from skywalking import Layer, Component
++from skywalking.trace import tags
++from skywalking.trace.carrier import Carrier
++from skywalking.trace.context import get_context
++from skywalking.trace.tags import Tag
++
++logger = logging.getLogger(__name__)
++
++
++def install():
++ # noinspection PyBroadException
++ try:
++ from flask import Flask
++ _full_dispatch_request = Flask.full_dispatch_request
++
++ _handle_user_exception = Flask.handle_user_exception
++
++ def _sw_full_dispatch_request(this: Flask):
++ import flask
++ req = flask.request
++ context = get_context()
++ carrier = Carrier()
++ for item in carrier:
++ item.val = req.headers[item.key.capitalize()]
++ with context.new_entry_span(op=req.path, carrier=carrier) as span:
++ span.layer = Layer.Http
++ span.component = Component.Flask
++ span.peer = '%s:%s' % (req.environ["REMOTE_ADDR"], req.environ["REMOTE_PORT"])
++ span.tag(Tag(key=tags.HttpMethod, val=req.method))
++ span.tag(Tag(key=tags.HttpUrl, val=req.url))
++ return _full_dispatch_request(this)
++
++ def _sw_handle_user_exception(this: Flask, e):
++ if e is not None:
++ entry_span = get_context().active_span()
++ entry_span.log(e)
++ return _handle_user_exception(this, e)
++
++ Flask.full_dispatch_request = _sw_full_dispatch_request
++ Flask.handle_user_exception = _sw_handle_user_exception
++
++ except Exception:
++ logger.warning('failed to install plugin %s', __name__)
++ traceback.print_exc()
diff --cc skywalking/plugins/sw_requests/__init__.py
index f24acef,b1b6d02..f6b90d2
--- a/skywalking/plugins/sw_requests/__init__.py
+++ b/skywalking/plugins/sw_requests/__init__.py
@@@ -74,4 -82,4 +82,4 @@@ def install()
Session.request = _sw_request
except Exception:
logger.warning('failed to install plugin %s', __name__)
-- traceback.print_exc()
++ traceback.print_exc()
diff --cc skywalking/trace/context/__init__.py
index 71beae6,dce8d68..8d97908
--- a/skywalking/trace/context/__init__.py
+++ b/skywalking/trace/context/__init__.py
@@@ -91,6 -91,6 +91,12 @@@ class SpanContext(object)
return len(self.spans) == 0
++ def active_span(self):
++ if self.spans:
++ return self.spans[len(self.spans) - 1]
++
++ return None
++
class NoopContext(SpanContext):
def __init__(self):
@@@ -114,6 -114,6 +120,9 @@@
self._depth -= 1
return self._depth == 0
++ def active_span(self):
++ return self._noop_span
++
_thread_local = threading.local()
_thread_local.context = None
diff --cc tests/plugin/sw_flask/__init__.py
index 6222972,0000000..b1312a0
mode 100644,000000..100644
--- a/tests/plugin/sw_flask/__init__.py
+++ b/tests/plugin/sw_flask/__init__.py
@@@ -1,17 -1,0 +1,16 @@@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
-
diff --cc tests/plugin/sw_flask/docker-compose.yml
index c43fee3,0000000..3f0d305
mode 100644,000000..100644
--- a/tests/plugin/sw_flask/docker-compose.yml
+++ b/tests/plugin/sw_flask/docker-compose.yml
@@@ -1,54 -1,0 +1,78 @@@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+version: '2.1'
+
+services:
+ collector:
+ build:
+ context: ../docker
+ dockerfile: Dockerfile.tool
+ ports:
+ - 19876:19876
+ - 12800:12800
+ networks:
+ - beyond
+ healthcheck:
+ test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/12800"]
+ interval: 5s
+ timeout: 60s
+ retries: 120
+
+ provider:
+ build:
+ context: ../../../
+ dockerfile: tests/plugin/docker/Dockerfile.agent
+ networks:
+ - beyond
+ ports:
+ - 9091:9091
+ volumes:
- - ./provider.py:/app/provider.py
++ - ./services/provider.py:/app/provider.py
+ environment:
+ SW_AGENT_COLLECTOR_BACKEND_SERVICES: collector:19876
+ command: ['python3', '/app/provider.py']
+ depends_on:
+ collector:
+ condition: service_healthy
++ healthcheck:
++ test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9091"]
++ interval: 5s
++ timeout: 60s
++ retries: 120
++
++ consumer:
++ build:
++ context: ../../../
++ dockerfile: tests/plugin/docker/Dockerfile.agent
++ networks:
++ - beyond
++ ports:
++ - 9090:9090
++ volumes:
++ - ./services/consumer.py:/app/consumer.py
++ environment:
++ SW_AGENT_COLLECTOR_BACKEND_SERVICES: collector:19876
++ command: ['python3', '/app/consumer.py']
++ depends_on:
++ collector:
++ condition: service_healthy
++ provider:
++ condition: service_healthy
+
+networks:
+ beyond:
diff --cc tests/plugin/sw_flask/expected.data.yml
index 0000000,0000000..509127e
new file mode 100644
--- /dev/null
+++ b/tests/plugin/sw_flask/expected.data.yml
@@@ -1,0 -1,0 +1,84 @@@
++#
++# Licensed to the Apache Software Foundation (ASF) under one or more
++# contributor license agreements. See the NOTICE file distributed with
++# this work for additional information regarding copyright ownership.
++# The ASF licenses this file to You under the Apache License, Version 2.0
++# (the "License"); you may not use this file except in compliance with
++# the License. You may obtain a copy of the License at
++#
++# http://www.apache.org/licenses/LICENSE-2.0
++#
++# Unless required by applicable law or agreed to in writing, software
++# distributed under the License is distributed on an "AS IS" BASIS,
++# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
++# See the License for the specific language governing permissions and
++# limitations under the License.
++#
++
++segmentItems:
++ - serviceName: provider
++ segmentSize: 1
++ segments:
++ - segmentId: not null
++ spans:
++ - operationName: /users
++ operationId: 0
++ parentSpanId: -1
++ spanId: 0
++ spanLayer: Http
++ tags:
++ - key: http.method
++ value: POST
++ refs:
++ - parentEndpoint: /users
++ networkAddress: provider:9091
++ refType: CrossProcess
++ parentSpanId: 1
++ parentTraceSegmentId: not null
++ parentServiceInstance: not null
++ parentService: consumer
++ traceId: not null
++ startTime: gt 0
++ endTime: gt 0
++ componentId: 7000
++ spanType: Entry
++ peer: not null
++ skipAnalysis: false
++ - serviceName: consumer
++ segmentSize: 1
++ segments:
++ - segmentId: not null
++ spans:
++ - operationName: /users
++ operationId: 0
++ parentSpanId: 0
++ spanId: 1
++ spanLayer: Http
++ tags:
++ - key: http.method
++ value: POST
++ - key: url
++ value: http://provider:9091/users
++ - key: status.code
++ value: '200'
++ startTime: gt 0
++ endTime: gt 0
++ componentId: 7000
++ spanType: Exit
++ peer: provider:9091
++ skipAnalysis: false
++ - operationName: /
++ operationId: 0
++ parentSpanId: -1
++ spanId: 0
++ spanLayer: Http
++ tags:
++ - key: http.method
++ value: POST
++ startTime: gt 0
++ endTime: gt 0
++ componentId: 7000
++ spanType: Entry
++ peer: not null
++ skipAnalysis: false
++
diff --cc tests/plugin/sw_flask/services/__init__.py
index 6222972,0000000..b1312a0
mode 100644,000000..100644
--- a/tests/plugin/sw_flask/services/__init__.py
+++ b/tests/plugin/sw_flask/services/__init__.py
@@@ -1,17 -1,0 +1,16 @@@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
-
diff --cc tests/plugin/sw_flask/services/consumer.py
index 7af884a,0000000..43f60bd
mode 100644,000000..100644
--- a/tests/plugin/sw_flask/services/consumer.py
+++ b/tests/plugin/sw_flask/services/consumer.py
@@@ -1,43 -1,0 +1,48 @@@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
- import time
++from urllib import request
+
+from skywalking import agent, config
+
+if __name__ == '__main__':
- config.service_name = 'provider'
++ config.service_name = 'consumer'
+ config.logging_level = 'DEBUG'
+ agent.start()
+
+ import socketserver
+ from http.server import BaseHTTPRequestHandler
+
+ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
-
+ def do_POST(self):
- time.sleep(0.5)
+ self.send_response(200)
- self.send_header('Content-Type', 'application/json')
++ self.send_header('Content-Type', 'application/json; charset=utf-8')
+ self.end_headers()
- self.wfile.write('{"song": "Despacito", "artist": "Luis Fonsi"}'.encode('ascii'))
+
- PORT = 9091
++ data = '{"name": "whatever"}'.encode('utf8')
++ req = request.Request('http://provider:9091/users')
++ req.add_header('Content-Type', 'application/json; charset=utf-8')
++ req.add_header('Content-Length', str(len(data)))
++ with request.urlopen(req, data):
++ self.wfile.write(data)
++
++ PORT = 9090
+ Handler = SimpleHTTPRequestHandler
+
+ with socketserver.TCPServer(("", PORT), Handler) as httpd:
++ print("serving at port", PORT)
+ httpd.serve_forever()
diff --cc tests/plugin/sw_flask/services/provider.py
index 6222972,0000000..e3213bb
mode 100644,000000..100644
--- a/tests/plugin/sw_flask/services/provider.py
+++ b/tests/plugin/sw_flask/services/provider.py
@@@ -1,17 -1,0 +1,37 @@@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
++import time
++
++from skywalking import agent, config
++
++if __name__ == '__main__':
++ config.service_name = 'provider'
++ config.logging_level = 'DEBUG'
++ agent.start()
++
++ from flask import Flask, jsonify
++
++ app = Flask(__name__)
++
++ @app.route("/users", methods=["POST","GET"])
++ def application():
++ time.sleep(0.5)
++ return jsonify('{"song": "Despacito", "artist": "Luis Fonsi"}')
++
++ PORT = 9091
++ app.run(port=PORT)
diff --cc tests/plugin/sw_flask/test_flask.py
index 1619dd8,0000000..eacaa98
mode 100644,000000..100644
--- a/tests/plugin/sw_flask/test_flask.py
+++ b/tests/plugin/sw_flask/test_flask.py
@@@ -1,48 -1,0 +1,46 @@@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import os
+import time
+import unittest
+from os.path import abspath, dirname
+
+import requests
+from testcontainers.compose import DockerCompose
+
+from tests.plugin import BasePluginTest
+
+
+class TestRequestPlugin(BasePluginTest):
+ @classmethod
+ def setUpClass(cls):
- docker_dir = dirname(dirname(abspath(__file__)))
-
- cls.compose = DockerCompose(filepath=os.path.join(docker_dir, 'http'))
++ cls.compose = DockerCompose(filepath=dirname(abspath(__file__)))
+ cls.compose.start()
+
+ cls.compose.wait_for(cls.url(cls.collector_address()))
+
+ def test_request_plugin(self):
- print('traffic: ', requests.post(url=self.url(('provider', '9091'))))
++ print('traffic: ', requests.post(url=self.url(('consumer', '9090'))))
+
+ time.sleep(3)
+
+ self.validate(expected_file_name=os.path.join(dirname(abspath(__file__)), 'expected.data.yml'))
+
+
+if __name__ == '__main__':
+ unittest.main()