Skip to content

Session Stitching - #4

Merged
Ret2Hell merged 3 commits into
mainfrom
dev
Mar 29, 2026
Merged

Ret2Hell merged 3 commits into
mainfrom
dev

Conversation

@Ret2Hell

@Ret2Hell Ret2Hell commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Redis-backed session tracking; events now include computed session IDs
    • Improved user-agent parsing for more accurate browser/OS detection
  • Behavior Changes

    • Device type values expanded to include "bot" alongside desktop, mobile, tablet
  • Tests

    • Updated and added tests covering session behavior and user-agent/device classification
  • Documentation

    • Event model docs updated to reflect new device type values

On each new event:
  - If no Redis state exists, create a new session.
  - If state exists and the gap from last_event_ts to this event is 30
minutes or less, reuse the same session.
  - If the gap is greater than 30 minutes, create a new session.
  - The Redis key TTL is also 30 minutes, matching the inactivity
timeout, so inactive sessions naturally expire
@coderabbitai

coderabbitai Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Free

Run ID: 87860059-d969-4206-801f-644d70bb1e3f

📥 Commits

Reviewing files that changed from the base of the PR and between 9376af4 and b6eed62.

📒 Files selected for processing (2)
  • cmd/processor/main.go
  • internal/processors/session_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/processors/session_test.go

📝 Walkthrough

Walkthrough

Integrates Redis-backed session management into the event pipeline and updates main wiring to initialize Redis and pass a SessionManager into Pipeline. Pipeline processing now accepts a context and performs session stitching via SessionManager. User-agent parsing was replaced with an external library; tests and deps updated accordingly.

Changes

Cohort / File(s) Summary
Main wiring & infra helpers
cmd/processor/main.go
Initializes Redis client, creates SessionManager, updates pipeline construction to accept session manager; refactors ClickHouse writer and DLQ producer setup into helpers and adjusts shutdown handling.
Pipeline changes
internal/pipeline/pipeline.go, internal/pipeline/pipeline_test.go
Pipeline now accepts sessionMgr *processors.SessionManager, Process and processEvent propagate context.Context, and session-stitching step added (fills SessionID, handles errors by falling back to "unknown"). Tests updated to pass context and the extra constructor arg.
Session manager implementation & tests
internal/processors/session.go, internal/processors/session_test.go
New processors.SessionManager backed by *redis.Client with GetOrCreateSession(ctx, siteID, visitorID, eventTS) implementing session create/update and 30-minute expiry; tests for nil-manager and key formatting.
User-agent parser refactor & tests
internal/processors/useragent.go, internal/processors/useragent_test.go
Replaces custom UA heuristics with mileusna/useragent parsing, simplifies device/browser/os detection (adds bot device type); test expectations updated accordingly.
Tests updated for pipeline consumer
internal/consumer/consumer_test.go
Pipeline constructor call in tests now includes an extra nil arg for the session manager.
Model comment
internal/models/event.go
Updated ProcessedEvent.DeviceType comment to include bot.
Dependencies
go.mod
Adds github.com/mileusna/useragent v1.3.5 and github.com/redis/go-redis/v9 v9.18.0 (plus indirect deps).

Sequence Diagram(s)

sequenceDiagram
    participant E as Event Source
    participant P as Pipeline
    participant S as SessionManager
    participant R as Redis
    participant CH as ClickHouseWriter

    E->>P: Send raw InternalEvent (with ctx)
    P->>S: GetOrCreateSession(ctx, siteID, visitorID, ts)
    S->>R: GET session:<siteID>:<visitorID>
    alt found & valid
        R-->>S: session JSON
        S->>R: SET updated session JSON (expire 30m)
        S-->>P: sessionID
    else not found / expired / corrupt
        S->>R: SET new session JSON (expire 30m)
        S-->>P: new sessionID
    end
    P->>CH: Batch write ProcessedEvent (with SessionID)
    CH-->>P: ack
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰
Redis nests our hopping tracks,
Sessions stitched from scattered facts.
Bots and browsers now well-known,
Events find the proper home.
Hoppity hooray — code has grown! 🥕


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9376af4df7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/processors/session.go Resolved
Comment thread internal/pipeline/pipeline.go Resolved
@sonarqubecloud

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b6eed62898

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread cmd/processor/main.go Resolved
@Ret2Hell
Ret2Hell merged commit a2091b5 into main Mar 29, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant