⚡️ Speed up function extract_sentrytrace_data by 37%#4944
Open
misrasaurabh1 wants to merge 3 commits intogetsentry:masterfrom
Open
⚡️ Speed up function extract_sentrytrace_data by 37%#4944misrasaurabh1 wants to merge 3 commits intogetsentry:masterfrom
extract_sentrytrace_data by 37%#4944misrasaurabh1 wants to merge 3 commits intogetsentry:masterfrom
Conversation
The optimization adds length checks before expensive string formatting operations. Specifically:
**Key Changes:**
- Added `len(trace_id) != 32` check before `"{:032x}".format(int(trace_id, 16))`
- Added `len(parent_span_id) != 16` check before `"{:016x}".format(int(parent_span_id, 16))`
**Why It's Faster:**
The original code always performed string-to-int conversion and formatting, even when the trace_id/span_id were already properly formatted. The optimization skips these expensive operations when the strings are already the correct length (32 hex chars for trace_id, 16 for span_id).
The `int(trace_id, 16)` and `"{:032x}".format()` operations are computationally expensive, involving:
- Hexadecimal string parsing
- Integer conversion
- String formatting with zero-padding
**Performance Impact:**
Test results show the optimization is most effective when trace IDs and span IDs are already properly formatted (which is common in production). Cases like `test_valid_full_header` show 51.6% speedup, and `test_missing_trace_id` shows 65.9% speedup. The optimization has minimal overhead for cases where formatting is still needed, with only small gains (1-7%) for malformed inputs.
This is particularly valuable for high-throughput tracing scenarios where most headers contain well-formatted trace data.
misrasaurabh1
commented
Oct 16, 2025
Comment on lines
+370
to
371
| if trace_id and len(trace_id) != 32: | ||
| trace_id = "{:032x}".format(int(trace_id, 16)) |
Contributor
There was a problem hiding this comment.
If the string formatting is really redundant after the regex match, the string formatting logic can be removed.
There's no point checking len(trace_id) != 32, because to reach this point the regex has matched, so the string must be 32 characters long.
Member
There was a problem hiding this comment.
agreed, can just remove it
misrasaurabh1
commented
Oct 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📄 37% (0.37x) speedup for
extract_sentrytrace_datainsentry_sdk/tracing_utils.pyThis one looked really important as well. I found a bunch more optimizations, but don't want to spam you guys. Would you like to meet over a 30 min call to discuss how we can better coordinate?
⏱️ Runtime :
3.30 milliseconds→2.41 milliseconds(best of124runs)📝 Explanation and details
The optimization adds length checks before expensive string formatting operations. Specifically:
Key Changes:
len(trace_id) != 32check before"{:032x}".format(int(trace_id, 16))len(parent_span_id) != 16check before"{:016x}".format(int(parent_span_id, 16))Why It's Faster:
The original code always performed string-to-int conversion and formatting, even when the trace_id/span_id were already properly formatted. The optimization skips these expensive operations when the strings are already the correct length (32 hex chars for trace_id, 16 for span_id).
The
int(trace_id, 16)and"{:032x}".format()operations are computationally expensive, involving:Performance Impact:
Test results show the optimization is most effective when trace IDs and span IDs are already properly formatted (which is common in production). Cases like
test_valid_full_headershow 51.6% speedup, andtest_missing_trace_idshows 65.9% speedup. The optimization has minimal overhead for cases where formatting is still needed, with only small gains (1-7%) for malformed inputs.This is particularly valuable for high-throughput tracing scenarios where most headers contain well-formatted trace data.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
tracing/test_http_headers.py::test_sentrytrace_extraction🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-extract_sentrytrace_data-mg9m9ul7and push.