Skip to content

Commit

Permalink
Add Flask Http Params (#43)
Browse files Browse the repository at this point in the history
* Add Flask Http Params

* Add Flask Http Params

Co-authored-by: huawei <huawei@bit-s.cn>
  • Loading branch information
alonelaval and huawei committed Jul 19, 2020
1 parent cdd2db8 commit 0246634
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions skywalking/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
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(
Expand Down
10 changes: 8 additions & 2 deletions skywalking/plugins/sw_flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -48,7 +51,10 @@ def _sw_full_dispatch_request(this: Flask):
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:
Expand Down
1 change: 1 addition & 0 deletions skywalking/trace/tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
DbInstance = 'db.instance'
DbStatement = 'db.statement'
DbSqlParameters = 'db.sql.parameters'
HttpParams = 'http.params'
2 changes: 2 additions & 0 deletions tests/plugin/sw_flask/expected.data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/plugin/sw_flask/services/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
if __name__ == '__main__':
config.service_name = 'consumer'
config.logging_level = 'DEBUG'
config.flask_collect_http_params = True
agent.start()

from flask import Flask, jsonify
Expand Down
2 changes: 1 addition & 1 deletion tests/plugin/sw_flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setUpClass(cls):
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)
Expand Down

0 comments on commit 0246634

Please sign in to comment.