Skip to content

Commit

Permalink
Fix subsegment naming on SQLAlchemy query capture (#32)
Browse files Browse the repository at this point in the history
* Improve SubSegment Naming to remove invalid chars from URL to prevent Warnings Later.

* Use strip_url to remove everything from ?  on in URL.

* Show segment/subsegment name when logging message about invalid chars to make debuging easier

* Update Changelog
  • Loading branch information
therealryanbonham authored and haotianw465 committed Mar 8, 2018
1 parent eb228b9 commit 29997ec
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
unreleased
==========
* feature: Use the official middleware pattern for Aiohttp ext. `PR29 <https://github.com/aws/aws-xray-sdk-python/pull/29>`_.

* bugfix: SQLAlcemy plugin would cause warning messages with some db connection strings that contained invalid characters for a segment/subsegment name.
0.96
====
* feature: Add support for SQLAlchemy and Flask-SQLAlcemy. `PR14 <https://github.com/aws/aws-xray-sdk-python/pull/14>`_.
Expand Down
2 changes: 1 addition & 1 deletion aws_xray_sdk/core/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, name):
self.parent_id = None

if self.name != name:
log.warning("Removing Segment/Subsugment Name invalid characters.")
log.warning("Removing Segment/Subsugment Name invalid characters from {}.".format(name))

# sampling
self.sampled = True
Expand Down
9 changes: 7 additions & 2 deletions aws_xray_sdk/ext/sqlalchemy/util/decerators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import re
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.ext.util import strip_url
from aws_xray_sdk.core.models.entity import _valid_name_characters
from future.standard_library import install_aliases
install_aliases()
from urllib.parse import urlparse, uses_netloc



def decorate_all_functions(function_decorator):
def decorator(cls):
for c in cls.__bases__:
Expand Down Expand Up @@ -46,7 +47,11 @@ def wrapper(*args, **kw):
sql = None
if sql is not None:
if getattr(c._local, 'entities', None) is not None:
subsegment = xray_recorder.begin_subsegment(sql['url'], namespace='remote')
# Strip URL of ? and following text
sub_name = strip_url(sql['url'])
# Ensure url has a valid characters.
sub_name = ''.join([c for c in sub_name if c in _valid_name_characters])
subsegment = xray_recorder.begin_subsegment(sub_name, namespace='remote')
else:
subsegment = None
res = func(*args, **kw)
Expand Down

0 comments on commit 29997ec

Please sign in to comment.