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/19 15:32:10 UTC

[skywalking-python] branch master updated: Add Flask Http Params (#43)

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git


The following commit(s) were added to refs/heads/master by this push:
     new 0246634  Add Flask Http Params (#43)
0246634 is described below

commit 0246634b8e908b845e1540c6d2b70d9058794601
Author: huawei <al...@gmail.com>
AuthorDate: Sun Jul 19 23:32:02 2020 +0800

    Add Flask Http Params (#43)
    
    * Add Flask Http Params
    
    * Add Flask Http Params
    
    Co-authored-by: huawei <hu...@bit-s.cn>
---
 README.md                                  |  2 ++
 skywalking/config/__init__.py              |  3 +++
 skywalking/plugins/sw_flask/__init__.py    | 10 ++++++++--
 skywalking/trace/tags/__init__.py          |  1 +
 tests/plugin/sw_flask/expected.data.yml    |  2 ++
 tests/plugin/sw_flask/services/consumer.py |  1 +
 tests/plugin/sw_flask/test_flask.py        |  2 +-
 7 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 963429c..694f04c 100755
--- a/README.md
+++ b/README.md
@@ -61,6 +61,8 @@ Environment Variable | Description | Default
 | `SW_MYSQL_TRACE_SQL_PARAMETERS` | Indicates whether to collect the sql parameters or not | `False` |
 | `SW_MYSQL_SQL_PARAMETERS_MAX_LENGTH` | The maximum length of the collected parameter, parameters longer than the specified length will be truncated | `512` |
 | `SW_IGNORE_SUFFIX` | If the operation name of the first span is included in this set, this segment should be ignored. | `.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg` |
+| `SW_FLASK_COLLECT_HTTP_PARAMS`| This config item controls that whether the Flask plugin should collect the parameters of the request.| `false` |
+| `SW_HTTP_PARAMS_LENGTH_THRESHOLD`| When `COLLECT_HTTP_PARAMS` is enabled, how many characters to keep and send to the OAP backend, use negative values to keep and send the complete parameters, NB. this config item is added for the sake of performance.  | `1024` |
 
 
 ## Supported Libraries
diff --git a/skywalking/config/__init__.py b/skywalking/config/__init__.py
index 5eccc9d..919bd7f 100644
--- a/skywalking/config/__init__.py
+++ b/skywalking/config/__init__.py
@@ -31,6 +31,9 @@ mysql_trace_sql_parameters = True if os.getenv('SW_MYSQL_TRACE_SQL_PARAMETERS')
 mysql_sql_parameters_max_length = int(os.getenv('SW_MYSQL_SQL_PARAMETERS_MAX_LENGTH') or '512')  # type: int
 ignore_suffix = os.getenv('SW_IGNORE_SUFFIX') or '.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,' \
                                                  '.mp4,.html,.svg '  # type: str
+flask_collect_http_params = True if os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') and \
+                                    os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') == 'True' else False   # type: bool
+http_params_length_threshold = int(os.getenv('SW_HTTP_PARAMS_LENGTH_THRESHOLD') or '1024')  # type: int
 
 
 def init(
diff --git a/skywalking/plugins/sw_flask/__init__.py b/skywalking/plugins/sw_flask/__init__.py
index b62ceb0..57c4963 100644
--- a/skywalking/plugins/sw_flask/__init__.py
+++ b/skywalking/plugins/sw_flask/__init__.py
@@ -16,7 +16,7 @@
 #
 import logging
 
-from skywalking import Layer, Component
+from skywalking import Layer, Component, config
 from skywalking.trace import tags
 from skywalking.trace.carrier import Carrier
 from skywalking.trace.context import get_context
@@ -34,6 +34,9 @@ def install():
 
         _handle_user_exception = Flask.handle_user_exception
 
+        def params_tostring(params):
+            return "\n".join([k + '=[' + ",".join(params.getlist(k)) + ']' for k, _ in params.items()])
+
         def _sw_full_dispatch_request(this: Flask):
             import flask
             req = flask.request
@@ -48,7 +51,10 @@ def install():
                 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))
+                span.tag(Tag(key=tags.HttpUrl, val=req.url.split("?")[0]))
+                if config.flask_collect_http_params and req.values:
+                    span.tag(Tag(key=tags.HttpParams,
+                             val=params_tostring(req.values)[0:config.http_params_length_threshold]))
                 resp = _full_dispatch_request(this)
 
                 if resp.status_code >= 400:
diff --git a/skywalking/trace/tags/__init__.py b/skywalking/trace/tags/__init__.py
index d642d69..89a6819 100644
--- a/skywalking/trace/tags/__init__.py
+++ b/skywalking/trace/tags/__init__.py
@@ -27,3 +27,4 @@ DbType = 'db.type'
 DbInstance = 'db.instance'
 DbStatement = 'db.statement'
 DbSqlParameters = 'db.sql.parameters'
+HttpParams = 'http.params'
diff --git a/tests/plugin/sw_flask/expected.data.yml b/tests/plugin/sw_flask/expected.data.yml
index 3e9d0b3..453f539 100644
--- a/tests/plugin/sw_flask/expected.data.yml
+++ b/tests/plugin/sw_flask/expected.data.yml
@@ -81,6 +81,8 @@ segmentItems:
                 value: GET
               - key: url
                 value: http://0.0.0.0:9090/users
+              - key: http.params
+                value: "test=[test1,test2]\ntest2=[test2]"
               - key: status.code
                 value: '200'
             startTime: gt 0
diff --git a/tests/plugin/sw_flask/services/consumer.py b/tests/plugin/sw_flask/services/consumer.py
index c942f91..45e7e80 100644
--- a/tests/plugin/sw_flask/services/consumer.py
+++ b/tests/plugin/sw_flask/services/consumer.py
@@ -22,6 +22,7 @@ from skywalking import agent, config
 if __name__ == '__main__':
     config.service_name = 'consumer'
     config.logging_level = 'DEBUG'
+    config.flask_collect_http_params = True
     agent.start()
 
     from flask import Flask, jsonify
diff --git a/tests/plugin/sw_flask/test_flask.py b/tests/plugin/sw_flask/test_flask.py
index 8187451..79827f5 100644
--- a/tests/plugin/sw_flask/test_flask.py
+++ b/tests/plugin/sw_flask/test_flask.py
@@ -30,7 +30,7 @@ class TestPlugin(BasePluginTest):
         cls.compose = DockerCompose(filepath=dirname(inspect.getfile(cls)))
         cls.compose.start()
 
-        cls.compose.wait_for(cls.url(('consumer', '9090'), 'users'))
+        cls.compose.wait_for(cls.url(('consumer', '9090'), 'users?test=test1&test=test2&test2=test2'))
 
     def test_plugin(self):
         time.sleep(3)