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

Fix/logfmtrenderer quotes escaping #513

Closed

Conversation

davellas
Copy link

Summary

PR to fix escaping double quotes in LogfmtRenderer
(fix #511 )

Pull Request Check List

  • Added tests for changed code.
    • The CI fails with less than 100% coverage.
  • New APIs are added to typing_examples.py.
  • Updated documentation for changed code.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      Find the appropriate next version in our __init__.py file.
  • Documentation in .rst and .md files is written using semantic newlines.
  • Changes (and possible deprecations) are documented in the changelog.
  • Consider granting push permissions to the PR branch, so maintainers can fix minor issues themselves without pestering you.

@hynek hynek mentioned this pull request Jan 29, 2024
9 tasks
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
Copy link
Contributor

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)
Copy link
Contributor

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).

hynek added a commit that referenced this pull request Feb 5, 2024
@hynek hynek mentioned this pull request Feb 5, 2024
@hynek hynek closed this in #594 Feb 7, 2024
@hynek hynek closed this in 354ef53 Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LogfmtRenderer has a bug with escaped double quotes
2 participants