Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions products/tracing/backend/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ def where(self) -> ast.Expr:
)

if self.query.traceId:
trace_id_b64 = base64.b64encode(bytes.fromhex(self.query.traceId)).decode("ascii")
exprs.append(
parse_expr(
"trace_id = assumeNotNull(base64Encode(unhex({traceId})))",
"trace_id = {traceId}",
placeholders={
"traceId": ast.Constant(value=self.query.traceId),
"traceId": ast.Constant(value=trace_id_b64),
},
)
)
Expand Down
5 changes: 5 additions & 0 deletions products/tracing/backend/presentation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def query(self, request: Request, *args, **kwargs) -> Response:
def trace(self, request: Request, trace_id: str, *args, **kwargs) -> Response:
query_data = request.data or {}
date_range = self.get_model(query_data.get("dateRange", {"date_from": "-24h"}), DateRange)
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)
Comment on lines +87 to +91
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


spans_query = TraceSpansQuery(
dateRange=date_range,
Expand Down
Loading