feat: 集成 Sentry 错误监控 + 前端错误上报中继
后端: - Cargo.toml 添加 sentry/sentry-actix/sentry-tracing (v0.48) - main.rs 条件初始化 Sentry(SENTRY_DSN 环境变量控制) - 新增 handlers/sentry.rs — POST /api/sentry/events 前端中继端点 - sentry-actix 全局中间件 + sentry-tracing 自动捕获 tracing::error! - .env.example 添加 SENTRY_DSN / SENTRY_TRACES_SAMPLE_RATE 配置 前端: - 新增 utils/sentryReporter.ts (批量上报/防抖/开发环境跳过) - app.ts 全局错误处理接入 Sentry (wx.onError/onUnhandledRejection/onPageNotFound) Sentry 未配置 DSN 时完全无操作,无性能开销
This commit is contained in:
@@ -5,6 +5,7 @@ pub mod favorites;
|
||||
pub mod health;
|
||||
pub mod meta;
|
||||
pub mod payment;
|
||||
pub mod sentry;
|
||||
pub mod static_files;
|
||||
pub mod user;
|
||||
pub mod weather;
|
||||
@@ -47,3 +48,4 @@ pub use payment::payment_index;
|
||||
pub use payment::payment_login_status;
|
||||
pub use payment::payment_page;
|
||||
pub use payment::payment_success;
|
||||
pub use sentry::report_frontend_error;
|
||||
|
||||
45
src/handlers/sentry.rs
Normal file
45
src/handlers/sentry.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use actix_web::{web, HttpResponse, post};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct SentryEventRequest {
|
||||
pub level: Option<String>,
|
||||
pub message: String,
|
||||
pub stack: Option<String>,
|
||||
pub page: Option<String>,
|
||||
pub user_id: Option<i32>,
|
||||
pub extra: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
#[post("/api/sentry/events")]
|
||||
pub async fn report_frontend_error(body: web::Json<SentryEventRequest>) -> HttpResponse {
|
||||
let body = body.into_inner();
|
||||
tracing::info!("前端错误上报: {}", body.message);
|
||||
|
||||
sentry::with_scope(
|
||||
|scope| {
|
||||
scope.set_tag("source", "wechat-miniprogram");
|
||||
if let Some(page) = &body.page {
|
||||
scope.set_tag("page", page);
|
||||
}
|
||||
if let Some(user_id) = body.user_id {
|
||||
scope.set_user(Some(sentry::User {
|
||||
id: Some(user_id.to_string()),
|
||||
..Default::default()
|
||||
}));
|
||||
}
|
||||
if let Some(extra) = &body.extra {
|
||||
if let Some(obj) = extra.as_object() {
|
||||
for (k, v) in obj {
|
||||
scope.set_extra(k, sentry::protocol::Value::from(v.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|| {
|
||||
sentry::capture_message(&body.message, sentry::Level::Error);
|
||||
},
|
||||
);
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({"success": true}))
|
||||
}
|
||||
Reference in New Issue
Block a user