未验证 提交 0246634b 编写于 作者: H huawei 提交者: GitHub

Add Flask Http Params (#43)

* Add Flask Http Params

* Add Flask Http Params
Co-authored-by: Nhuawei <huawei@bit-s.cn>
上级 cdd2db86
......@@ -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
......
......@@ -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(
......
......@@ -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:
......
......@@ -27,3 +27,4 @@ DbType = 'db.type'
DbInstance = 'db.instance'
DbStatement = 'db.statement'
DbSqlParameters = 'db.sql.parameters'
HttpParams = 'http.params'
......@@ -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
......
......@@ -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
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册