跳到主要内容

将云应用日志路由到管道

本指南将向您展示如何在可观测性管道工作流中模拟、发送并验证单个 JSON 日志事件。

目标: 摄入模拟的应用日志,验证其是否已到达管道,并准备将其路由到目标(如 Splunk、Elastic 或 S3)。

为何重要​

在构建复杂的解析规则或将数据路由到昂贵的安全信息和事件管理(SIEM)工具之前,您必须验证数据摄取路径。 此工作流可确认:

  1. 您的 API 令牌有效。
  2. 管道接受您的 JSON 结构。
  3. 您能实时查看数据。

开始之前​

请确保您拥有:

  • API 令牌(具有 ingest 权限)。
  • 您的唯一流 ID(来自平台 UI)。
  • 终端访问权限(用于运行 curl)或 Postman。

管道架构截图

1. 创建或选择一个流​

在您的可观测性平台中:

  1. 转到 流 > 管理。
  2. 点击 新建流 或 select default-logs。
  3. 复制 流 ID,例如 st_12345。
  4. (可选)设置保留策略。默认通常为 7 天。

2. 创建示例日志事件​

通过将以下内容保存为 app-log.json 来模拟云应用日志。

app-log.json
{
"timestamp": "2025-11-15T08:30:00Z",
"service": "payment-gateway",
"level": "ERROR",
"message": "交易失败:网关超时",
"transaction_id": "txn_998877",
"meta": {
"region": "us-east-1",
"customer_id": "cus_554433"
}
}
备注

请将 timestamp 保持为国际标准化组织 (ISO) 8601 格式,以确保正确索引。

3. 发送日志事件​

使用 curl 模拟应用程序发送日志.

cURL
export STREAM_ID="your_stream_id_here"
export API_TOKEN="your_api_token_here"

curl -X POST "https://api.observability-platform.com/v1/streams/ingest" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-H "X-Stream-Id: $STREAM_ID" \
-d @app-log.json

预期响应​

Response
{
"status": "accepted",
"ingest_id": "evt_abc12345",
"accepted_items": 1
}

如果看到 status: accepted,说明管道已接收数据。

4. 在实时数据查看器中验证日志​

  1. 在平台界面中导航至 Explore 或 Live Tail。
  2. 选择您的流(st_12345)。
  3. 添加过滤条件:service:payment-gateway。
  4. 您的事件应立即显示:
Live tail output
[ERROR] 2025-11-15T08:30:00Z service=payment-gateway msg="Transaction failed..."

5. 将日志路由到目标​

现在数据已开始流动,请创建一条路由规则:

  1. 转到 Pipelines > Routing Rules。
  2. 点击 New Rule。
  3. Filter: level:ERROR
  4. Action: 路由到 S3-Archive 和 Slack-Alerts。
  5. 保存并部署。

Send the curl request again.

  • 检查您的 S3 存储桶。
  • 检查您的 Slack 频道。

6. 优化管道​

现在流程已正常运行,请考虑添加以下内容:

  • Remapping: remap meta.customer_id to a top-level attribute for easier filtering and faceting.
  • Parsing: extract error codes or transaction IDs from raw message strings using a Grok parser.
  • Enrichment: add a team_owner tag based on the service name.
  • Masking: obfuscate customer_id if it's personally identifiable information (PII).

故障排除​

错误代码可能原因 / 解决方法
401 未授权检查您的 API 令牌权限。
400 请求错误验证您的 JSON 语法。
404 未找到验证 X-Stream-Id 是否正确。
时间戳问题确保您使用的是协调世界时 (UTC) ISO 8601 格式。
数据未显示检查是否有正在运行的过滤器排除了您的新日志。

下一步​