全部產品
Search
文件中心

Application Real-Time Monitoring Service:配置 Sentry SDK 資料寫入

更新時間:Feb 12, 2026

本文介紹如何將 Sentry SDK 採集的應用資料寫入阿里雲Log Service(SLS),以便在使用者體驗監控控制台查看和分析。

前提條件

本方案僅支援 Sentry Envelope 協議格式。如您使用的 SDK 版本較低(如 JavaScript SDK v6 及以下),請先升級至支援 Envelope 的版本。

步驟一:建立 RUM 應用

  1. 登入CloudMonitor2.0控制台,選擇目標工作空間,在左側導覽列單擊接入中心

  2. 在左側導覽列,單擊接入中心

  3. 使用者體驗監控地區,根據應用類型選擇對應的平台卡片(如 iOS、Android、Web 等)。

  4. 輸入應用程式名稱,根據需要配置其他參數,單擊建立應用

  5. 建立成功後,進入應用設定 > 基礎資訊頁面,擷取以下資訊:

    參數

    說明

    擷取位置

    Workspace

    工作空間名稱

    應用設定 > 基礎資訊

    Endpoint

    資料上報網域名稱

    應用設定 > 基礎資訊 > 接入配置

    ServiceId

    RUM 應用 ID

    應用設定 > 基礎資訊

步驟二:在 Sentry 建立專案(可選)

如需同時使用 Sentry 控制台查看資料,請執行此步驟以擷取 Sentry 專案 ID。

  1. 登入 Sentry 控制台。

  2. 建立新專案(Project),平台選擇與用戶端應用一致。

  3. 進入專案設定頁面,找到專案的 DSN 配置。

  4. 從 DSN 中提取專案 ID(URL 路徑的最後一段數字)。

例如,DSN 為 https://xxx@sentry.example.com/21\,則專案 ID 為 21

說明 如不需要使用 Sentry 控制台,可跳過此步驟,後續配置中的專案 ID 填寫 0

步驟三:配置資料寫入

根據使用情境,選擇以下任一方式配置資料寫入。新使用者或已完成遷移驗證的使用者,建議使用 SDK 直寫方式。從自建 Sentry 遷移的使用者,建議先使用 Nginx 雙寫方式進行過渡,驗證資料完整性後再切換至 SDK 直寫。

方式一:SDK 直寫

修改用戶端應用中 Sentry SDK 的 DSN 配置,將資料上報地址指向阿里雲 SLS。

DSN 格式

原始 Sentry DSN 格式:

https://<public_key>@<sentry_host>/<project_id>

修改後的 阿里雲 DSN 格式:

https://<public_key>@<endpoint>/rum/sentry/<workspace>/<service_id>/<project_id>

參數說明

參數

說明

public_key

Sentry DSN 中的公開金鑰,SLS 不校正此參數,可使用任意值

endpoint

資料上報網域名稱,在步驟一中擷取

workspace

工作空間名稱,在步驟一中擷取

service_id

RUM 應用 ID,在步驟一中擷取

project_id

Sentry 專案 ID,在步驟二中擷取。如不使用 Sentry 控制台,填寫 0

配置樣本

以下為各平台 SDK 的配置樣本。

iOS(Swift)

import Sentry

SentrySDK.start { options in
    options.dsn = "https://key@cn-chengdu.log.aliyuncs.com/rum/sentry/my-workspace/abc123@example/21"
    options.debug = true
}

Android(Kotlin)

import io.sentry.android.core.SentryAndroid

SentryAndroid.init(this) { options ->
    options.dsn = "https://key@cn-chengdu.log.aliyuncs.com/rum/sentry/my-workspace/abc123@example/21"
    options.isDebug = true
}

Web(JavaScript)

import * as Sentry from "@sentry/browser";

Sentry.init({
  dsn: "https://key@cn-chengdu.log.aliyuncs.com/rum/sentry/my-workspace/abc123@example/21",
  debug: true,
});

Electron

import * as Sentry from "@sentry/electron";

Sentry.init({
  dsn: "http://key@cn-chengdu.log.aliyuncs.com/rum/sentry/my-workspace/abc123@example/21",
});

Vue

import * as Sentry from "@sentry/vue";

Sentry.init({
  app,
  dsn: "http://key@cn-chengdu.log.aliyuncs.com/rum/sentry/my-workspace/abc123@example/21",
});

方式二:Nginx 鏡像雙寫

通過 Nginx mirror 模組將資料同時寫入原 Sentry 服務和阿里雲 SLS。

適用情境:需同時在標準版 Sentry、RUM 版 Sentry 和阿里雲 RUM 控制台查看資料。

