Dynatrace

了解如何将 Dynatrace 后端日志与 OpenReplay 会话回放集成。

Dynatrace

了解如何将 Dynatrace 后端日志与 OpenReplay 会话回放集成,以扩展你的监控和调试能力。

此集成适用于 Cloud(Serverless、Dedicated)和 Self-Host(Community、Enterprise) 用户。

1. 生成你的 Dynatrace 凭据

Section titled 1. 生成你的 Dynatrace 凭据

按照以下步骤从 Dynatrace 获取所需的凭据:

  1. 登录你的 Dynatrace 账户。
  2. 导航到 Account Management
  3. 创建一个 OAuth Client:
  • 点击 Identity & Access Management
  • 选择 OAuth clients
  • 点击 Create client 以生成所需的凭据。
  1. 保存你的凭据:
  • 创建客户端后,保存以下内容:
    • Client ID
    • Client Secret
    • Account URN
  1. 获取你的 environment ID
    // Example
    Dynatrace URL: https://rzr85764.app.dynatrace.com/
    Environment ID: rzr85764

2. 将 openReplaySession.id 传播到后端

Section titled 2. 将 openReplaySession.id 传播到后端

要将 Dynatrace 日志与 OpenReplay 会话关联,必须在你想要跟踪的每个请求中,将唯一的会话 ID openReplaySession.id 从前端传播到后端。

在前端 API 请求中包含 openReplaySession.id

Section titled 在前端 API 请求中包含 openReplaySession.id

修改你的前端代码,将 openReplaySession.id 添加到 API 请求的请求头中:


// JavaScript Example for for Single Page Applications (SPA):

// Import OpenReplay
import OpenReplay from '@openreplay/tracker';

// Initialize the tracker
const tracker = new OpenReplay({
  projectKey: 'YOUR_PROJECT_KEY',
});

// Start the tracker and wait for it to resolve
tracker.start().then(() => {
  // Get the session ID after the tracker has started
  const sessionId = tracker.getSessionID();

  const headers = {
    'Content-Type': 'application/json',
  };

// Get the session ID
const sessionId = tracker.getSessionID();

const headers = {
  'Content-Type': 'application/json',
};

if (sessionId) {
  headers['openReplaySession.id'] = sessionId;
}

// Make the API request
fetch('/api/endpoint', {
  method: 'GET', // or 'POST', etc.
  headers,
  // ...other options
})
  .then(response => {
    // Handle response
  })
  .catch(error => {
    // Handle error
  });

在后端日志中包含 openReplaySession.id

Section titled 在后端日志中包含 openReplaySession.id

在你的后端应用中,从传入请求中提取 openReplaySession.id,并使用特定的键值格式 openReplaySession.id=<sessionId> 将其包含在日志中:


//  Example.....
// Middleware to extract the session ID
app.use((req, res, next) => {
  const sessionId = req.headers['openreplaysession.id'];
  req.sessionId = sessionId; // Attach to request object
  next();
});

// When logging events or errors
app.get('/api/endpoint', (req, res) => {
  // Your logic here

  // Log with the session ID in the required format
  console.log(`openReplaySession.id=${req.sessionId} - Endpoint accessed`);

  res.send('Success');
});

// Error handling middleware
app.use((err, req, res, next) => {
  console.error(`openReplaySession.id=${req.sessionId} - Error: ${err.message}`);
  res.status(500).send('Internal Server Error');
});

通过在后端日志中包含 openReplaySession.id,Dynatrace 可以收集与特定 OpenReplay 会话相关联的日志。这使你能够在 OpenReplay 的会话回放期间查看这些日志,从而全面了解用户交互和后端日志。

3. 在 Dynatrace 中创建自定义日志属性

Section titled 3. 在 Dynatrace 中创建自定义日志属性

在 Dynatrace 中创建一个自定义日志属性,以便从日志中提取 openReplaySession.id

  1. 访问你的 Dynatrace SaaS 账户。
  2. 进入 Settings。
  3. 选择 Log Monitoring > Custom attributes,然后选择 Add custom attribute
  4. 输入键 openReplaySession.id

参考:详细步骤请参阅 Dynatrace 文档:Log custom attributes

4. 在 OpenReplay 中配置 Dynatrace 集成

Section titled 4. 在 OpenReplay 中配置 Dynatrace 集成

在你的 OpenReplay 账户中,按照以下 3 个步骤完成会话回放与 Dynatrace 后端日志的关联。

  1. 在 OpenReplay 中进入 Preferences > Integrations
  2. 选择 Backend Logging 标签页。
  3. 选择你想要启用 Dynatrace 集成的项目:找到 Dynatrace 集成卡片 > 点击它。

在 Dynatrace 集成侧边栏中输入:

  1. Environment ID:你的 Dynatrace 环境 ID,用于外部访问。
  2. Client IDClient Secret:在第一步中保存的内容。
  3. Dynatrace Account URN:你在 Dynatrace 平台内使用的唯一标识符,同样在第一步中保存。
  1. 点击 Add 以测试连接:成功消息将确认集成已正确设置。

OpenReplay 中的 Dynatrace 集成

5. 在 OpenReplay 中查看 Dynatrace 日志

Section titled 5. 在 OpenReplay 中查看 Dynatrace 日志
  1. 在 OpenReplay 中导航到 Sessions。
  2. 打开任意会话回放。
  3. 点击 Traces 按钮。
  4. 将显示与该会话相关的 Dynatrace 日志。
  5. 点击任意日志条目以查看详细信息。

OpenReplay 中的 Dynatrace 追踪

类型为 LOG 且状态为 WARNERRORINFONONE 的日志通过 Dynatrace Log Monitoring API v2 检索。

如果你遇到任何问题,请加入我们的 Slack 或访问我们的论坛,从我们的社区获得帮助。