-
Notifications
You must be signed in to change notification settings - Fork 405
Cheaper logcontext debug logs (pseudo_random_string(...))
#19094
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
base: develop
Are you sure you want to change the base?
Cheaper logcontext debug logs (pseudo_random_string(...))
#19094
Conversation
| return "".join(secrets.choice(_string_with_symbols) for _ in range(length)) | ||
|
|
||
|
|
||
| def pseudo_random_string(length: int) -> str: |
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 also considered naming this random_string_insecure_fast
🤷
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 we should call this insecure_random_string. Being pseudo-random doesn't necessarily imply insecure, that's what CSPRNGs are for, after all.
It's maybe not a huge deal, but given the sheer hazard of using the wrong type of random in the wrong place, I much prefer the clear and simple insecure label, because it brings your attention to an important (negative) caveat. In theory that could raise some alarm bells at a critical time during review.
On the other hand, I don't think it's important to say fast — if someone consciously thinks about the speed at PR review time, they can look it up. (But I'm also not against calling it 'fast', to be fair!)
| instance_id = pseudo_random_string(5) | ||
| calling_context = current_context() | ||
| logcontext_debug_logger.debug( | ||
| "run_in_background(%s): called with logcontext=%s", instance_id, calling_context | ||
| ) |
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.
As another alternative, we could gate the random string creation behind if logcontext_debug_logger.isEnabledFor(logging.DEBUG)
| return "".join(secrets.choice(_string_with_symbols) for _ in range(length)) | ||
|
|
||
|
|
||
| def pseudo_random_string(length: int) -> str: |
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 we should call this insecure_random_string. Being pseudo-random doesn't necessarily imply insecure, that's what CSPRNGs are for, after all.
It's maybe not a huge deal, but given the sheer hazard of using the wrong type of random in the wrong place, I much prefer the clear and simple insecure label, because it brings your attention to an important (negative) caveat. In theory that could raise some alarm bells at a critical time during review.
On the other hand, I don't think it's important to say fast — if someone consciously thinks about the speed at PR review time, they can look it up. (But I'm also not against calling it 'fast', to be fair!)
Cheaper logcontext debug logs (
pseudo_random_string(...))Follow-up to #18966
During the weekly Backend team meeting, it was mentioned that
random_string(...)was taking a significant amount of CPU onmatrix.org. This makes sense as it relies onsecrets.choice(...), a cryptographically secure function that is inherently computationally expensive. And since #18966, we're callingrandom_string(...)as part of a bunch of logcontext utilities.Since we don't need cryptographically secure random strings for our debug logs, this PR is introducing a new
pseudo_random_string(...)function that usesrandom.choice(...)which uses pseudo-random numbers that are "both fast and threadsafe".Dev notes
Pull Request Checklist
EventStoretoEventWorkerStore.".code blocks.