Datadog
了解如何将 Datadog 的后端错误与 OpenReplay 的会话回放集成,以扩展你的监控和调试能力。
1. 生成 Datadog API Key 和 Application Key
Section titled 1. 生成 Datadog API Key 和 Application Key- 前往 Datadog > Integrations > APIs 并生成 API Key,或使用已有的密钥。
- 在同一页面上,点击 Application Keys 并生成一个新的 application key。
2. 在 OpenReplay 中配置 Datadog 集成
Section titled 2. 在 OpenReplay 中配置 Datadog 集成在你的 OpenReplay 账户中,按照以下 3 个步骤完成会话回放与 Datadog 后端错误的关联。
启用 Datadog 集成
Section titled 启用 Datadog 集成- 在 OpenReplay 中前往 Preferences > Integrations。
- 选择 Backend Logging 选项卡。
- 选择你想要启用 Datadog 集成的项目:找到 Datadog 集成卡片 > 点击它。
输入 Datadog 凭据
Section titled 输入 Datadog 凭据在 Datadog 集成的侧边栏中输入:
- Datadog API Key
- Application Key
- 点击 Add 以测试连接
3. 传递 openReplaySession.id
Section titled 3. 传递 openReplaySession.id要将 Datadog 事件与录制的用户会话关联起来,需要在你想要跟踪的每个请求中,将唯一的会话 ID openReplaySession.id 从前端传递到后端。这可以通过自定义 HTTP 头来实现。
前端:在 API 请求中包含 openReplaySession.id
Section titled 前端:在 API 请求中包含 openReplaySession.id// JavaScript Example for for Single Page Applications (SPA):
const sessionId = tracker.getSessionID();
const headers = {
'Content-Type': 'application/json',
};
if (sessionId) {
headers['openReplaySession.id'] = sessionId;
}
// Make the API request
fetch('https://www.your-backend.com/api/endpoint', {
method: 'GET', // or 'POST', 'PUT', etc.
headers,
// ...other options
})
.then(response => {
// Handle response
})
.catch(error => {
// Handle error
});
后端:在日志中包含 openReplaySession.id
Section titled 后端:在日志中包含 openReplaySession.id下面是一个使用 logging 模块的 Python 示例,可自动将 openReplaySession.id 包含到你的日志中。
import logging
from flask import Flask, request, g
app = Flask(__name__)
# Configure the root logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
# Create a custom logging filter
class OpenReplayFilter(logging.Filter):
def filter(self, record):
session_id = getattr(g, 'openreplay_session_id', None)
if session_id:
record.msg = f"[openReplaySession.id={session_id}] {record.msg}"
return True
# Add the filter to the logger
logger.addFilter(OpenReplayFilter())
@app.before_request
def before_request():
# Extract the session ID from headers and store it in the Flask `g` object
g.openreplay_session_id = request.headers.get('openReplaySession.id')
@app.route('/api/endpoint')
def api_endpoint():
# Your logic here
# Log an event with the session ID automatically included
logger.info("Endpoint accessed")
return 'Success', 200
@app.errorhandler(Exception)
def handle_exception(e):
# Log the error with the session ID automatically included
logger.error(f"Error: {str(e)}")
return 'Internal Server Error', 500
if __name__ == '__main__':
app.run()