fix(tracing): calculate traceid in python not clickhouse to avoid nullable checks#52061
fix(tracing): calculate traceid in python not clickhouse to avoid nullable checks#52061
Conversation
|
| try: | ||
| # verify the trace_id is valid | ||
| base64.b64encode(bytes.fromhex(trace_id)).decode("ascii") | ||
| except ValueError: | ||
| return Response(status=status.HTTP_400_BAD_REQUEST) |
There was a problem hiding this comment.
Redundant computation — result is discarded
The full base64.b64encode(bytes.fromhex(trace_id)).decode("ascii") conversion is computed here purely for validation, but the result is thrown away. logic.py then performs the exact same computation again. This is a mild OnceAndOnlyOnce violation.
If you only need to verify the hex is valid, bytes.fromhex(trace_id) alone is sufficient — no need to do the base64 step:
try:
bytes.fromhex(trace_id)
except ValueError:
return Response(status=status.HTTP_400_BAD_REQUEST)Prompt To Fix With AI
This is a comment left during a code review.
Path: products/tracing/backend/presentation/views.py
Line: 87-91
Comment:
**Redundant computation — result is discarded**
The full `base64.b64encode(bytes.fromhex(trace_id)).decode("ascii")` conversion is computed here purely for validation, but the result is thrown away. `logic.py` then performs the exact same computation again. This is a mild OnceAndOnlyOnce violation.
If you only need to verify the hex is valid, `bytes.fromhex(trace_id)` alone is sufficient — no need to do the base64 step:
```python
try:
bytes.fromhex(trace_id)
except ValueError:
return Response(status=status.HTTP_400_BAD_REQUEST)
```
How can I resolve this? If you propose a fix, please make it concise.
Problem
hogql was wrapping the clickhouse comparison in ifNull checks, which breaks clickhouse's index analysis
assumeNotNull did not fix this - instead calculate in python so it's a constant check which should avoid the ifNull check
Changes
How did you test this code?
👉 Stay up-to-date with PostHog coding conventions for a smoother review.
Publish to changelog?
Docs update