Skip to content

Commit 29997ec

Browse files
therealryanbonhamhaotianw465
authored andcommitted
Fix subsegment naming on SQLAlchemy query capture (#32)
* 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
1 parent eb228b9 commit 29997ec

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANGELOG
55
unreleased
66
==========
77
* feature: Use the official middleware pattern for Aiohttp ext. `PR29 <https://github.com/aws/aws-xray-sdk-python/pull/29>`_.
8-
8+
* bugfix: SQLAlcemy plugin would cause warning messages with some db connection strings that contained invalid characters for a segment/subsegment name.
99
0.96
1010
====
1111
* feature: Add support for SQLAlchemy and Flask-SQLAlcemy. `PR14 <https://github.com/aws/aws-xray-sdk-python/pull/14>`_.

aws_xray_sdk/core/models/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, name):
3535
self.parent_id = None
3636

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

4040
# sampling
4141
self.sampled = True

aws_xray_sdk/ext/sqlalchemy/util/decerators.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import re
22
from aws_xray_sdk.core import xray_recorder
3+
from aws_xray_sdk.ext.util import strip_url
4+
from aws_xray_sdk.core.models.entity import _valid_name_characters
35
from future.standard_library import install_aliases
46
install_aliases()
57
from urllib.parse import urlparse, uses_netloc
68

79

8-
910
def decorate_all_functions(function_decorator):
1011
def decorator(cls):
1112
for c in cls.__bases__:
@@ -46,7 +47,11 @@ def wrapper(*args, **kw):
4647
sql = None
4748
if sql is not None:
4849
if getattr(c._local, 'entities', None) is not None:
49-
subsegment = xray_recorder.begin_subsegment(sql['url'], namespace='remote')
50+
# Strip URL of ? and following text
51+
sub_name = strip_url(sql['url'])
52+
# Ensure url has a valid characters.
53+
sub_name = ''.join([c for c in sub_name if c in _valid_name_characters])
54+
subsegment = xray_recorder.begin_subsegment(sub_name, namespace='remote')
5055
else:
5156
subsegment = None
5257
res = func(*args, **kw)

0 commit comments

Comments
 (0)