-
Notifications
You must be signed in to change notification settings - Fork 641
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
feat(instrumentation-dbapi): add experimental sql commenter capability #908
Changes from all commits
54b8cb4
c800b21
575cb17
83ddcbb
bf24957
14d67c5
addfb3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import urllib.parse | ||
from typing import Dict, Sequence | ||
|
||
from wrapt import ObjectProxy | ||
|
@@ -115,3 +116,38 @@ def _start_internal_or_server_span( | |
attributes=attributes, | ||
) | ||
return span, token | ||
|
||
|
||
_KEY_VALUE_DELIMITER = "," | ||
|
||
|
||
def _generate_sql_comment(**meta): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really necessary to add this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so, there will be many sql db instrumentations that will rely on this. |
||
""" | ||
Return a SQL comment with comma delimited key=value pairs created from | ||
**meta kwargs. | ||
""" | ||
if not meta: # No entries added. | ||
return "" | ||
|
||
# Sort the keywords to ensure that caching works and that testing is | ||
# deterministic. It eases visual inspection as well. | ||
return ( | ||
" /*" | ||
+ _KEY_VALUE_DELIMITER.join( | ||
"{}={!r}".format(_url_quote(key), _url_quote(value)) | ||
for key, value in sorted(meta.items()) | ||
if value is not None | ||
) | ||
+ "*/" | ||
) | ||
|
||
|
||
def _url_quote(s): # pylint: disable=invalid-name | ||
if not isinstance(s, (str, bytes)): | ||
return s | ||
quoted = urllib.parse.quote(s) | ||
# Since SQL uses '%' as a keyword, '%' is a by-product of url quoting | ||
# e.g. foo,bar --> foo%2Cbar | ||
# thus in our quoting, we need to escape it too to finally give | ||
# foo,bar --> foo%%2Cbar | ||
return quoted.replace("%", "%%") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single-use constant