Skip to content

Conversation

@ben-fornefeld
Copy link
Member

@ben-fornefeld ben-fornefeld commented Dec 23, 2025

  • Removes sending redundant log arrays on each response
  • Adds a "direction" parameter, which is required to build a sophisticated logs viewer inside e2b-dev/dashboard
  • Renames "start" to "cursor"

Note

Adds a new logs API focused on structured entries and navigable ranges.

  • Introduces GET /v2/sandboxes/{sandboxID}/logs with cursor, limit, and direction (forward/backward); returns SandboxLogsV2Response (structured logs only)
  • Deprecates GET /sandboxes/{sandboxID}/logs in spec; keeps existing behavior for backward compatibility
  • Edge service: V1SandboxLogs now supports end and direction; default forward/backward handling wired through to Loki
  • Logger provider: QuerySandboxLogs now accepts direction and passes it to Loki
  • API handler (sandbox_logs_v2.go): maps request params to edge call with a max 7-day window around cursor
  • Updates OpenAPI specs, generated clients/types, enums, and routing to expose the new endpoint and parameters

Written by Cursor Bugbot for commit 7e0ef78. This will update automatically on new commits. Configure here.

- Introduced new endpoint (GET /v2/sandboxes/{sandboxID}/logs) to fetch structured logs for a specific sandbox.
- Added corresponding middleware and request handling in the server interface.
- Defined new parameters and response types for the V2 sandbox logs.
- Updated OpenAPI specifications to include the new endpoint and response structure.
- Merged QuerySandboxLogs and QuerySandboxLogsV2 into a single method with an updated signature to include the direction parameter.
- Removed the old QuerySandboxLogs method implementation to streamline the logging query process.
- Consolidated V2 sandbox logs handling into V1 API, removing the deprecated V2 endpoint.
- Updated method signatures to include optional parameters for start, end, limit, and direction.
- Adjusted response structures to streamline log entries and improve consistency.
- Enhanced OpenAPI specifications to reflect the changes in parameters and responses.
@ben-fornefeld ben-fornefeld added the improvement Improvement for current functionality label Dec 23, 2025
cursor := time.UnixMilli(*params.Cursor)
if direction == api.LogsDirectionForward {
start = cursor
end = cursor.Add(sandboxLogsMaxTimeRange)
Copy link

Choose a reason for hiding this comment

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

When direction is forward and cursor is near the present, end can extend into the future (line 52). This could cause unexpected behavior. Consider capping end at time.Now() to prevent querying future timestamps.

Copy link
Member Author

Choose a reason for hiding this comment

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

invalid, template build logs uses the same pattern, querying future timestamps in loki is harmless


start, end := time.Now().Add(-sandboxLogsMaxTimeRange), time.Now()
if params.Cursor != nil {
cursor := time.UnixMilli(*params.Cursor)
Copy link

Choose a reason for hiding this comment

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

The cursor timestamp is not validated before use. While the OpenAPI spec defines minimum: 0, consider adding validation to ensure the cursor is within a reasonable range (e.g., not older than 7 days, not in the future) to prevent excessive queries or invalid time ranges.

Copy link
Member Author

Choose a reason for hiding this comment

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

i think this is fine, it's handled the same in template build logs, excessive cursors just return empty results

}

if res.JSON200 == nil {
telemetry.ReportCriticalError(ctx, "error when returning logs for sandbox", fmt.Errorf("unexpected response for sandbox '%s': %s", sandboxID, string(res.Body)))
Copy link

Choose a reason for hiding this comment

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

Converting the entire response body to string for error logging could expose sensitive data or cause memory issues with large responses. Consider logging just the HTTP status code and truncating the body, or using a structured error response if available.

Copy link
Member Author

Choose a reason for hiding this comment

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

taken from sandbox logs v1

@ben-fornefeld ben-fornefeld marked this pull request as ready for review December 23, 2025 16:35
Copy link
Contributor

@dobrac dobrac left a comment

Choose a reason for hiding this comment

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

lgtm, just few nits

"500":
$ref: "#/components/responses/500"

/sandboxes/{sandboxID}/logs:
Copy link
Contributor

Choose a reason for hiding this comment

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

can you please make this endpoint deprecated?

properties:
logs:
default: []
description: Sandbox logs
Copy link
Contributor

Choose a reason for hiding this comment

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

why removal of this?

Copy link
Member Author

Choose a reason for hiding this comment

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

slipped through, thanks

ctx := c.Request.Context()
sandboxID = utils.ShortID(sandboxID)

team := c.Value(auth.TeamContextKey).(*types.Team)
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use GetTeamInfo instead

team := c.Value(auth.TeamContextKey).(*types.Team)

telemetry.SetAttributes(ctx,
attribute.String("instance.id", sandboxID),
Copy link
Contributor

Choose a reason for hiding this comment

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

use telemetry.WithSandboxID instead

Comment on lines 61 to 62
d := apiedge.V1SandboxLogsParamsDirectionForward
edgeDirection = &d
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use utils.ToPtr instead

@sitole sitole self-requested a review December 30, 2025 10:37
@sitole sitole self-assigned this Dec 30, 2025
@sitole
Copy link
Member

sitole commented Jan 13, 2026

Main PR with API migration is merged, I would wait for this #1713 one, and then I will apply your changes to the current main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improvement for current functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants