-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: switch analytics to posthog #508
Conversation
Warning Rate limit exceeded@morgante has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 27 seconds before requesting another review. How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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. WalkthroughWalkthroughThe changes involve restructuring the analytics functionality within a CLI application. A new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CLI
participant PostHogClient
participant PostHogAPI
User->>CLI: Trigger event
CLI->>PostHogClient: Capture event with AnalyticsPayload
PostHogClient->>PostHogAPI: Send PostHogEvent
PostHogAPI-->>PostHogClient: Response
PostHogClient-->>CLI: Acknowledge event capture
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
Early access features: enabledWe are currently testing the following features in early access:
Note:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (3)
- crates/cli/src/analytics.rs (3 hunks)
- crates/cli/src/lib.rs (1 hunks)
- crates/cli/src/posthog.rs (1 hunks)
Additional comments not posted (2)
crates/cli/src/lib.rs (1)
12-12
: Module inclusion is correctThe addition of the
posthog
module integrates the new analytics functionality properly into the CLI application.crates/cli/src/analytics.rs (1)
206-212
: Ensure compatibility with Rust version when usinglet else
syntaxThe usage of the
let Ok(client) = ... else { ... }
syntax requires Rust 1.65 or newer. It's important to confirm that the project's minimum supported Rust version (MSRV) is set accordingly to avoid build failures for users on older Rust versions.Run the following script to check the project's specified Rust version:
Ensure that the Rust version is set to 1.65 or higher in your project's configuration.
static ref POSTHOG_WRITE_KEY: String = | ||
String::from("phc_ksrztn1ogPbqUSUf1qRjhoC6GMzpmBm7iqSNhVzvor5"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid hard-coding API keys
The PostHog API key is hard-coded into the source code:
static ref POSTHOG_WRITE_KEY: String =
String::from("phc_ksrztn1ogPbqUSUf1qRjhoC6GMzpmBm7iqSNhVzvor5");
This practice poses a security risk, as it can lead to unauthorized access if the code is exposed publicly. It's recommended to retrieve the API key from environment variables or a secure configuration.
Consider modifying the code to read the API key from an environment variable:
-static ref POSTHOG_WRITE_KEY: String =
- String::from("phc_ksrztn1ogPbqUSUf1qRjhoC6GMzpmBm7iqSNhVzvor5");
+static ref POSTHOG_WRITE_KEY: String =
+ env::var("POSTHOG_WRITE_KEY").expect("POSTHOG_WRITE_KEY must be set");
Ensure that the environment variable POSTHOG_WRITE_KEY
is securely provided in the deployment environment.
crates/cli/src/posthog.rs
Outdated
.client | ||
.post(POSTHOG_HOST.to_string()) | ||
.header(CONTENT_TYPE, "application/json") | ||
.body(serde_json::to_string(&event).expect("unwrap here is safe")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle serialization errors without panicking
Using expect("unwrap here is safe")
on serde_json::to_string(&event)
may cause a panic if serialization fails unexpectedly, which could crash the application.
Modify the code to properly handle serialization errors:
.header(CONTENT_TYPE, "application/json")
- .body(serde_json::to_string(&event).expect("unwrap here is safe"))
+ .body(serde_json::to_string(&event)?)
.send()
By using the ?
operator, any serialization errors will be propagated as part of the Result
, allowing for graceful error handling.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
.body(serde_json::to_string(&event).expect("unwrap here is safe")) | |
.body(serde_json::to_string(&event)?) |
let Ok(client) = reqwest::Client::builder() | ||
.timeout(Duration::from_secs(5)) | ||
.send() | ||
.await | ||
{ | ||
Ok(response) => { | ||
if !response.status().is_success() { | ||
log::error!( | ||
"Failed to send event {}: {}", | ||
analytics_event_name, | ||
response.status() | ||
); | ||
} | ||
} | ||
Err(e) => { | ||
log::error!("Failed to send event {}: {:#}", analytics_event_name, e); | ||
} | ||
.build() | ||
else { | ||
log::error!("Failed to create client"); | ||
return; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log error details when client creation fails
Currently, if the reqwest::Client
fails to build, the error details are not logged, making it difficult to diagnose the issue.
Modify the error handling to capture and log the specific error:
let client = match reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build() {
- Ok(client) => client,
- else {
- log::error!("Failed to create client");
+ Ok(client) => client,
+ Err(e) => {
+ log::error!("Failed to create client: {:#}", e);
return;
- }
+ }
};
This will provide more insight into why the client creation failed.
Committable suggestion was skipped due to low confidence.
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
On commit ce93abe, Grit tried to heal these tests:
Tip You can view and edit CI healing settings on the Grit App. |
There was a problem hiding this 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.yaml
Review profile: CHILL
Files selected for processing (1)
- crates/cli/src/posthog.rs (1 hunks)
Additional context used
GitHub Check: clippy
crates/cli/src/posthog.rs
[failure] 39-39: borrow of moved value:
res
error[E0382]: borrow of moved value:res
--> crates/cli/src/posthog.rs:39:82
|
30 | let res = self
| --- move occurs becauseres
has typereqwest::Response
, which does not implement theCopy
trait
...
38 | let body = res.text().await.unwrap_or_else(|_| "Unable to read response body".to_string());
| ------res
moved due to this method call
39 | bail!("Failed to send event {}: {}\nResponse body: {}", event.event, res.status(), body);
| ^^^ value borrowed here after move
|
note:reqwest::Response::text
takes ownership of the receiverself
, which movesres
--> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/reqwest-0.11.24/src/async_impl/response.rs:147:23
|
147 | pub async fn text(self) -> crate::Result {
| ^^^^
Additional comments not posted (2)
crates/cli/src/posthog.rs (2)
15-16
: Avoid hardcoding API keys
34-34
: Handle serialization errors without panicking
crates/cli/src/posthog.rs
Outdated
let body = res.text().await.unwrap_or_else(|_| "Unable to read response body".to_string()); | ||
bail!("Failed to send event {}: {}\nResponse body: {}", event.event, res.status(), body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the use-after-move error
res
is moved when calling res.text().await
, so it cannot be used afterward. This causes a compilation error due to the use of a moved value.
Apply this diff to fix the error by capturing the status before consuming res
:
if !res.status().is_success() {
+ let status = res.status();
let body = res.text().await.unwrap_or_else(|_| "Unable to read response body".to_string());
- bail!("Failed to send event {}: {}\nResponse body: {}", event.event, res.status(), body);
+ bail!("Failed to send event {}: {}\nResponse body: {}", event.event, status, body);
}
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
let body = res.text().await.unwrap_or_else(|_| "Unable to read response body".to_string()); | |
bail!("Failed to send event {}: {}\nResponse body: {}", event.event, res.status(), body); | |
if !res.status().is_success() { | |
let status = res.status(); | |
let body = res.text().await.unwrap_or_else(|_| "Unable to read response body".to_string()); | |
bail!("Failed to send event {}: {}\nResponse body: {}", event.event, status, body); | |
} |
Tools
GitHub Check: clippy
[failure] 39-39: borrow of moved value:
res
error[E0382]: borrow of moved value:res
--> crates/cli/src/posthog.rs:39:82
|
30 | let res = self
| --- move occurs becauseres
has typereqwest::Response
, which does not implement theCopy
trait
...
38 | let body = res.text().await.unwrap_or_else(|_| "Unable to read response body".to_string());
| ------res
moved due to this method call
39 | bail!("Failed to send event {}: {}\nResponse body: {}", event.event, res.status(), body);
| ^^^ value borrowed here after move
|
note:reqwest::Response::text
takes ownership of the receiverself
, which movesres
--> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/reqwest-0.11.24/src/async_impl/response.rs:147:23
|
147 | pub async fn text(self) -> crate::Result {
| ^^^^
Cut costs / complexity by skipping Segment
Summary by CodeRabbit
New Features
Bug Fixes
Documentation