-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Support bytearray for final processor output #344
Comments
Yes, the change should be as simple as you say. May I ask how you'd intend to use it? I've added bytes specifically to support orjson's performance gains. Is there something out there spewing out bytearrays? 🤔 |
Thanks for the reply, and I'm happy to share my use case, although it may be a little half-baked at this point. In short, I created a custom Here's what the processor looks like, with the hard-coded assumption that import flask
import orjson
from structlog.types import EventDict
def _render_request(_, __, event_dict: EventDict) -> bytes:
"""Structlog processor for injecting the Flask request.
The key will be "request_data"
"""
bytes_ = orjson.dumps(event_dict)
# Create bytes that includes the request data.
return (
# Everything but the last character (assumed to be b"}")
bytes_[0:-1]
# Call the field "request_data"
+ b',"request_data":'
# Add in the data.
+ flask.request.data
# Close it out.
+ b"}"
) Simple enough, and nice and hard-coded 😉. I was initially thinking (but have since proved myself wrong) that using a |
OK, now I'm confused. 😅 Did you come to the conclusion that bytearrays are helpful or not? :D |
Ah whatever, does #350 fix your needs? |
It does, thank you! 😄 |
Use case: For performance in concatenating bytes, one might want their final processor to output a
bytearray
instead ofbytes
. This blog post illustrates that using abytearray
is the most performant way to concatenate bytes in Python (with all the caveats of sharing results from a blog post that haven't been verified locally, and the experiment can't exactly be considered comprehensive or rigorous).Use of
bytearray
output is currently unsupported, as can be seen in _base.py:I'll be honest in that I'm not an expert in byte handling in Python, but it seems like
bytearray
quacks likebytes
according to the docs:In a very crude and naive local test, I modified this line in
structlog
from:if isinstance(event_dict, (str, bytes)):
to:
if isinstance(event_dict, (str, bytes, bytearray)):
and my stdout bytes logging still worked (configured identically to this example in the Performance section of the
structlog
docs).The text was updated successfully, but these errors were encountered: