Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Flask Http Params #43

Merged
merged 2 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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