Skip to content

Conversation

@oscartbeaumont
Copy link
Contributor

@oscartbeaumont oscartbeaumont commented Oct 23, 2025

This will help us tell if all the uploader bugs are fixed because we think most of the current errors are people hosting old versions of Cap web.

Summary by CodeRabbit

  • Bug Fixes

    • Consistent server URL resolution at startup: debug env → stored preference → fallback env, and stored preference updated when needed.
    • Analytics now detect and label CAP Cloud vs self-hosted environments for more accurate tracking.
  • Chores

    • Streamlined startup and settings initialization for more reliable and consistent behavior.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 23, 2025

Warning

Rate limit exceeded

@oscartbeaumont has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 15 minutes and 32 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 9a5d32e and bcf8ab6.

📒 Files selected for processing (1)
  • apps/desktop/src-tauri/src/lib.rs (3 hunks)

Walkthrough

Resolves and seeds a single server_url at startup (env → store → compile-time/default), persists it when sourced from env, and calls posthog::set_server_url. PostHog gains a thread-safe flag set by that URL and now enriches events (cap_version, cap_backend, os, arch) in the async capture path before sending.

Changes

Cohort / File(s) Summary
App init & server URL resolution
apps/desktop/src-tauri/src/lib.rs
Startup now resolves server_url with precedence: debug-runtime env VITE_SERVER_URLGeneralSettingsStore stored value → compile-time VITE_SERVER_URL/default. If resolved from env, persists to GeneralSettingsStore. Calls posthog::set_server_url and constructs App with the resolved value.
PostHog enrichment & server-url flag
apps/desktop/src-tauri/src/posthog.rs
Add pub fn set_server_url(url: &str) and a thread-safe static (OnceLock + RwLock<Option>) to mark CAP Cloud vs self-hosted. Move injection of cap_version, cap_backend, os, and arch into async_capture_event and send via posthog_rs::capture(enriched_event). Remove those inserts from the From<PostHogEvent> conversion.

Sequence Diagram(s)

sequenceDiagram participant Init as App Init participant Store as GeneralSettingsStore participant PH as PostHog Module participant Event as Event Capture Init->>Init: Resolve server_url (debug env → stored → compile-time/default) alt resolved from env Init->>Store: Persist server_url end Init->>PH: set_server_url(resolved_url) PH->>PH: store cloud/self-hosted flag (OnceLock + RwLock) Init->>Init: Construct App with resolved server_url Note over Event,PH: Later — capturing an event Event->>PH: async_capture_event(raw_event) PH->>PH: read backend flag PH->>PH: enrich event (cap_version, cap_backend, os, arch) PH->>Event: posthog_rs::capture(enriched_event) 
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • Brendonovich

Poem

🐰 I hopped through env and setting bright,

I sniffed the URL by day and night;
Events now wear a tiny crown,
Saying cloud or self-hosted down;
A joyful hop for telemetry light. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "PostHog track API backend" refers to a real aspect of the changeset, where the PR adds functionality to track and distinguish whether the API server is CAP Cloud or self-hosted, enriching PostHog events with this backend information. The title accurately identifies the core components involved (PostHog and API backend tracking), though it could be more specific about what exactly is being added or changed. While the phrasing is somewhat concise and could potentially be clearer, it conveys meaningful information about the changeset rather than being purely generic or vague like "misc updates."

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@oscartbeaumont oscartbeaumont marked this pull request as ready for review October 23, 2025 11:45
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d81b2ae and 79d33da.

