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

Enable Django collect http parameters #49

Merged
merged 4 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Environment Variable | Description | Default
| `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_DJANGO_COLLECT_HTTP_PARAMS`| This config item controls that whether the Django 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` |


Expand Down
2 changes: 2 additions & 0 deletions skywalking/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
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
django_collect_http_params = True if os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') and \
os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') == 'True' else False # type: bool


def init(
Expand Down
14 changes: 12 additions & 2 deletions skywalking/plugins/sw_django/__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 Down Expand Up @@ -50,7 +50,13 @@ def _sw_get_response(this: BaseHandler, request):
span.peer = '%s:%s' % (request.META.get('REMOTE_ADDR'), request.META.get('REMOTE_PORT') or "80")

span.tag(Tag(key=tags.HttpMethod, val=request.method))
span.tag(Tag(key=tags.HttpUrl, val=request.build_absolute_uri()))
span.tag(Tag(key=tags.HttpUrl, val=request.build_absolute_uri().split("?")[0]))

# you can get request parameters by `request.GET` even though client are using POST or other methods
if config.django_collect_http_params and request.GET:
span.tag(Tag(key=tags.HttpParams,
val=params_tostring(request.GET)[0:config.http_params_length_threshold]))

resp = _get_response(this, request)
span.tag(Tag(key=tags.HttpStatus, val=resp.status_code))
if resp.status_code >= 400:
Expand All @@ -70,3 +76,7 @@ def _sw_handle_uncaught_exception(request, resolver, exc_info):
exception.handle_uncaught_exception = _sw_handle_uncaught_exception
except Exception:
logger.warning('failed to install plugin %s', __name__)


def params_tostring(params):
return "\n".join([k + '=[' + ",".join(params.getlist(k)) + ']' for k, _ in params.items()])
38 changes: 20 additions & 18 deletions tests/plugin/sw_django/expected.data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ segmentItems:
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: gt 0
endTime: gt 0
componentId: 7004
spanType: Entry
peer: not null
skipAnalysis: false
tags:
- key: http.method
value: POST
Expand All @@ -42,12 +48,6 @@ segmentItems:
parentServiceInstance: not null
parentService: consumer
traceId: not null
startTime: gt 0
endTime: gt 0
componentId: 7004
spanType: Entry
peer: not null
skipAnalysis: false
- serviceName: consumer
segmentSize: 1
segments:
Expand All @@ -58,34 +58,36 @@ segmentItems:
parentSpanId: 0
spanId: 1
spanLayer: Http
startTime: gt 0
endTime: gt 0
componentId: 7002
spanType: Exit
peer: provider:9091
skipAnalysis: false
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: 7002
spanType: Exit
peer: provider:9091
skipAnalysis: false
- operationName: /users
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: gt 0
endTime: gt 0
componentId: 7004
spanType: Entry
peer: not null
skipAnalysis: false
tags:
- key: http.method
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
endTime: gt 0
componentId: 7004
spanType: Entry
peer: not null
skipAnalysis: false
1 change: 1 addition & 0 deletions tests/plugin/sw_django/services/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

config.service_name = "consumer"
config.logging_level = "DEBUG"
config.django_collect_http_params = True
agent.start()


Expand Down
2 changes: 1 addition & 1 deletion tests/plugin/sw_django/test_django.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