配置步驟

  1. 在 Nginx 設定檔中添加 SLS 轉寄的 upstream:

    upstream rum_forwarder {
        server <endpoint>:80;
        keepalive 2;
    }
  2. 添加 URI 映射規則,根據 Sentry 專案 ID 配置對應的轉寄路徑:

    map $request_uri $forwarder_path {
        # 預設轉寄路徑
        default /rum/sentry/<workspace>/<service_id>;
        
        # 按專案 ID 配置不同的轉寄路徑
        "~^/api/<project_id>/envelope/"  /rum/sentry/<workspace>/<service_id_for_project_1>;
        "~^/api/1/envelope/"  /rum/sentry/<workspace>/<service_id_for_project_1>;
        "~^/api/2/envelope/"  /rum/sentry/<workspace>/<service_id_for_project_2>;
    }
  3. 在 Sentry API 路由中啟用流量鏡像:

       location ~ ^/api/[1-9]\d*/ {
           proxy_pass http://relay;
           mirror /rum_mirror;
           mirror_request_body on;
       }
    
       location = /rum_mirror {
           internal;
           
           proxy_pass http://rum_forwarder$forwarder_path$request_uri;
           
           proxy_set_header Host <endpoint>;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header X-Request-Id $request_id;
           proxy_set_header Connection '';
    
           proxy_read_timeout 5s;
           proxy_send_timeout 5s;
           proxy_connect_timeout 5s;
    
           proxy_ignore_client_abort on;
       }
  4. 重新載入 Nginx 配置:

    nginx -t && nginx -s reload

多專案配置樣本

如有多個 Sentry 專案需要轉寄至不同的 RUM 應用,按以下方式配置 map 規則:

map $request_uri $forwarder_path {
    default /rum/sentry/my-workspace/default-service;
    
    # 專案 1 → RUM 應用 A
    "~^/api/1/envelope/"  /rum/sentry/my-workspace/service-a;
    
    # 專案 2 → RUM 應用 B
    "~^/api/2/envelope/"  /rum/sentry/my-workspace/service-b;
    
    # 專案 3 → RUM 應用 C
    "~^/api/3/envelope/"  /rum/sentry/my-workspace/service-c;
}

完整配置樣本

http {
    upstream rum_forwarder {
        server <endpoint>:80;
        keepalive 2;
    }

    map $request_uri $forwarder_path {
        # 預設轉寄路徑
        default /rum/sentry/<workspace>/<service_id>;
       
        # 按專案 ID 配置不同的轉寄路徑
        "~^/api/1/envelope/"  /rum/sentry/<workspace>/<service_id_for_project_1>;
        "~^/api/2/envelope/"  /rum/sentry/<workspace>/<service_id_for_project_2>;
   }

    server {
        listen 80;
        server_name sentry.example.com;

        # Sentry API 路由 - 啟用鏡像
        location ~ ^/api/[1-9]\d*/ {
            proxy_pass http://relay;
            mirror /rum_mirror;
            mirror_request_body on;
        }

        # SLS 鏡像處理
        location = /rum_mirror {
            internal;
       
            proxy_pass http://rum_forwarder$forwarder_path$request_uri;
       
            proxy_set_header Host <endpoint>;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Request-Id $request_id;
            proxy_set_header Connection '';

            proxy_read_timeout 5s;
            proxy_send_timeout 5s;
            proxy_connect_timeout 5s;

            proxy_ignore_client_abort on;
        }

        # 其他 Sentry 路由配置
        # ...
    }
}

參數說明

參數

說明

endpoint

SLS 資料上報網域名稱,如 cn-chengdu.log.aliyuncs.com

workspace

工作空間名稱

service_id

RUM 應用 ID

project_id

Sentry 專案 ID。

驗證資料寫入

完成配置後,通過以下方式驗證資料是否正常寫入:

  1. 在用戶端應用中觸發一個測試錯誤或事件。

  2. 登入CloudMonitor2.0控制台,進入使用者體驗監控 > 應用列表

  3. 選擇對應的 RUM 應用,查看是否有新資料上報。資料上報後,通常在 1-2 分鐘內可在控制台查看。

常見問題

Q:DSN 中的 public_key 是否可以隨意填寫?

A:是的,SLS 不校正 Sentry DSN 中的 public_key 參數。建議保留原有的 key 值以便追溯。

Q:資料寫入 SLS 後,是否還能在 Sentry 控制台查看?

A:可以。您需要部署定製版的 Sentry Web 和 Snuba API 容器,從 SLS 讀取資料。具體操作請參見配置 Sentry 控制台讀取使用者體驗監控資料。