-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Fix/logfmtrenderer quotes escaping #513
Conversation
string_with_quotes = r'"example": "this \"should\" work!"' | ||
event_dict = {"exception": string_with_quotes} | ||
rv = LogfmtRenderer()(None, None, event_dict) | ||
assert r'exception="\"example\": \"this \\\"should\\\" work!\""' == rv |
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.
I checked, and this makes grafana (or rather, loki) happy, and the output is "example": "this \"should\" work!"
However, I think that what makes it work in this implementation is the spaces, the same example without spaces won't be between quotes, and that'll result in invalid logfmt.
@@ -166,6 +168,10 @@ def __call__( | |||
|
|||
return " ".join(elements) | |||
|
|||
def _escape_double_quotes(self, text: str) -> str: | |||
value = f"{text}".replace(r"\"", r"\\\"") | |||
return self._double_quotes_without_backslash_prefix.sub(r"\"", value) |
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.
I think there's one more corner case here: if the value is between quotes, then the \
need to be escaped.
For example, the raw string \ "
needs to be encoded as "\\ \"
, not "\ \"
(confirmed on loki).
Summary
PR to fix escaping double quotes in LogfmtRenderer
(fix #511 )
Pull Request Check List
typing_examples.py
.docs/api.rst
by hand.versionadded
,versionchanged
, ordeprecated
directives.Find the appropriate next version in our
__init__.py
file..rst
and.md
files is written using semantic newlines.