diff --git a/contents/docs/error-tracking/installation/rust.mdx b/contents/docs/error-tracking/installation/rust.mdx new file mode 100644 index 000000000000..aacc3fd54632 --- /dev/null +++ b/contents/docs/error-tracking/installation/rust.mdx @@ -0,0 +1,127 @@ +--- +title: Rust error tracking installation +platformLogo: rust +showStepsToc: true +--- + +import { Steps, Step } from 'components/Docs/Steps' +import { CalloutBox } from 'components/Docs/CalloutBox' + + + + + +Install the [PostHog Rust SDK](/docs/libraries/rust): + +```bash +cargo add posthog-rs +``` + +Error tracking ships enabled by default through the `error-tracking` feature. If you build with `default-features = false`, add it back explicitly: + +```toml +[dependencies] +posthog-rs = { version = "*", default-features = false, features = ["error-tracking"] } +``` + + + +The Rust SDK resolves stack traces in-process, so captured frames include file names, line numbers, and function names without any symbol uploads. Source context (displaying the surrounding lines of code in the error tracking UI) is not yet supported. + + + + + + + +```rust +let client = posthog_rs::client(( + "", + "", +)).await; +``` + +The default client is async (Tokio). Building with `default-features = false` gives you a blocking client instead — the same methods without `.await`. + + + + + +`capture_exception` works with any `std::error::Error` and captures it personlessly — the exception type, message, and full `source()` chain are sent, with a stack trace recorded at the call site: + +```rust +let error = std::io::Error::new(std::io::ErrorKind::Other, "connection refused"); + +client.capture_exception(&error).await.unwrap(); +``` + +To associate the exception with a person or attach context, use `capture_exception_with`: + +```rust +use posthog_rs::CaptureExceptionOptions; + +client.capture_exception_with( + &error, + CaptureExceptionOptions::new() + .distinct_id("user_distinct_id") + .property("route", "/checkout").unwrap() + .group("company", "company_id") + .fingerprint("my-custom-fingerprint") + .level("warning"), +).await.unwrap(); +``` + +All options are optional: `distinct_id` links a person, `property` and `group` add context, `fingerprint` overrides [issue grouping](/docs/error-tracking/grouping-issues), and `level` sets the severity (defaults to `error`). + +If you use `anyhow`, pass the underlying error with `err.as_ref()`: + +```rust +let result: anyhow::Result<()> = do_work(); +if let Err(err) = result { + client.capture_exception(err.as_ref()).await.unwrap(); +} +``` + + + + + +Stack trace capture and in-app frame classification are configured per client through `ErrorTrackingOptionsBuilder`: + +```rust +use posthog_rs::{ClientOptionsBuilder, ErrorTrackingOptionsBuilder}; + +let options = ClientOptionsBuilder::default() + .api_key("".to_string()) + .host("") + .error_tracking( + ErrorTrackingOptionsBuilder::default() + // Skip the stack walk entirely, e.g. for high-volume handled errors + .capture_stacktrace(false) + // Mark frames from a crate as library code rather than in-app + .in_app_exclude_paths(vec!["other_crate::".to_string()]) + .build() + .unwrap(), + ) + .build() + .unwrap(); + +let client = posthog_rs::client(options).await; +``` + +In-app patterns match both file paths and function symbols, so crate prefixes like `"my_crate::"` and path fragments like `"/service/"` both work. By default, frames from the cargo registry, the standard library, and vendored or target paths are classified as library code. + + + + + +Trigger a test exception to confirm events are being sent to PostHog. You should see it appear in the [error tracking issues view](https://app.posthog.com/error_tracking). + +```rust +let error = std::io::Error::new(std::io::ErrorKind::Other, "This is a test exception from Rust"); +client.capture_exception(&error).await.unwrap(); +``` + + + + diff --git a/contents/docs/libraries/rust/index.mdx b/contents/docs/libraries/rust/index.mdx index 14752d4205db..3b74a2d1b7aa 100644 --- a/contents/docs/libraries/rust/index.mdx +++ b/contents/docs/libraries/rust/index.mdx @@ -9,6 +9,7 @@ features: groupAnalytics: true surveys: false aiObservability: false + errorTracking: true --- ## Installation @@ -63,6 +64,29 @@ When local evaluation is enabled, flag definitions are fetched on initialization > **Note:** Local evaluation requires providing any person properties, groups, or group properties needed to evaluate the flag's release conditions, since PostHog can't fetch these automatically without a server request. +## Error tracking + +You can capture exceptions with the Rust SDK. `capture_exception` accepts any `std::error::Error` and records a stack trace at the call site; `capture_exception_with` additionally links a person and attaches context: + +```rust +use posthog_rs::CaptureExceptionOptions; + +let error = std::io::Error::new(std::io::ErrorKind::Other, "connection refused"); + +// Captured personlessly +client.capture_exception(&error).await.unwrap(); + +// Associated with a person, with optional context +client.capture_exception_with( + &error, + CaptureExceptionOptions::new() + .distinct_id("user_distinct_id") + .property("route", "/checkout").unwrap(), +).await.unwrap(); +``` + +For the full setup guide, see the [Rust error tracking installation docs](/docs/error-tracking/installation/rust). + ## Observability The Rust SDK uses [`tracing`](https://docs.rs/tracing) for structured logging. Add a tracing subscriber to your application to see SDK logs: diff --git a/src/navs/index.js b/src/navs/index.js index a9ed1dd83e3a..3388cb2bcb4f 100644 --- a/src/navs/index.js +++ b/src/navs/index.js @@ -4888,6 +4888,10 @@ export const docsMenu = { name: 'PHP', url: '/docs/error-tracking/installation/php', }, + { + name: 'Rust', + url: '/docs/error-tracking/installation/rust', + }, { name: 'iOS', url: '/docs/error-tracking/installation/ios',