📒 Files selected for processing (2)
  • apps/desktop/src-tauri/src/lib.rs (3 hunks)
  • apps/desktop/src-tauri/src/posthog.rs (4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

**/*.rs: Format Rust code using rustfmt and ensure all Rust code passes workspace-level clippy lints.
Rust modules should be named with snake_case, and crate directories should be in kebab-case.

Files:

  • apps/desktop/src-tauri/src/lib.rs
  • apps/desktop/src-tauri/src/posthog.rs
🧬 Code graph analysis (2)
apps/desktop/src-tauri/src/lib.rs (2)
apps/desktop/src-tauri/src/posthog.rs (1)
  • set_server_url (70-75)
apps/desktop/src-tauri/src/general_settings.rs (2)
  • update (222-231)
  • get (208-219)
apps/desktop/src-tauri/src/posthog.rs (2)
apps/desktop/src-tauri/src/lib.rs (3)
  • std (1518-1536)
  • std (1561-1583)
  • set_server_url (1882-1888)
apps/desktop/src-tauri/src/upload.rs (1)
  • spawn (335-373)
🔇 Additional comments (4)
apps/desktop/src-tauri/src/lib.rs (1)

1883-1886: LGTM! Proper state synchronization.

The function correctly updates both the PostHog backend state and the in-memory app.server_url, ensuring consistency across the application.

apps/desktop/src-tauri/src/posthog.rs (3)

1-4: LGTM! Appropriate imports for thread-safe state management.

The added synchronization primitives (OnceLock, RwLock, PoisonError) are well-suited for managing the per-thread CAP Cloud flag.


27-55: LGTM! Clean separation of concerns.

The refactoring to remove global properties from the From implementation and inject them in async_capture_event centralizes property management and keeps the conversion focused on event-specific data.


82-108: LGTM! Property injection correctly implemented.

The function properly enriches events with global properties before capture. The handling of API_SERVER_IS_CAP_CLOUD being None (returning "unknown") is appropriate for events captured before set_server_url is called.

Note: This ties into the initialization order issue flagged in lib.rs - ensure set_server_url is called before init to avoid events being tagged with backend="unknown".

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 79d33da and b6da141.

📒 Files selected for processing (1)
  • apps/desktop/src-tauri/src/posthog.rs (4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

**/*.rs: Format Rust code using rustfmt and ensure all Rust code passes workspace-level clippy lints.
Rust modules should be named with snake_case, and crate directories should be in kebab-case.

Files:

  • apps/desktop/src-tauri/src/posthog.rs
🧬 Code graph analysis (1)
apps/desktop/src-tauri/src/posthog.rs (2)
apps/desktop/src-tauri/src/lib.rs (3)
  • std (1518-1536)
  • std (1561-1583)
  • set_server_url (1882-1888)
apps/desktop/src-tauri/src/upload.rs (1)
  • spawn (335-373)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build Desktop (aarch64-apple-darwin, macos-latest)
  • GitHub Check: Build Desktop (x86_64-pc-windows-msvc, windows-latest)
  • GitHub Check: Analyze (rust)
🔇 Additional comments (4)
apps/desktop/src-tauri/src/posthog.rs (4)

1-4: LGTM!

The imports are appropriate for the thread-safe state management pattern used in this module.


25-57: LGTM!

The event conversion is clean and uses appropriate error handling for analytics properties. The error-logging pattern (.map_err().ok()) ensures failures don't impact the main operation flow.


77-77: LGTM!

The use of OnceLock<RwLock<Option<bool>>> is the appropriate pattern for thread-safe, lazily-initialized global state. The Option<bool> allows representing three states (cloud/self-hosted/unknown), which is correctly utilized in async_capture_event.


79-114: LGTM!

The event enrichment and capture logic is well-structured:

  • Non-blocking async task spawn prevents impacting the main thread
  • Property enrichment happens after event conversion, maintaining separation of concerns
  • Thread-safe read from API_SERVER_IS_CAP_CLOUD with proper poison error handling
  • The three-state cap_backend mapping (cloud/self_hosted/unknown) provides useful telemetry
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
apps/desktop/src-tauri/src/lib.rs (1)

2229-2253: Initialize PostHog after resolving server_url to avoid “unknown” cap_backend on early events.

posthog::init() is called at Line 1944, before set_server_url runs here; events emitted in that window won’t be enriched correctly.

Move init to run after set_server_url (or call set_server_url earlier). Minimal change:

@@ - posthog::init(); + // defer until server_url is resolved

Then initialize here, right after set_server_url:

 posthog::set_server_url(&server_url); + posthog::init();

Also, tiny nit: the closure param in GeneralSettingsStore::update doesn’t need mut. Optional cleanup:

- GeneralSettingsStore::update(&app, |mut s| { + GeneralSettingsStore::update(&app, |s| { s.server_url = server_url.clone(); })
  • Should release builds also honor a runtime VITE_SERVER_URL (std::env::var) or is compile‑time + store the intended contract? If runtime override is desired in release, mirror the debug path without cfg!(debug_assertions).
🧹 Nitpick comments (1)
apps/desktop/src-tauri/src/lib.rs (1)

1883-1886: Persist setting to GeneralSettingsStore to survive app restarts.

Right now this only updates in‑memory state and PostHog flag. If the UI doesn’t also save to settings, the change is lost after restart. Persist here to make the command authoritative.

Apply this diff:

 async fn set_server_url(app: MutableState<'_, App>, server_url: String) -> Result<(), ()> { - let mut app = app.write().await; - posthog::set_server_url(&server_url); - app.server_url = server_url; + let mut app = app.write().await; + posthog::set_server_url(&server_url); + app.server_url = server_url.clone(); + // Persist for future launches (non-fatal on error) + GeneralSettingsStore::update(&app.handle, |s| { + s.server_url = server_url; + }) + .map_err(|err| warn!("Error persisting server_url: {err}")) + .ok(); Ok(()) }

Confirm the UI also writes GeneralSettingsStore.server_url. If so, we can keep this as a safety net; otherwise this becomes the primary persistence path.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b6da141 and 9a5d32e.

📒 Files selected for processing (1)
  • apps/desktop/src-tauri/src/lib.rs (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

**/*.rs: Format Rust code using rustfmt and ensure all Rust code passes workspace-level clippy lints.
Rust modules should be named with snake_case, and crate directories should be in kebab-case.

Files:

  • apps/desktop/src-tauri/src/lib.rs
🧬 Code graph analysis (1)
apps/desktop/src-tauri/src/lib.rs (2)
apps/desktop/src-tauri/src/posthog.rs (1)
  • set_server_url (70-75)
apps/desktop/src-tauri/src/general_settings.rs (2)
  • get (208-219)
  • update (222-231)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build Desktop (aarch64-apple-darwin, macos-latest)
  • GitHub Check: Build Desktop (x86_64-pc-windows-msvc, windows-latest)
  • GitHub Check: Analyze (rust)
@Brendonovich Brendonovich merged commit 5e16d50 into main Oct 23, 2025
15 checks passed
@oscartbeaumont oscartbeaumont deleted the posthog-backend-info branch October 23, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants