使用 Sentry 即時監控應用程式錯誤和效能,支援多種程式語言和框架
專案簡介
Sentry 是一個開源的應用程式監控平台,用於即時追蹤錯誤和效能問題。支援 100+ 語言和框架,幫助開發者快速定位和修復問題。
GitHub Stars: 43K+
主要功能
- 錯誤追蹤 - 即時捕獲和聚合錯誤
- 效能監控 - 追蹤交易和請求
- 版本追蹤 - 關聯錯誤和發布版本
- 警報 - 自訂通知規則
- 整合 - Slack、GitHub、Jira 等
安裝
雲端服務
直接使用 sentry.io
自建部署
1
2
3
4
| # 使用官方腳本
git clone https://github.com/getsentry/self-hosted.git
cd self-hosted
./install.sh
|
Docker Compose
Python SDK
安裝
基本設定
1
2
3
4
5
6
7
| import sentry_sdk
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
traces_sample_rate=1.0,
environment="production",
)
|
捕獲錯誤
1
2
3
4
5
6
| import sentry_sdk
try:
risky_operation()
except Exception as e:
sentry_sdk.capture_exception(e)
|
自訂訊息
1
| sentry_sdk.capture_message("Something went wrong", level="warning")
|
使用者上下文
1
2
3
4
5
| sentry_sdk.set_user({
"id": "12345",
"email": "user@example.com",
"username": "john_doe"
})
|
JavaScript SDK
安裝
1
| npm install @sentry/browser
|
設定
1
2
3
4
5
6
| import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
tracesSampleRate: 1.0,
});
|
React 整合
1
| npm install @sentry/react
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "...",
integrations: [
Sentry.browserTracingIntegration(),
Sentry.replayIntegration(),
],
});
// Error Boundary
function App() {
return (
<Sentry.ErrorBoundary fallback={<ErrorFallback />}>
<MyApp />
</Sentry.ErrorBoundary>
);
}
|
Node.js SDK
安裝
1
| npm install @sentry/node
|
Express 整合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| const Sentry = require("@sentry/node");
const express = require("express");
const app = express();
Sentry.init({
dsn: "...",
tracesSampleRate: 1.0,
});
// 請求處理器
app.use(Sentry.Handlers.requestHandler());
// 路由
app.get("/", (req, res) => {
res.send("Hello!");
});
// 錯誤處理器
app.use(Sentry.Handlers.errorHandler());
app.listen(3000);
|
效能監控
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
| import sentry_sdk
sentry_sdk.init(
dsn="...",
traces_sample_rate=1.0,
)
# 手動 transaction
with sentry_sdk.start_transaction(name="process_data") as transaction:
with transaction.start_child(op="task", description="load_data"):
load_data()
with transaction.start_child(op="task", description="process"):
process()
|
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
| const transaction = Sentry.startTransaction({
name: "API Request",
op: "http.request",
});
const span = transaction.startChild({
op: "db.query",
description: "SELECT * FROM users",
});
// 執行操作
span.finish();
transaction.finish();
|
上下文和標籤
設定標籤
1
2
| sentry_sdk.set_tag("component", "auth")
sentry_sdk.set_tag("environment", "staging")
|
設定上下文
1
2
3
4
5
| sentry_sdk.set_context("request", {
"url": "/api/users",
"method": "POST",
"data": sanitized_data
})
|
麵包屑
1
2
3
4
5
| sentry_sdk.add_breadcrumb(
category="auth",
message="User logged in",
level="info",
)
|
版本追蹤
設定版本
1
2
3
4
| sentry_sdk.init(
dsn="...",
release="myapp@1.0.0",
)
|
整合 CI/CD
1
2
3
4
5
6
7
8
9
10
11
| # 建立 Release
sentry-cli releases new myapp@1.0.0
# 上傳 Source Maps
sentry-cli releases files myapp@1.0.0 upload-sourcemaps ./dist
# 完成 Release
sentry-cli releases finalize myapp@1.0.0
# 部署
sentry-cli releases deploys myapp@1.0.0 new -e production
|
警報設定
問題警報
1
2
3
4
5
6
7
8
| # 透過 UI 或 API 設定
conditions:
- event_frequency > 100
- first_seen_event
actions:
- notify_slack: "#alerts"
- notify_email: "team@example.com"
|
效能警報
1
2
3
4
5
6
| conditions:
- transaction_duration > 2000ms
- throughput < 100/min
actions:
- notify_pagerduty
|
整合
GitHub
- 自動連結 Issue 和 Commit
- 建立 GitHub Issue
- Suspect Commits
Slack
Jira
自建部署設定
環境變數
1
2
3
4
5
| # .env
SENTRY_SECRET_KEY=your-secret-key
SENTRY_POSTGRES_HOST=postgres
SENTRY_REDIS_HOST=redis
SENTRY_EMAIL_HOST=smtp.example.com
|
效能調優
1
2
3
4
5
6
7
8
9
10
| # docker-compose.yml
services:
web:
environment:
- SENTRY_WEB_WORKERS=4
- SENTRY_WEB_CONCURRENCY=2
worker:
environment:
- SENTRY_WORKER_CONCURRENCY=8
|
相關連結
延伸閱讀