Skip to content

The only tool you need to know what is happening and how to fix it.

fix: derive session end from replay activity and speed up replay loading (#378)

Jovan Stojiljkovic676462cc793f
diff --git a/CLAUDE.md b/CLAUDE.md
index 6548f2b9..8248f534 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -1116,6 +1116,8 @@ Observability (emitted every 10s via `traceway.CaptureMetric`):
Sustained drops also fire a rate-limited (1/min) `traceway.CaptureException` so overload is visible in the issues feed without flooding it.
+**Session end is derived on read.** The browser SDK's closing payload is unreliable (a keepalive body over 64 KB is rejected on unload, and frozen mobile tabs never fire `pagehide`), so the stored `sessions.ended_at` is only a hint. Each segment row carries its client-side `ended_at` (`ch/0100`, `sqlite_telemetry/0027`, `duckdb_telemetry/0008`), and every session read joins the latest segment activity and applies `shared.ResolveSessionEnd`: an explicit end is kept only when it lands within `SessionIdleTimeout` (15 min, the SDK's inactivity timeout) after the last activity, otherwise the session ends at its last activity; it is still live while a segment arrived (server time) within the timeout. `HasRecording` is set when any segment exists, and the frontend labels an old session with neither as "No recording" (`utils/session-duration.ts`). Sorting the list by duration uses a SQL copy of the same rule in each backend (`sessionDurationSortKey`); `TestSessionRepository_EndFromRecordingActivity` holds the SQL order to the Go rule. `GET /api/sessions/:id/recording?startedAt=` bounds the segment lookup to the session's window, and segments are read from storage 16 at a time.
+
#### Users, Organizations & Projects
- **Users to organizations is many-to-many** via `organization_users`, one role per membership. A user joins additional organizations through invitations (`POST /api/invitations/:token/accept-existing` for existing accounts) and, in cloud mode, registration. Login/register/`LoginBundle` return every membership as `Organizations[]` with roles; the frontend keeps them in `authState.organizations` and groups the navbar project selector by organization when there is more than one.
diff --git a/backend/app/controllers/clientcontrollers/client.controller.go b/backend/app/controllers/clientcontrollers/client.controller.go
index 2c2f309c..8fffa31a 100644
--- a/backend/app/controllers/clientcontrollers/client.controller.go
+++ b/backend/app/controllers/clientcontrollers/client.controller.go
@@ -266,6 +266,7 @@ func (e clientController) Report(c *gin.Context) {
Key: key,
Body: body,
RecordedAt: time.Now().UTC(),
+ EndedAt: sr.EndedAt,
})
}
}
diff --git a/backend/app/controllers/session_detail.controller.go b/backend/app/controllers/session_detail.controller.go
index 155c0d55..de5e067b 100644
--- a/backend/app/controllers/session_detail.controller.go
+++ b/backend/app/controllers/session_detail.controller.go
@@ -10,6 +10,7 @@ import (
"github.com/tracewayapp/traceway/backend/app/models"
"github.com/tracewayapp/traceway/backend/app/repositories/telemetry"
"github.com/tracewayapp/traceway/backend/app/storage"
+ "golang.org/x/sync/errgroup"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
@@ -79,7 +80,7 @@ func (s sessionDetailController) GetSessionDetail(c *gin.Context) {
}
span = traceway.StartSpan(c, "loading session exceptions")
- exceptions, err := telemetry.ExceptionStackTraceRepository.FindAllBySessionId(c, projectId, sessionId)
+ exceptions, err := telemetry.ExceptionStackTraceRepository.FindAllBySessionId(c, projectId, sessionId, session.StartedAt)
span.End()
if err != nil {
c.AbortWithError(500, traceway.NewStackTraceErrorf("error loading session exceptions: %w", err))
@@ -116,56 +117,33 @@ func (s sessionDetailController) GetSessionRecording(c *gin.Context) {
return
}
+ var startedAt *time.Time
+ if t, err := time.Parse(time.RFC3339Nano, c.Query("startedAt")); err == nil {
+ startedAt = &t
+ }
+
span := traceway.StartSpan(c, "loading session segments")
- segments, err := telemetry.SessionRecordingRepository.FindBySessionId(c, projectId, sessionId)
+ segments, err := telemetry.SessionRecordingRepository.FindBySessionId(c, projectId, sessionId, startedAt)
span.End()
if err != nil {
c.AbortWithError(500, traceway.NewStackTraceErrorf("error loading session segments: %w", err))
return
}
+ span = traceway.StartSpan(c, "reading session segments")
+ bodies := readSegments(c.Request.Context(), segments)
+ span.End()
+
payload := SessionRecordingPayload{
Events: []json.RawMessage{},
}
-
- type segmentBody struct {
- Events json.RawMessage `json:"events"`
- Logs json.RawMessage `json:"logs"`
- Actions json.RawMessage `json:"actions"`
- StartedAt *time.Time `json:"startedAt"`
- EndedAt *time.Time `json:"endedAt"`
- }
-
- for _, seg := range segments {
- raw, err := storage.Store.Read(context.Background(), seg.FilePath)
- if err != nil {
- traceway.CaptureException(traceway.NewStackTraceErrorf("failed to read session segment (key=%s): %w", seg.FilePath, err))
+ for _, body := range bodies {
+ if body == nil {
continue
}
- var body segmentBody
- if err := json.Unmarshal(raw, &body); err != nil {
- traceway.CaptureException(traceway.NewStackTraceErrorf("failed to parse session segment (key=%s): %w", seg.FilePath, err))
- continue
- }
-
- if len(body.Events) > 0 {
- var events []json.RawMessage
- if err := json.Unmarshal(body.Events, &events); err == nil {
- payload.Events = append(payload.Events, events...)
- }
- }
- if len(body.Logs) > 0 {
- var logs []json.RawMessage
- if err := json.Unmarshal(body.Logs, &logs); err == nil {
- payload.Logs = append(payload.Logs, logs...)
- }
- }
- if len(body.Actions) > 0 {
- var actions []json.RawMessage
- if err := json.Unmarshal(body.Actions, &actions); err == nil {
- payload.Actions = append(payload.Actions, actions...)
- }
- }
+ payload.Events = append(payload.Events, body.events...)
+ payload.Logs = append(payload.Logs, body.logs...)
+ payload.Actions = append(payload.Actions, body.actions...)
if body.StartedAt != nil && (payload.StartedAt == nil || body.StartedAt.Before(*payload.StartedAt)) {
t := *body.StartedAt
payload.StartedAt = &t
@@ -179,4 +157,59 @@ func (s sessionDetailController) GetSessionRecording(c *gin.Context) {
c.JSON(http.StatusOK, payload)
}
+const segmentReadConcurrency = 16
+
+type segmentBody struct {
+ Events json.RawMessage `json:"events"`
+ Logs json.RawMessage `json:"logs"`
+ Actions json.RawMessage `json:"actions"`
+ StartedAt *time.Time `json:"startedAt"`
+ EndedAt *time.Time `json:"endedAt"`
+
+ events []json.RawMessage
+ logs []json.RawMessage
+ actions []json.RawMessage
+}
+
+func readSegments(ctx context.Context, segments []models.SessionRecording) []*segmentBody {
+ bodies := make([]*segmentBody, len(segments))
+ var group errgroup.Group
+ group.SetLimit(segmentReadConcurrency)
+ for i, seg := range segments {
+ group.Go(func() error {
+ raw, err := storage.Store.Read(ctx, seg.FilePath)
+ if err != nil {
+ if ctx.Err() == nil {
+ traceway.CaptureException(traceway.NewStackTraceErrorf("failed to read session segment (key=%s): %w", seg.FilePath, err))
+ }
+ return nil
+ }
+ var body segmentBody
+ if err := json.Unmarshal(raw, &body); err != nil {
+ traceway.CaptureException(traceway.NewStackTraceErrorf("failed to parse session segment (key=%s): %w", seg.FilePath, err))
+ return nil
+ }
+ body.events = decodeArray(body.Events)
+ body.logs = decodeArray(body.Logs)
+ body.actions = decodeArray(body.Actions)
+ body.Events, body.Logs, body.Actions = nil, nil, nil
+ bodies[i] = &body
+ return nil
+ })
+ }
+ _ = group.Wait()
+ return bodies
+}
+
+func decodeArray(raw json.RawMessage) []json.RawMessage {
+ if len(raw) == 0 {
+ return nil
+ }
+ var items []json.RawMessage
+ if err := json.Unmarshal(raw, &items); err != nil {
+ return nil
+ }
+ return items
+}
+
var SessionDetailController = sessionDetailController{}
diff --git a/backend/app/migrations/ch/0100_add_ended_at_to_session_recordings.up.sql b/backend/app/migrations/ch/0100_add_ended_at_to_session_recordings.up.sql
new file mode 100644
index 00000000..22f90ee7
--- /dev/null
+++ b/backend/app/migrations/ch/0100_add_ended_at_to_session_recordings.up.sql
@@ -0,0 +1 @@
+ALTER TABLE session_recordings ADD COLUMN IF NOT EXISTS ended_at Nullable(DateTime64(3)) DEFAULT NULL
diff --git a/backend/app/migrations/duckdb_telemetry/0008_add_ended_at_to_session_recordings.up.sql b/backend/app/migrations/duckdb_telemetry/0008_add_ended_at_to_session_recordings.up.sql
new file mode 100644
index 00000000..691266a6
--- /dev/null
+++ b/backend/app/migrations/duckdb_telemetry/0008_add_ended_at_to_session_recordings.up.sql
@@ -0,0 +1 @@
+ALTER TABLE session_recordings ADD COLUMN ended_at TIMESTAMP;
diff --git a/backend/app/migrations/sqlite_telemetry/0027_add_ended_at_to_session_recordings.up.sql b/backend/app/migrations/sqlite_telemetry/0027_add_ended_at_to_session_recordings.up.sql
new file mode 100644
index 00000000..84be9fd9
--- /dev/null
+++ b/backend/app/migrations/sqlite_telemetry/0027_add_ended_at_to_session_recordings.up.sql
@@ -0,0 +1,3 @@
+ALTER TABLE session_recordings ADD COLUMN ended_at DATETIME DEFAULT NULL;
+
+CREATE INDEX IF NOT EXISTS idx_session_recordings_project_recorded ON session_recordings(project_id, recorded_at);
diff --git a/backend/app/models/session.model.go b/backend/app/models/session.model.go
index fd0886e6..b48349b0 100644
--- a/backend/app/models/session.model.go
+++ b/backend/app/models/session.model.go
@@ -7,14 +7,15 @@ import (
)
type Session struct {
- Id uuid.UUID `json:"id" ch:"id"`
- ProjectId uuid.UUID `json:"projectId" ch:"project_id"`
- StartedAt time.Time `json:"startedAt" ch:"started_at"`
- EndedAt *time.Time `json:"endedAt,omitempty" ch:"ended_at"`
- Duration int64 `json:"duration" ch:"duration"`
- ClientIP string `json:"clientIP" ch:"client_ip"`
- Attributes map[string]string `json:"attributes" ch:"attributes"`
- AppVersion string `json:"appVersion" ch:"app_version"`
- ServerName string `json:"serverName" ch:"server_name"`
- TraceId string `json:"traceId,omitempty" ch:"trace_id"`
+ Id uuid.UUID `json:"id" ch:"id"`
+ ProjectId uuid.UUID `json:"projectId" ch:"project_id"`
+ StartedAt time.Time `json:"startedAt" ch:"started_at"`
+ EndedAt *time.Time `json:"endedAt,omitempty" ch:"ended_at"`
+ Duration int64 `json:"duration" ch:"duration"`
+ ClientIP string `json:"clientIP" ch:"client_ip"`
+ Attributes map[string]string `json:"attributes" ch:"attributes"`
+ AppVersion string `json:"appVersion" ch:"app_version"`
+ ServerName string `json:"serverName" ch:"server_name"`
+ TraceId string `json:"traceId,omitempty" ch:"trace_id"`
+ HasRecording bool `json:"hasRecording" ch:"-"`
}
diff --git a/backend/app/models/session_recording.model.go b/backend/app/models/session_recording.model.go
index 2db5fc24..f44bf922 100644
--- a/backend/app/models/session_recording.model.go
+++ b/backend/app/models/session_recording.model.go
@@ -14,4 +14,5 @@ type SessionRecording struct {
SegmentIndex int32 `json:"segmentIndex" ch:"segment_index"`
FilePath string `json:"filePath" ch:"file_path"`
RecordedAt time.Time `json:"recordedAt" ch:"recorded_at"`
+ EndedAt *time.Time `json:"endedAt,omitempty" ch:"ended_at"`
}
diff --git a/backend/app/recordings/uploader.go b/backend/app/recordings/uploader.go
index 96af2f44..c9c97a7e 100644
--- a/backend/app/recordings/uploader.go
+++ b/backend/app/recordings/uploader.go
@@ -35,6 +35,7 @@ type Job struct {
Key string
Body []byte
RecordedAt time.Time
+ EndedAt *time.Time
}
type storer interface {
@@ -172,6 +173,7 @@ func (p *pool) handle(ctx context.Context, j Job) {
SegmentIndex: j.SegmentIndex,
FilePath: j.Key,
RecordedAt: j.RecordedAt,
+ EndedAt: j.EndedAt,
}
select {
case p.inserts <- rec:
diff --git a/backend/app/repositories/telemetry/clickhouse/exception_stack_trace.repository.go b/backend/app/repositories/telemetry/clickhouse/exception_stack_trace.repository.go
index e35aa70f..8029e71f 100644
--- a/backend/app/repositories/telemetry/clickhouse/exception_stack_trace.repository.go
+++ b/backend/app/repositories/telemetry/clickhouse/exception_stack_trace.repository.go
@@ -44,13 +44,14 @@ func (e *exceptionStackTraceRepository) InsertAsync(ctx context.Context, lines [
}
// FindAllBySessionId returns all exceptions/messages stamped with the given session_id.
-func (e *exceptionStackTraceRepository) FindAllBySessionId(ctx context.Context, projectId, sessionId uuid.UUID) ([]models.ExceptionStackTrace, error) {
+func (e *exceptionStackTraceRepository) FindAllBySessionId(ctx context.Context, projectId, sessionId uuid.UUID, startedAt time.Time) ([]models.ExceptionStackTrace, error) {
+ from, to := shared.TraceWindowBounds(startedAt)
rows, err := chdb.Conn.Query(ctx,
`SELECT id, project_id, trace_id, span_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, session_id
FROM exceptions_v2
- WHERE project_id = ? AND session_id = ?
+ WHERE project_id = ? AND session_id = ? AND recorded_at >= ? AND recorded_at <= ?
ORDER BY recorded_at ASC`,
- projectId, sessionId)
+ projectId, sessionId, from, to)
if err != nil {
return nil, err
}
diff --git a/backend/app/repositories/telemetry/clickhouse/session.repository.go b/backend/app/repositories/telemetry/clickhouse/session.repository.go
index ea07a4ce..f858bbf8 100644
--- a/backend/app/repositories/telemetry/clickhouse/session.repository.go
+++ b/backend/app/repositories/telemetry/clickhouse/session.repository.go
@@ -6,6 +6,7 @@ import (
"context"
"database/sql"
"errors"
+ "fmt"
"github.com/tracewayapp/traceway/backend/app/repositories/telemetry/shared"
"strings"
"time"
@@ -55,12 +56,9 @@ func (r *sessionRepository) FindAll(ctx context.Context, projectId uuid.UUID, fr
return nil, 0, err
}
- allowedOrderBy := map[string]bool{
- "started_at": true,
- "duration": true,
- }
- if !allowedOrderBy[orderBy] {
- orderBy = "started_at"
+ orderExpr := "s.started_at"
+ if orderBy == "duration" {
+ orderExpr = sessionDurationSortKey
}
sortDir := "DESC"
if sortDirection == "asc" {
@@ -69,9 +67,15 @@ func (r *sessionRepository) FindAll(ctx context.Context, projectId uuid.UUID, fr
offset := (page - 1) * pageSize
- query := "SELECT id, project_id, started_at, ended_at, duration, client_ip, attributes, app_version, server_name, trace_id FROM sessions FINAL WHERE project_id = ? AND started_at >= ? AND started_at <= ?" + whereExtra + " ORDER BY " + orderBy + " " + sortDir + " LIMIT ? OFFSET ?"
+ recFrom, recTo := shared.SessionRecordingWindow(fromDate, toDate)
+ query := "SELECT s.id, s.project_id, s.started_at, s.ended_at, s.duration, s.client_ip, s.attributes, s.app_version, s.server_name, s.trace_id, a.last_activity, a.last_received" +
+ " FROM (SELECT id, project_id, started_at, ended_at, duration, client_ip, attributes, app_version, server_name, trace_id FROM sessions FINAL WHERE project_id = ? AND started_at >= ? AND started_at <= ?" + whereExtra + ") AS s" +
+ " LEFT JOIN (" + sessionActivityQuery + ") AS a ON a.sid = s.id" +
+ " ORDER BY " + orderExpr + " " + sortDir + " LIMIT ? OFFSET ?" +
+ " SETTINGS join_use_nulls = 1"
queryArgs := []interface{}{projectId, fromDate, toDate}
queryArgs = append(queryArgs, extraArgs...)
+ queryArgs = append(queryArgs, projectId, recFrom, recTo)
queryArgs = append(queryArgs, pageSize, offset)
rows, err := chdb.Conn.Query(ctx, query, queryArgs...)
if err != nil {
@@ -79,17 +83,38 @@ func (r *sessionRepository) FindAll(ctx context.Context, projectId uuid.UUID, fr
}
defer rows.Close()
+ now := time.Now()
var sessions []models.Session
for rows.Next() {
var s models.Session
- if err := rows.Scan(&s.Id, &s.ProjectId, &s.StartedAt, &s.EndedAt, &s.Duration, &s.ClientIP, &s.Attributes, &s.AppVersion, &s.ServerName, &s.TraceId); err != nil {
+ var activity shared.SessionActivity
+ if err := rows.Scan(&s.Id, &s.ProjectId, &s.StartedAt, &s.EndedAt, &s.Duration, &s.ClientIP, &s.Attributes, &s.AppVersion, &s.ServerName, &s.TraceId, &activity.LastActivity, &activity.LastReceived); err != nil {
return nil, 0, err
}
+ shared.ResolveSessionEnd(&s, activity, now)
sessions = append(sessions, s)
}
+ if err := rows.Err(); err != nil {
+ return nil, 0, err
+ }
return sessions, int64(count), nil
}
+const sessionActivityQuery = `SELECT assumeNotNull(session_id) AS sid,
+ max(coalesce(ended_at, toDateTime64(recorded_at, 3))) AS last_activity,
+ max(recorded_at) AS last_received
+ FROM session_recordings
+ WHERE project_id = ? AND session_id IS NOT NULL AND recorded_at >= ? AND recorded_at <= ?
+ GROUP BY sid`
+
+var sessionDurationSortKey = fmt.Sprintf(`if(a.last_activity IS NULL, intDiv(s.duration, 1000000),
+ toInt64(dateDiff('millisecond', toDateTime64(s.started_at, 3), if(
+ s.ended_at > greatest(assumeNotNull(a.last_activity), toDateTime64(s.started_at, 3))
+ AND s.ended_at < greatest(assumeNotNull(a.last_activity), toDateTime64(s.started_at, 3)) + INTERVAL %d MINUTE,
+ toDateTime64(assumeNotNull(s.ended_at), 3),
+ greatest(assumeNotNull(a.last_activity), toDateTime64(s.started_at, 3))))))`,
+ int(shared.SessionIdleTimeout/time.Minute))
+
func (r *sessionRepository) FindById(ctx context.Context, projectId, sessionId uuid.UUID, startedAt *time.Time) (*models.Session, error) {
var s models.Session
@@ -113,6 +138,23 @@ func (r *sessionRepository) FindById(ctx context.Context, projectId, sessionId u
}
return nil, err
}
+
+ from, to := shared.TraceWindowBounds(s.StartedAt)
+ var segments uint64
+ var lastActivity, lastReceived time.Time
+ err = chdb.Conn.QueryRow(ctx,
+ `SELECT count(), max(coalesce(ended_at, toDateTime64(recorded_at, 3))), max(recorded_at)
+ FROM session_recordings
+ WHERE project_id = ? AND session_id = ? AND recorded_at >= ? AND recorded_at <= ?`,
+ projectId, sessionId, from, to).Scan(&segments, &lastActivity, &lastReceived)
+ if err != nil {
+ return nil, err
+ }
+ var activity shared.SessionActivity
+ if segments > 0 {
+ activity = shared.SessionActivity{LastActivity: &lastActivity, LastReceived: &lastReceived}
+ }
+ shared.ResolveSessionEnd(&s, activity, time.Now())
return &s, nil
}
diff --git a/backend/app/repositories/telemetry/clickhouse/session_recording.repository.go b/backend/app/repositories/telemetry/clickhouse/session_recording.repository.go
index f8283f0d..7cfb659e 100644
--- a/backend/app/repositories/telemetry/clickhouse/session_recording.repository.go
+++ b/backend/app/repositories/telemetry/clickhouse/session_recording.repository.go
@@ -8,6 +8,8 @@ import (
"errors"
"github.com/tracewayapp/traceway/backend/app/chdb"
"github.com/tracewayapp/traceway/backend/app/models"
+ "github.com/tracewayapp/traceway/backend/app/repositories/telemetry/shared"
+ "time"
"github.com/google/uuid"
)
@@ -18,12 +20,12 @@ func (r *sessionRecordingRepository) InsertAsync(ctx context.Context, recordings
if len(recordings) == 0 {
return nil
}
- batch, err := chdb.Conn.PrepareBatch(chdb.BatchCtx(), "INSERT INTO session_recordings (id, project_id, exception_id, session_id, segment_index, file_path, recorded_at)")
+ batch, err := chdb.Conn.PrepareBatch(chdb.BatchCtx(), "INSERT INTO session_recordings (id, project_id, exception_id, session_id, segment_index, file_path, recorded_at, ended_at)")
if err != nil {
return err
}
for _, rec := range recordings {
- if err := batch.Append(rec.Id, rec.ProjectId, rec.ExceptionId, rec.SessionId, rec.SegmentIndex, rec.FilePath, rec.RecordedAt); err != nil {
+ if err := batch.Append(rec.Id, rec.ProjectId, rec.ExceptionId, rec.SessionId, rec.SegmentIndex, rec.FilePath, rec.RecordedAt, rec.EndedAt); err != nil {
return err
}
}
@@ -45,10 +47,17 @@ func (r *sessionRecordingRepository) FindByExceptionId(ctx context.Context, proj
}
// FindBySessionId returns all recording segments for a session ordered by segment_index.
-func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projectId, sessionId uuid.UUID) ([]models.SessionRecording, error) {
- rows, err := chdb.Conn.Query(ctx,
- "SELECT id, project_id, exception_id, session_id, segment_index, file_path, recorded_at FROM session_recordings WHERE project_id = ? AND session_id = ? ORDER BY segment_index ASC, recorded_at ASC",
- projectId, sessionId)
+func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projectId, sessionId uuid.UUID, startedAt *time.Time) ([]models.SessionRecording, error) {
+ query := "SELECT id, project_id, exception_id, session_id, segment_index, file_path, recorded_at, ended_at FROM session_recordings WHERE project_id = ? AND session_id = ?"
+ args := []any{projectId, sessionId}
+ if startedAt != nil {
+ from, to := shared.TraceWindowBounds(*startedAt)
+ query += " AND recorded_at >= ? AND recorded_at <= ?"
+ args = append(args, from, to)
+ }
+ query += " ORDER BY segment_index ASC, recorded_at ASC"
+
+ rows, err := chdb.Conn.Query(ctx, query, args...)
if err != nil {
return nil, err
}
@@ -57,12 +66,12 @@ func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projec
var recordings []models.SessionRecording
for rows.Next() {
var rec models.SessionRecording
- if err := rows.Scan(&rec.Id, &rec.ProjectId, &rec.ExceptionId, &rec.SessionId, &rec.SegmentIndex, &rec.FilePath, &rec.RecordedAt); err != nil {
+ if err := rows.Scan(&rec.Id, &rec.ProjectId, &rec.ExceptionId, &rec.SessionId, &rec.SegmentIndex, &rec.FilePath, &rec.RecordedAt, &rec.EndedAt); err != nil {
return nil, err
}
recordings = append(recordings, rec)
}
- return recordings, nil
+ return recordings, rows.Err()
}
var SessionRecordingRepository = &sessionRecordingRepository{}
diff --git a/backend/app/repositories/telemetry/duckdb/exception_stack_trace.repository.go b/backend/app/repositories/telemetry/duckdb/exception_stack_trace.repository.go
index 8f2edf45..1a36181a 100644
--- a/backend/app/repositories/telemetry/duckdb/exception_stack_trace.repository.go
+++ b/backend/app/repositories/telemetry/duckdb/exception_stack_trace.repository.go
@@ -509,13 +509,14 @@ func (e *exceptionStackTraceRepository) FindByTraceIds(ctx context.Context, trac
}
// FindAllBySessionId returns all exceptions/messages stamped with the given session_id, ordered by time.
-func (e *exceptionStackTraceRepository) FindAllBySessionId(ctx context.Context, projectId, sessionId uuid.UUID) ([]models.ExceptionStackTrace, error) {
+func (e *exceptionStackTraceRepository) FindAllBySessionId(ctx context.Context, projectId, sessionId uuid.UUID, startedAt time.Time) ([]models.ExceptionStackTrace, error) {
+ from, to := shared.TraceWindowBounds(startedAt)
rows, err := lit.SelectNamed[exceptionRow](db.TelemetryDB,
`SELECT id, project_id, trace_id, span_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, session_id
FROM exceptions_v2
- WHERE project_id = :project_id AND session_id = :session_id
+ WHERE project_id = :project_id AND session_id = :session_id AND recorded_at >= :from AND recorded_at <= :to
ORDER BY recorded_at ASC`,
- lit.P{"project_id": projectId, "session_id": sessionId})
+ lit.P{"project_id": projectId, "session_id": sessionId, "from": from.UTC(), "to": to.UTC()})
if err != nil {
return nil, err
}
diff --git a/backend/app/repositories/telemetry/duckdb/session.repository.go b/backend/app/repositories/telemetry/duckdb/session.repository.go
index c51de09b..482b3c72 100644
--- a/backend/app/repositories/telemetry/duckdb/session.repository.go
+++ b/backend/app/repositories/telemetry/duckdb/session.repository.go
@@ -28,6 +28,9 @@ type sessionRow struct {
AppVersion string `lit:"app_version"`
ServerName string `lit:"server_name"`
TraceId string `lit:"trace_id"`
+
+ LastActivity *sqlitetypes.SQLiteTime `lit:"last_activity"`
+ LastReceived *sqlitetypes.SQLiteTime `lit:"last_received"`
}
type sessionRowNaming struct{ lit.DefaultDbNamingStrategy }
@@ -60,9 +63,27 @@ func (row *sessionRow) toModel() models.Session {
if row.Attributes != nil {
s.Attributes = map[string]string(row.Attributes)
}
+ activity := sqlitetypes.SessionActivityResult{LastActivity: row.LastActivity, LastReceived: row.LastReceived}
+ shared.ResolveSessionEnd(&s, activity.Activity(), time.Now())
return s
}
+const sessionActivityJoin = ` LEFT JOIN (
+ SELECT session_id,
+ MAX(COALESCE(ended_at, recorded_at)) AS last_activity,
+ MAX(recorded_at) AS last_received
+ FROM session_recordings
+ WHERE project_id = :project_id AND session_id IS NOT NULL AND recorded_at >= :rec_from AND recorded_at <= :rec_to
+ GROUP BY session_id
+) AS activity ON activity.session_id = sessions.id`
+
+var sessionDurationSortKey = fmt.Sprintf(`CASE WHEN activity.last_activity IS NULL THEN CAST(sessions.duration // 1000000 AS BIGINT)
+ ELSE epoch_ms(CASE
+ WHEN sessions.ended_at > greatest(activity.last_activity, sessions.started_at)
+ AND sessions.ended_at < greatest(activity.last_activity, sessions.started_at) + INTERVAL %d MINUTE
+ THEN sessions.ended_at ELSE greatest(activity.last_activity, sessions.started_at) END) - epoch_ms(sessions.started_at) END`,
+ int(shared.SessionIdleTimeout/time.Minute))
+
type sessionRepository struct{}
// Upsert uses DuckDB ON CONFLICT rather than the Appender because sessions carry a
@@ -159,12 +180,9 @@ func (r *sessionRepository) FindAll(ctx context.Context, projectId uuid.UUID, fr
count = int64(countResult.Count)
}
- allowedOrderBy := map[string]bool{
- "started_at": true,
- "duration": true,
- }
- if !allowedOrderBy[orderBy] {
- orderBy = "started_at"
+ orderExpr := "sessions.started_at"
+ if orderBy == "duration" {
+ orderExpr = sessionDurationSortKey
}
sortDir := "DESC"
if sortDirection == "asc" {
@@ -172,12 +190,16 @@ func (r *sessionRepository) FindAll(ctx context.Context, projectId uuid.UUID, fr
}
offset := (page - 1) * pageSize
- query := "SELECT id, project_id, started_at, ended_at, duration, client_ip, attributes, app_version, server_name, trace_id FROM sessions WHERE project_id = :project_id AND started_at >= :start AND started_at <= :end" + whereExtra + " ORDER BY " + orderBy + " " + sortDir + " LIMIT :limit OFFSET :offset"
+ query := "SELECT sessions.id, sessions.project_id, sessions.started_at, sessions.ended_at, sessions.duration, sessions.client_ip, sessions.attributes, sessions.app_version, sessions.server_name, sessions.trace_id, activity.last_activity, activity.last_received FROM sessions" + sessionActivityJoin +
+ " WHERE sessions.project_id = :project_id AND sessions.started_at >= :start AND sessions.started_at <= :end" + whereExtra + " ORDER BY " + orderExpr + " " + sortDir + " LIMIT :limit OFFSET :offset"
+ recFrom, recTo := shared.SessionRecordingWindow(fromDate, toDate)
queryParams := lit.P{
"project_id": projectId,
"start": fromDate.UTC(),
"end": toDate.UTC(),
+ "rec_from": recFrom.UTC(),
+ "rec_to": recTo.UTC(),
"limit": pageSize,
"offset": offset,
}
@@ -243,6 +265,17 @@ func (r *sessionRepository) FindById(ctx context.Context, projectId, sessionId u
if row == nil {
return nil, nil
}
+
+ from, to := shared.TraceWindowBounds(row.StartedAt.Time)
+ activity, err := lit.SelectSingleNamed[sqlitetypes.SessionActivityResult](db.TelemetryDB,
+ "SELECT MAX(COALESCE(ended_at, recorded_at)) AS last_activity, MAX(recorded_at) AS last_received FROM session_recordings WHERE project_id = :project_id AND session_id = :session_id AND recorded_at >= :from AND recorded_at <= :to",
+ lit.P{"project_id": projectId, "session_id": sessionId, "from": from.UTC(), "to": to.UTC()})
+ if err != nil {
+ return nil, err
+ }
+ if activity != nil {
+ row.LastActivity, row.LastReceived = activity.LastActivity, activity.LastReceived
+ }
s := row.toModel()
return &s, nil
}
diff --git a/backend/app/repositories/telemetry/duckdb/session_recording.repository.go b/backend/app/repositories/telemetry/duckdb/session_recording.repository.go
index 715d0e9e..be066408 100644
--- a/backend/app/repositories/telemetry/duckdb/session_recording.repository.go
+++ b/backend/app/repositories/telemetry/duckdb/session_recording.repository.go
@@ -6,7 +6,9 @@ import (
"context"
"database/sql"
"fmt"
+ "time"
+ "github.com/tracewayapp/traceway/backend/app/repositories/telemetry/shared"
"github.com/tracewayapp/traceway/backend/app/repositories/telemetry/sqlitetypes"
"github.com/duckdb/duckdb-go/v2"
@@ -17,13 +19,14 @@ import (
)
type sessionRecording struct {
- Id uuid.UUID `lit:"id"`
- ProjectId uuid.UUID `lit:"project_id"`
- ExceptionId uuid.UUID `lit:"exception_id"`
- SessionId *uuid.UUID `lit:"session_id"`
- SegmentIndex int32 `lit:"segment_index"`
- FilePath string `lit:"file_path"`
- RecordedAt sqlitetypes.SQLiteTime `lit:"recorded_at"`
+ Id uuid.UUID `lit:"id"`
+ ProjectId uuid.UUID `lit:"project_id"`
+ ExceptionId uuid.UUID `lit:"exception_id"`
+ SessionId *uuid.UUID `lit:"session_id"`
+ SegmentIndex int32 `lit:"segment_index"`
+ FilePath string `lit:"file_path"`
+ RecordedAt sqlitetypes.SQLiteTime `lit:"recorded_at"`
+ EndedAt *sqlitetypes.SQLiteTime `lit:"ended_at"`
}
func init() {
@@ -50,7 +53,11 @@ func (r *sessionRecordingRepository) InsertAsync(ctx context.Context, recordings
s := rec.SessionId.String()
sessionId = &s
}
- // DDL column order: id, project_id, exception_id, file_path, recorded_at, session_id, segment_index
+ var endedAt any
+ if rec.EndedAt != nil {
+ endedAt = rec.EndedAt.UTC()
+ }
+ // DDL column order: id, project_id, exception_id, file_path, recorded_at, session_id, segment_index, ended_at
if err := appender.AppendRow(
rec.Id.String(),
rec.ProjectId.String(),
@@ -59,6 +66,7 @@ func (r *sessionRecordingRepository) InsertAsync(ctx context.Context, recordings
rec.RecordedAt.UTC(),
nullableString(sessionId),
int64(rec.SegmentIndex),
+ endedAt,
); err != nil {
captureDroppedRow("session_recordings", err)
dropped++
@@ -88,17 +96,25 @@ func (r *sessionRecordingRepository) FindByExceptionId(ctx context.Context, proj
}
// FindBySessionId returns all recording segments for a session ordered by segment_index.
-func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projectId, sessionId uuid.UUID) ([]models.SessionRecording, error) {
- rows, err := lit.SelectNamed[sessionRecording](db.TelemetryDB,
- "SELECT id, project_id, exception_id, session_id, segment_index, file_path, recorded_at FROM session_recordings WHERE project_id = :project_id AND session_id = :session_id ORDER BY segment_index ASC, recorded_at ASC",
- lit.P{"project_id": projectId, "session_id": sessionId})
+func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projectId, sessionId uuid.UUID, startedAt *time.Time) ([]models.SessionRecording, error) {
+ query := "SELECT id, project_id, exception_id, session_id, segment_index, file_path, recorded_at, ended_at FROM session_recordings WHERE project_id = :project_id AND session_id = :session_id"
+ params := lit.P{"project_id": projectId, "session_id": sessionId}
+ if startedAt != nil {
+ from, to := shared.TraceWindowBounds(*startedAt)
+ query += " AND recorded_at >= :from AND recorded_at <= :to"
+ params["from"] = from.UTC()
+ params["to"] = to.UTC()
+ }
+ query += " ORDER BY segment_index ASC, recorded_at ASC"
+
+ rows, err := lit.SelectNamed[sessionRecording](db.TelemetryDB, query, params)
if err != nil {
return nil, err
}
out := make([]models.SessionRecording, 0, len(rows))
for _, row := range rows {
- out = append(out, models.SessionRecording{
+ rec := models.SessionRecording{
Id: row.Id,
ProjectId: row.ProjectId,
ExceptionId: row.ExceptionId,
@@ -106,7 +122,12 @@ func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projec
SegmentIndex: row.SegmentIndex,
FilePath: row.FilePath,
RecordedAt: row.RecordedAt.Time,
- })
+ }
+ if row.EndedAt != nil {
+ t := row.EndedAt.Time
+ rec.EndedAt = &t
+ }
+ out = append(out, rec)
}
return out, nil
}
diff --git a/backend/app/repositories/telemetry/session_repository_test.go b/backend/app/repositories/telemetry/session_repository_test.go
index b1e19125..f042ac59 100644
--- a/backend/app/repositories/telemetry/session_repository_test.go
+++ b/backend/app/repositories/telemetry/session_repository_test.go
@@ -92,3 +92,85 @@ func TestSessionRepository_SearchAttributes(t *testing.T) {
})
}
}
+
+func TestSessionRepository_EndFromRecordingActivity(t *testing.T) {
+ setupTestDB(t)
+ ctx := context.Background()
+ projectID := uuid.New()
+ now := time.Now().UTC().Truncate(time.Second)
+ ago := func(d time.Duration) time.Time { return now.Add(-d) }
+ ptr := func(v time.Time) *time.Time { return &v }
+
+ idle := models.Session{Id: uuid.New(), ProjectId: projectID, StartedAt: ago(60 * time.Minute)}
+ closedByPagehide := models.Session{Id: uuid.New(), ProjectId: projectID, StartedAt: ago(40 * time.Minute), EndedAt: ptr(ago(38 * time.Minute)), Duration: int64(2 * time.Minute)}
+ closedByLateTimer := models.Session{Id: uuid.New(), ProjectId: projectID, StartedAt: ago(90 * time.Minute), EndedAt: ptr(ago(30 * time.Minute)), Duration: int64(60 * time.Minute)}
+ live := models.Session{Id: uuid.New(), ProjectId: projectID, StartedAt: ago(10 * time.Minute)}
+ unrecorded := models.Session{Id: uuid.New(), ProjectId: projectID, StartedAt: ago(70 * time.Minute)}
+ skewedClock := models.Session{Id: uuid.New(), ProjectId: projectID, StartedAt: ago(30 * time.Minute), EndedAt: ptr(ago(16 * time.Minute)), Duration: int64(14 * time.Minute)}
+ if err := SessionRepository.Upsert(ctx, []models.Session{idle, closedByPagehide, closedByLateTimer, live, unrecorded, skewedClock}); err != nil {
+ t.Fatal(err)
+ }
+
+ segment := func(s models.Session, index int32, end time.Time) models.SessionRecording {
+ return models.SessionRecording{Id: uuid.New(), ProjectId: projectID, SessionId: &s.Id, SegmentIndex: index, FilePath: "k", RecordedAt: end, EndedAt: ptr(end)}
+ }
+ if err := SessionRecordingRepository.InsertAsync(ctx, []models.SessionRecording{
+ segment(idle, 0, ago(55*time.Minute)),
+ segment(idle, 1, ago(50*time.Minute)),
+ segment(closedByPagehide, 0, ago(38*time.Minute-400*time.Millisecond)),
+ segment(closedByLateTimer, 0, ago(85*time.Minute)),
+ segment(live, 0, ago(time.Minute)),
+ segment(skewedClock, 0, ago(32*time.Minute)),
+ }); err != nil {
+ t.Fatal(err)
+ }
+
+ want := []struct {
+ session models.Session
+ duration time.Duration
+ ended bool
+ recorded bool
+ }{
+ {skewedClock, 14 * time.Minute, true, true},
+ {idle, 10 * time.Minute, true, true},
+ {live, 9 * time.Minute, false, true},
+ {closedByLateTimer, 5 * time.Minute, true, true},
+ {closedByPagehide, 2 * time.Minute, true, true},
+ {unrecorded, 0, false, false},
+ }
+
+ rows, total, err := SessionRepository.FindAll(ctx, projectID, ago(2*time.Hour), now, 1, 10, "duration", "desc", "", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if total != int64(len(want)) || len(rows) != len(want) {
+ t.Fatalf("got %d rows (total %d), want %d", len(rows), total, len(want))
+ }
+ for i, w := range want {
+ got := rows[i]
+ if got.Id != w.session.Id {
+ t.Fatalf("row %d is %s, want %s", i, got.Id, w.session.Id)
+ }
+ if time.Duration(got.Duration).Round(time.Second) != w.duration || (got.EndedAt != nil) != w.ended || got.HasRecording != w.recorded {
+ t.Fatalf("row %d: duration=%v ended=%v recorded=%v, want %v %v %v", i, time.Duration(got.Duration), got.EndedAt != nil, got.HasRecording, w.duration, w.ended, w.recorded)
+ }
+
+ byId, err := SessionRepository.FindById(ctx, projectID, w.session.Id, &w.session.StartedAt)
+ if err != nil || byId == nil {
+ t.Fatalf("FindById(%s): %+v: %v", w.session.Id, byId, err)
+ }
+ if byId.Duration != got.Duration || (byId.EndedAt != nil) != w.ended || byId.HasRecording != w.recorded {
+ t.Fatalf("FindById disagrees with FindAll for row %d: %+v vs %+v", i, byId, got)
+ }
+ }
+
+ asc, _, err := SessionRepository.FindAll(ctx, projectID, ago(2*time.Hour), now, 1, 10, "duration", "asc", "", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for i := range asc {
+ if asc[i].Id != want[len(want)-1-i].session.Id {
+ t.Fatalf("ascending row %d is %s, want %s", i, asc[i].Id, want[len(want)-1-i].session.Id)
+ }
+ }
+}
diff --git a/backend/app/repositories/telemetry/shared/session_activity.go b/backend/app/repositories/telemetry/shared/session_activity.go
new file mode 100644
index 00000000..7726f9f2
--- /dev/null
+++ b/backend/app/repositories/telemetry/shared/session_activity.go
@@ -0,0 +1,53 @@
+package shared
+
+import (
+ "time"
+
+ "github.com/tracewayapp/traceway/backend/app/models"
+)
+
+const SessionIdleTimeout = 15 * time.Minute
+
+const sessionEndTolerance = time.Second
+
+type SessionActivity struct {
+ LastActivity *time.Time
+ LastReceived *time.Time
+}
+
+func SessionRecordingWindow(from, to time.Time) (time.Time, time.Time) {
+ lower, _ := TraceWindowBounds(from)
+ _, upper := TraceWindowBounds(to)
+ return lower, upper
+}
+
+func ResolveSessionEnd(s *models.Session, activity SessionActivity, now time.Time) {
+ s.HasRecording = activity.LastActivity != nil
+ if activity.LastActivity == nil {
+ return
+ }
+
+ last := *activity.LastActivity
+ if last.Before(s.StartedAt) {
+ last = s.StartedAt
+ }
+
+ end := last
+ if s.EndedAt != nil && s.EndedAt.After(last) && s.EndedAt.Before(last.Add(SessionIdleTimeout)) {
+ end = *s.EndedAt
+ }
+ endedByRecord := s.EndedAt != nil && !s.EndedAt.Before(last.Add(-sessionEndTolerance))
+
+ lastReceived := last
+ if activity.LastReceived != nil {
+ lastReceived = *activity.LastReceived
+ }
+ idle := now.Sub(lastReceived) >= SessionIdleTimeout
+
+ s.Duration = end.Sub(s.StartedAt).Nanoseconds()
+ if endedByRecord || idle {
+ s.EndedAt = &end
+ } else {
+ s.EndedAt = nil
+ }
+}
diff --git a/backend/app/repositories/telemetry/shared/session_activity_test.go b/backend/app/repositories/telemetry/shared/session_activity_test.go
new file mode 100644
index 00000000..3ad98e8d
--- /dev/null
+++ b/backend/app/repositories/telemetry/shared/session_activity_test.go
@@ -0,0 +1,53 @@
+package shared
+
+import (
+ "testing"
+ "time"
+
+ "github.com/tracewayapp/traceway/backend/app/models"
+)
+
+func TestResolveSessionEnd(t *testing.T) {
+ start := time.Date(2026, 9, 26, 12, 0, 0, 0, time.UTC)
+ at := func(minutes float64) *time.Time {
+ v := start.Add(time.Duration(minutes * float64(time.Minute)))
+ return &v
+ }
+ cases := []struct {
+ name string
+ endedAt *time.Time
+ lastActivity *time.Time
+ lastReceived *time.Time
+ now time.Time
+ wantEnd *time.Time
+ wantDuration time.Duration
+ wantRecorded bool
+ }{
+ {name: "no segments keeps the stored end", endedAt: at(3), now: *at(60), wantEnd: at(3), wantDuration: 0},
+ {name: "no segments and no end stays open", now: *at(60)},
+ {name: "end record lost, idle session ends at last activity", lastActivity: at(4), now: *at(30), wantEnd: at(4), wantDuration: 4 * time.Minute, wantRecorded: true},
+ {name: "recent activity is in progress", lastActivity: at(20), now: *at(25), wantDuration: 20 * time.Minute, wantRecorded: true},
+ {name: "idle is measured on server receive time", lastActivity: at(4), lastReceived: at(20), now: *at(30), wantDuration: 4 * time.Minute, wantRecorded: true},
+ {name: "pagehide end shortly after activity is kept", endedAt: at(5), lastActivity: at(4), now: *at(6), wantEnd: at(5), wantDuration: 5 * time.Minute, wantRecorded: true},
+ {name: "late inactivity-timer end falls back to activity", endedAt: at(125), lastActivity: at(10), now: *at(130), wantEnd: at(10), wantDuration: 10 * time.Minute, wantRecorded: true},
+ {name: "activity after the end record reopens the session", endedAt: at(20), lastActivity: at(50), now: *at(55), wantDuration: 50 * time.Minute, wantRecorded: true},
+ {name: "activity before start on a skewed clock clamps to zero", lastActivity: at(-2), now: *at(30), wantEnd: at(0), wantDuration: 0, wantRecorded: true},
+ {name: "second-precision end just before the last segment still closes", endedAt: at(5), lastActivity: at(5.01), now: *at(6), wantEnd: at(5.01), wantDuration: time.Duration(5.01 * float64(time.Minute)), wantRecorded: true},
+ {name: "explicit end is measured from the clamped activity", endedAt: at(14), lastActivity: at(-2), now: *at(30), wantEnd: at(14), wantDuration: 14 * time.Minute, wantRecorded: true},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ s := models.Session{StartedAt: start, EndedAt: tc.endedAt}
+ ResolveSessionEnd(&s, SessionActivity{LastActivity: tc.lastActivity, LastReceived: tc.lastReceived}, tc.now)
+ if s.HasRecording != tc.wantRecorded {
+ t.Fatalf("HasRecording = %v, want %v", s.HasRecording, tc.wantRecorded)
+ }
+ if (s.EndedAt == nil) != (tc.wantEnd == nil) || (s.EndedAt != nil && !s.EndedAt.Equal(*tc.wantEnd)) {
+ t.Fatalf("EndedAt = %v, want %v", s.EndedAt, tc.wantEnd)
+ }
+ if time.Duration(s.Duration) != tc.wantDuration {
+ t.Fatalf("Duration = %v, want %v", time.Duration(s.Duration), tc.wantDuration)
+ }
+ })
+ }
+}
diff --git a/backend/app/repositories/telemetry/sqlite/exception_stack_trace.repository.go b/backend/app/repositories/telemetry/sqlite/exception_stack_trace.repository.go
index d495e165..d02dab50 100644
--- a/backend/app/repositories/telemetry/sqlite/exception_stack_trace.repository.go
+++ b/backend/app/repositories/telemetry/sqlite/exception_stack_trace.repository.go
@@ -476,13 +476,14 @@ func (e *exceptionStackTraceRepository) FindByTraceIds(ctx context.Context, trac
// FindAllBySessionId returns all exceptions/messages stamped with the given session_id, ordered by time.
// The plain `session_id = ?` is what lets SQLite use the partial idx_exceptions_session_recorded
// (WHERE session_id IS NOT NULL); an IS, IN or COALESCE rewrite falls back to a whole-project scan.
-func (e *exceptionStackTraceRepository) FindAllBySessionId(ctx context.Context, projectId, sessionId uuid.UUID) ([]models.ExceptionStackTrace, error) {
+func (e *exceptionStackTraceRepository) FindAllBySessionId(ctx context.Context, projectId, sessionId uuid.UUID, startedAt time.Time) ([]models.ExceptionStackTrace, error) {
+ from, to := shared.TraceWindowBounds(startedAt)
rows, err := lit.SelectNamed[exceptionRow](db.TelemetryDB,
`SELECT id, project_id, trace_id, span_id, trace_type, exception_hash, stack_trace, recorded_at, attributes, app_version, server_name, is_message, session_id
FROM exceptions_v2
- WHERE project_id = :project_id AND session_id = :session_id
+ WHERE project_id = :project_id AND session_id = :session_id AND recorded_at >= :from AND recorded_at <= :to
ORDER BY recorded_at ASC`,
- lit.P{"project_id": projectId, "session_id": sessionId})
+ lit.P{"project_id": projectId, "session_id": sessionId, "from": sqlitetypes.NewSQLiteTime(from), "to": sqlitetypes.NewSQLiteTime(to)})
if err != nil {
return nil, err
}
diff --git a/backend/app/repositories/telemetry/sqlite/session.repository.go b/backend/app/repositories/telemetry/sqlite/session.repository.go
index cb7b5c4e..96b12c03 100644
--- a/backend/app/repositories/telemetry/sqlite/session.repository.go
+++ b/backend/app/repositories/telemetry/sqlite/session.repository.go
@@ -27,6 +27,9 @@ type sessionRow struct {
AppVersion string `lit:"app_version"`
ServerName string `lit:"server_name"`
TraceId string `lit:"trace_id"`
+
+ LastActivity *sqlitetypes.SQLiteTime `lit:"last_activity"`
+ LastReceived *sqlitetypes.SQLiteTime `lit:"last_received"`
}
type sessionRowNaming struct{ lit.DefaultDbNamingStrategy }
@@ -78,9 +81,27 @@ func (row *sessionRow) toModel() models.Session {
if row.Attributes != nil {
s.Attributes = map[string]string(row.Attributes)
}
+ activity := sqlitetypes.SessionActivityResult{LastActivity: row.LastActivity, LastReceived: row.LastReceived}
+ shared.ResolveSessionEnd(&s, activity.Activity(), time.Now())
return s
}
+const sessionActivityJoin = ` LEFT JOIN (
+ SELECT session_id,
+ MAX(COALESCE(ended_at, recorded_at)) AS last_activity,
+ MAX(recorded_at) AS last_received
+ FROM session_recordings
+ WHERE project_id = :project_id AND session_id IS NOT NULL AND recorded_at >= :rec_from AND recorded_at <= :rec_to
+ GROUP BY session_id
+) AS activity ON activity.session_id = sessions.id`
+
+var sessionDurationSortKey = fmt.Sprintf(`CASE WHEN activity.last_activity IS NULL THEN sessions.duration / 1000000
+ ELSE CAST(ROUND((julianday(CASE
+ WHEN sessions.ended_at > MAX(activity.last_activity, sessions.started_at)
+ AND julianday(sessions.ended_at) < julianday(MAX(activity.last_activity, sessions.started_at)) + %d / 1440.0
+ THEN sessions.ended_at ELSE MAX(activity.last_activity, sessions.started_at) END) - julianday(sessions.started_at)) * 86400000) AS INTEGER) END`,
+ int(shared.SessionIdleTimeout/time.Minute))
+
type sessionRepository struct{}
func (r *sessionRepository) Upsert(ctx context.Context, sessions []models.Session) error {
@@ -166,12 +187,9 @@ func (r *sessionRepository) FindAll(ctx context.Context, projectId uuid.UUID, fr
count = int64(countResult.Count)
}
- allowedOrderBy := map[string]bool{
- "started_at": true,
- "duration": true,
- }
- if !allowedOrderBy[orderBy] {
- orderBy = "started_at"
+ orderExpr := "sessions.started_at"
+ if orderBy == "duration" {
+ orderExpr = sessionDurationSortKey
}
sortDir := "DESC"
if sortDirection == "asc" {
@@ -179,12 +197,16 @@ func (r *sessionRepository) FindAll(ctx context.Context, projectId uuid.UUID, fr
}
offset := (page - 1) * pageSize
- query := "SELECT id, project_id, started_at, ended_at, duration, client_ip, attributes, app_version, server_name, trace_id FROM sessions WHERE project_id = :project_id AND started_at >= :start AND started_at <= :end" + whereExtra + " ORDER BY " + orderBy + " " + sortDir + " LIMIT :limit OFFSET :offset"
+ query := "SELECT sessions.id, sessions.project_id, sessions.started_at, sessions.ended_at, sessions.duration, sessions.client_ip, sessions.attributes, sessions.app_version, sessions.server_name, sessions.trace_id, activity.last_activity, activity.last_received FROM sessions" + sessionActivityJoin +
+ " WHERE sessions.project_id = :project_id AND sessions.started_at >= :start AND sessions.started_at <= :end" + whereExtra + " ORDER BY " + orderExpr + " " + sortDir + " LIMIT :limit OFFSET :offset"
+ recFrom, recTo := shared.SessionRecordingWindow(fromDate, toDate)
queryParams := lit.P{
"project_id": projectId,
"start": sqlitetypes.NewSQLiteTime(fromDate),
"end": sqlitetypes.NewSQLiteTime(toDate),
+ "rec_from": sqlitetypes.NewSQLiteTime(recFrom),
+ "rec_to": sqlitetypes.NewSQLiteTime(recTo),
"limit": pageSize,
"offset": offset,
}
@@ -250,6 +272,17 @@ func (r *sessionRepository) FindById(ctx context.Context, projectId, sessionId u
if row == nil {
return nil, nil
}
+
+ from, to := shared.TraceWindowBounds(row.StartedAt.Time)
+ activity, err := lit.SelectSingleNamed[sqlitetypes.SessionActivityResult](db.TelemetryDB,
+ "SELECT MAX(COALESCE(ended_at, recorded_at)) AS last_activity, MAX(recorded_at) AS last_received FROM session_recordings WHERE project_id = :project_id AND session_id = :session_id AND recorded_at >= :from AND recorded_at <= :to",
+ lit.P{"project_id": projectId, "session_id": sessionId, "from": sqlitetypes.NewSQLiteTime(from), "to": sqlitetypes.NewSQLiteTime(to)})
+ if err != nil {
+ return nil, err
+ }
+ if activity != nil {
+ row.LastActivity, row.LastReceived = activity.LastActivity, activity.LastReceived
+ }
s := row.toModel()
return &s, nil
}
diff --git a/backend/app/repositories/telemetry/sqlite/session_recording.repository.go b/backend/app/repositories/telemetry/sqlite/session_recording.repository.go
index 0e412ac4..950dd94c 100644
--- a/backend/app/repositories/telemetry/sqlite/session_recording.repository.go
+++ b/backend/app/repositories/telemetry/sqlite/session_recording.repository.go
@@ -6,7 +6,9 @@ import (
"context"
"database/sql"
"errors"
+ "github.com/tracewayapp/traceway/backend/app/repositories/telemetry/shared"
"github.com/tracewayapp/traceway/backend/app/repositories/telemetry/sqlitetypes"
+ "time"
"github.com/google/uuid"
"github.com/tracewayapp/lit/v2"
@@ -15,13 +17,14 @@ import (
)
type sessionRecording struct {
- Id uuid.UUID `lit:"id"`
- ProjectId uuid.UUID `lit:"project_id"`
- ExceptionId uuid.UUID `lit:"exception_id"`
- SessionId *uuid.UUID `lit:"session_id"`
- SegmentIndex int32 `lit:"segment_index"`
- FilePath string `lit:"file_path"`
- RecordedAt sqlitetypes.SQLiteTime `lit:"recorded_at"`
+ Id uuid.UUID `lit:"id"`
+ ProjectId uuid.UUID `lit:"project_id"`
+ ExceptionId uuid.UUID `lit:"exception_id"`
+ SessionId *uuid.UUID `lit:"session_id"`
+ SegmentIndex int32 `lit:"segment_index"`
+ FilePath string `lit:"file_path"`
+ RecordedAt sqlitetypes.SQLiteTime `lit:"recorded_at"`
+ EndedAt *sqlitetypes.SQLiteTime `lit:"ended_at"`
}
func init() {
@@ -53,6 +56,10 @@ func (r *sessionRecordingRepository) InsertAsync(ctx context.Context, recordings
FilePath: rec.FilePath,
RecordedAt: sqlitetypes.NewSQLiteTime(rec.RecordedAt),
}
+ if rec.EndedAt != nil {
+ t := sqlitetypes.NewSQLiteTime(*rec.EndedAt)
+ row.EndedAt = &t
+ }
if err := lit.InsertExistingUuid(tx, &row); err != nil {
return err
}
@@ -75,17 +82,25 @@ func (r *sessionRecordingRepository) FindByExceptionId(ctx context.Context, proj
}
// FindBySessionId returns all recording segments for a session ordered by segment_index.
-func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projectId, sessionId uuid.UUID) ([]models.SessionRecording, error) {
- rows, err := lit.SelectNamed[sessionRecording](db.TelemetryDB,
- "SELECT id, project_id, exception_id, session_id, segment_index, file_path, recorded_at FROM session_recordings WHERE project_id = :project_id AND session_id = :session_id ORDER BY segment_index ASC, recorded_at ASC",
- lit.P{"project_id": projectId, "session_id": sessionId})
+func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projectId, sessionId uuid.UUID, startedAt *time.Time) ([]models.SessionRecording, error) {
+ query := "SELECT id, project_id, exception_id, session_id, segment_index, file_path, recorded_at, ended_at FROM session_recordings WHERE project_id = :project_id AND session_id = :session_id"
+ params := lit.P{"project_id": projectId, "session_id": sessionId}
+ if startedAt != nil {
+ from, to := shared.TraceWindowBounds(*startedAt)
+ query += " AND recorded_at >= :from AND recorded_at <= :to"
+ params["from"] = sqlitetypes.NewSQLiteTime(from)
+ params["to"] = sqlitetypes.NewSQLiteTime(to)
+ }
+ query += " ORDER BY segment_index ASC, recorded_at ASC"
+
+ rows, err := lit.SelectNamed[sessionRecording](db.TelemetryDB, query, params)
if err != nil {
return nil, err
}
out := make([]models.SessionRecording, 0, len(rows))
for _, row := range rows {
- out = append(out, models.SessionRecording{
+ rec := models.SessionRecording{
Id: row.Id,
ProjectId: row.ProjectId,
ExceptionId: row.ExceptionId,
@@ -93,7 +108,12 @@ func (r *sessionRecordingRepository) FindBySessionId(ctx context.Context, projec
SegmentIndex: row.SegmentIndex,
FilePath: row.FilePath,
RecordedAt: row.RecordedAt.Time,
- })
+ }
+ if row.EndedAt != nil {
+ t := row.EndedAt.Time
+ rec.EndedAt = &t
+ }
+ out = append(out, rec)
}
return out, nil
}
diff --git a/backend/app/repositories/telemetry/sqlitetypes/sqlitetypes.go b/backend/app/repositories/telemetry/sqlitetypes/sqlitetypes.go
index cd4392f3..e77c2104 100644
--- a/backend/app/repositories/telemetry/sqlitetypes/sqlitetypes.go
+++ b/backend/app/repositories/telemetry/sqlitetypes/sqlitetypes.go
@@ -8,6 +8,7 @@ import (
"github.com/tracewayapp/lit/v2"
"github.com/tracewayapp/traceway/backend/app/models"
+ "github.com/tracewayapp/traceway/backend/app/repositories/telemetry/shared"
)
// SQLiteTime handles scanning time.Time from SQLite DATETIME text columns
@@ -125,10 +126,29 @@ type FilePathResult struct {
FilePath string `lit:"file_path"`
}
+type SessionActivityResult struct {
+ LastActivity *SQLiteTime `lit:"last_activity"`
+ LastReceived *SQLiteTime `lit:"last_received"`
+}
+
+func (r SessionActivityResult) Activity() shared.SessionActivity {
+ var activity shared.SessionActivity
+ if r.LastActivity != nil {
+ t := r.LastActivity.Time
+ activity.LastActivity = &t
+ }
+ if r.LastReceived != nil {
+ t := r.LastReceived.Time
+ activity.LastReceived = &t
+ }
+ return activity
+}
+
func init() {
models.ExtensionModelRegistrations = append(models.ExtensionModelRegistrations, func(driver lit.Driver) {
lit.RegisterModel[TimeSeriesResult](driver)
lit.RegisterModel[GroupedTimeSeriesResult](driver)
lit.RegisterModel[FilePathResult](driver)
+ lit.RegisterModel[SessionActivityResult](driver)
})
}
diff --git a/backend/go.mod b/backend/go.mod
index 4a344ab6..a9ec978f 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -196,7 +196,7 @@ require (
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
golang.org/x/crypto v0.53.0
- golang.org/x/sync v0.21.0 // indirect
+ golang.org/x/sync v0.21.0
golang.org/x/text v0.39.0 // indirect
)
diff --git a/docs/pages/learn/sessions.mdx b/docs/pages/learn/sessions.mdx
index 8b37a9fb..cf5005f9 100644
--- a/docs/pages/learn/sessions.mdx
+++ b/docs/pages/learn/sessions.mdx
@@ -43,7 +43,7 @@ It ends on the first of:
| **Maximum duration** | 60 minutes from session start | Hard cap regardless of activity |
| **Page unload** | `pagehide` fires (close tab, hard refresh, navigate away) | Final segment is drained, closing payload sent via `fetch keepalive` |
-If a session ends without an explicit close (e.g. the browser is force-quit before `pagehide` runs, or the closing flush is dropped), its `ended_at` stays NULL on the backend and the dashboard renders the duration as **Abandoned** once the row has been quiet for ≥ 15 min.
+The closing payload is not guaranteed to arrive: the browser can be force-quit before `pagehide` runs, mobile browsers freeze background tabs without firing it, and the flush itself can be dropped. The backend therefore does not depend on it. Once no replay segment has arrived for 15 minutes, the session ends at the time of its last recorded segment. A close reported more than 15 minutes after the last segment, which is what the inactivity timer sends from a tab that sat frozen in the background, is replaced the same way, so the duration reflects activity rather than time the tab spent unattended. A session with no recorded segments and no close is shown as **No recording**.
## Attributes
diff --git a/frontend/src/lib/utils/session-duration.test.ts b/frontend/src/lib/utils/session-duration.test.ts
new file mode 100644
index 00000000..39b3cf21
--- /dev/null
+++ b/frontend/src/lib/utils/session-duration.test.ts
@@ -0,0 +1,51 @@
+import { describe, expect, it } from 'vitest';
+import { formatSessionDuration, sessionDurationLabel } from './session-duration';
+
+const now = Date.parse('2026-09-26T17:00:00Z');
+const minutesAgo = (m: number) => new Date(now - m * 60_000).toISOString();
+
+describe('sessionDurationLabel', () => {
+ it('shows the duration of an ended session', () => {
+ expect(
+ sessionDurationLabel(
+ { startedAt: minutesAgo(40), endedAt: minutesAgo(38), duration: 120e9 },
+ now
+ )
+ ).toBe('2m 0s');
+ });
+
+ it('keeps a long session with recent activity in progress', () => {
+ expect(
+ sessionDurationLabel(
+ { startedAt: minutesAgo(40), duration: 35 * 60e9, hasRecording: true },
+ now
+ )
+ ).toBe('in progress');
+ });
+
+ it('treats a fresh session without segments as in progress', () => {
+ expect(sessionDurationLabel({ startedAt: minutesAgo(5), duration: 0 }, now)).toBe(
+ 'in progress'
+ );
+ });
+
+ it('labels an old session without segments or end as unrecorded', () => {
+ expect(sessionDurationLabel({ startedAt: minutesAgo(40), duration: 0 }, now)).toBe(
+ 'No recording'
+ );
+ });
+});
+
+describe('formatSessionDuration', () => {
+ it.each([
+ [11.9e9, '11.9s'],
+ [59.94e9, '59.9s'],
+ [60e9, '1m 0s'],
+ [1267e9, '21m 7s'],
+ [3600e9, '1h 0m'],
+ [7514e9, '2h 5m'],
+ [-5e9, '0.0s']
+ ])('formats %d ns as %s', (ns, label) => {
+ expect(formatSessionDuration(ns)).toBe(label);
+ });
+});
diff --git a/frontend/src/lib/utils/session-duration.ts b/frontend/src/lib/utils/session-duration.ts
new file mode 100644
index 00000000..95a371ce
--- /dev/null
+++ b/frontend/src/lib/utils/session-duration.ts
@@ -0,0 +1,28 @@
+export const SESSION_IDLE_TIMEOUT_MS = 15 * 60_000;
+
+export type SessionTiming = {
+ startedAt: string;
+ endedAt?: string | null;
+ duration: number;
+ hasRecording?: boolean;
+};
+
+export function formatSessionDuration(nanoseconds: number): string {
+ const totalSeconds = Math.max(0, nanoseconds) / 1e9;
+ if (totalSeconds < 60) return `${totalSeconds.toFixed(1)}s`;
+ const seconds = Math.floor(totalSeconds);
+ const hours = Math.floor(seconds / 3600);
+ const minutes = Math.floor((seconds % 3600) / 60);
+ if (hours > 0) return `${hours}h ${minutes}m`;
+ return `${minutes}m ${seconds % 60}s`;
+}
+
+export function sessionDurationLabel(session: SessionTiming, now = Date.now()): string {
+ if (session.endedAt) return formatSessionDuration(session.duration);
+ if (session.hasRecording) return 'in progress';
+ const startedMs = Date.parse(session.startedAt);
+ if (Number.isFinite(startedMs) && now - startedMs < SESSION_IDLE_TIMEOUT_MS) {
+ return 'in progress';
+ }
+ return 'No recording';
+}
diff --git a/frontend/src/routes/sessions/+page.svelte b/frontend/src/routes/sessions/+page.svelte
index 270dc6d4..6a14fe0c 100644
--- a/frontend/src/routes/sessions/+page.svelte
+++ b/frontend/src/routes/sessions/+page.svelte
@@ -2,12 +2,8 @@
import { getErrorMessage } from '$lib/utils/errors';
import { onMount, onDestroy } from 'svelte';
import { api } from '$lib/api';
- import {
- formatDuration,
- formatRelativeTimeAgo,
- toUTCISO,
- calendarDateTimeToLuxon
- } from '$lib/utils/formatters';
+ import { formatRelativeTimeAgo, toUTCISO, calendarDateTimeToLuxon } from '$lib/utils/formatters';
+ import { sessionDurationLabel } from '$lib/utils/session-duration';
import { getTimezone } from '$lib/state/timezone.svelte';
import * as Table from '$lib/components/ui/table';
import { LoadingCircle } from '$lib/components/ui/loading-circle';
@@ -63,6 +59,7 @@
startedAt: string;
endedAt?: string | null;
duration: number;
+ hasRecording?: boolean;
clientIP: string;
attributes?: Record<string, string>;
appVersion: string;
@@ -264,17 +261,6 @@
return id.split('-')[0] ?? id.slice(0, 8);
}
- const ABANDONED_AFTER_MS = 15 * 60_000;
-
- function durationLabel(s: Session): string {
- if (s.endedAt) return formatDuration(s.duration);
- const startedMs = Date.parse(s.startedAt);
- if (Number.isFinite(startedMs) && Date.now() - startedMs >= ABANDONED_AFTER_MS) {
- return 'Abandoned';
- }
- return 'in progress';
- }
-
async function loadData(pushToHistory = true) {
loading = true;
error = '';
@@ -591,7 +577,7 @@
>{formatRelativeTimeAgo(session.startedAt, timezone)}</Table.Cell
>
<Table.Cell class="font-mono text-sm tabular-nums">
- {durationLabel(session)}
+ {sessionDurationLabel(session)}
</Table.Cell>
<Table.Cell class="font-mono text-sm">{session.appVersion || '—'}</Table.Cell>
</Table.Row>
diff --git a/frontend/src/routes/sessions/[sessionId]/+page.svelte b/frontend/src/routes/sessions/[sessionId]/+page.svelte
index d15273c4..46df3af8 100644
--- a/frontend/src/routes/sessions/[sessionId]/+page.svelte
+++ b/frontend/src/routes/sessions/[sessionId]/+page.svelte
@@ -6,7 +6,8 @@
import { api } from '$lib/api';
import { projectsState } from '$lib/state/projects.svelte';
import { getTimezone } from '$lib/state/timezone.svelte';
- import { formatDuration, formatDateTime } from '$lib/utils/formatters';
+ import { formatDateTime } from '$lib/utils/formatters';
+ import { sessionDurationLabel } from '$lib/utils/session-duration';
import { createSmartBackHandler } from '$lib/utils/back-navigation';
import { createRowClickHandler } from '$lib/utils/navigation';
import * as Card from '$lib/components/ui/card';
@@ -35,6 +36,7 @@
startedAt: string;
endedAt?: string | null;
duration: number;
+ hasRecording?: boolean;
clientIP: string;
appVersion: string;
serverName: string;
@@ -72,34 +74,34 @@
const hasActions = $derived(recordingActions.length > 0);
const defaultTab = $derived(hasLogs ? 'logs' : 'actions');
- const ABANDONED_AFTER_MS = 15 * 60_000;
-
- function durationLabel(s: Session | null): string {
- if (!s) return '—';
- if (s.endedAt) return formatDuration(s.duration);
- const startedMs = Date.parse(s.startedAt);
- if (Number.isFinite(startedMs) && Date.now() - startedMs >= ABANDONED_AFTER_MS) {
- return 'Abandoned';
- }
- return 'in progress';
- }
-
async function loadAll() {
loading = true;
error = '';
notFound = false;
try {
+ const projectId = projectsState.currentProjectId ?? undefined;
+ const loadRecording = (startedAt: string) =>
+ api.get(`/sessions/${sessionId}/recording?startedAt=${encodeURIComponent(startedAt)}`, {
+ projectId
+ });
+
const startedAt = pageState.url.searchParams.get('t');
- const detail = await api.post(`/sessions/${sessionId}`, startedAt ? { startedAt } : {}, {
- projectId: projectsState.currentProjectId ?? undefined
+ const detailRequest = api.post(`/sessions/${sessionId}`, startedAt ? { startedAt } : {}, {
+ projectId
});
+ const recordingRequest = startedAt ? loadRecording(startedAt) : null;
+ recordingRequest?.catch(() => {});
+
+ const detail = await detailRequest;
session = detail.session;
exceptions = detail.exceptions || [];
- const recording = await api.get(`/sessions/${sessionId}/recording`, {
- projectId: projectsState.currentProjectId ?? undefined
- });
+ const sessionStartedAt: string = detail.session.startedAt;
+ const recording =
+ startedAt && recordingRequest && Date.parse(startedAt) === Date.parse(sessionStartedAt)
+ ? await recordingRequest
+ : await loadRecording(sessionStartedAt);
recordingEvents = recording?.events ?? [];
recordingLogs = recording?.logs ?? [];
recordingActions = recording?.actions ?? [];
@@ -145,7 +147,7 @@
</Card.Header>
<Card.Content class="grid grid-cols-1 gap-4 sm:grid-cols-3">
<LabelValue label="Started" value={formatDateTime(session.startedAt, { timezone })} />
- <LabelValue label="Duration" value={durationLabel(session)} />
+ <LabelValue label="Duration" value={sessionDurationLabel(session)} />
<LabelValue label="App version" value={session.appVersion || '—'} />
</Card.Content>
</Card.Root>