使用 Winston structured event analytics 发送并验证事件
在受控应用程序边界使用 Winston structured event analytics,保持事件契约较小,并在构建聚合视图之前验证已知结果。
- 1
选择结果
Node.js 工作流程监控
- 2
定义合同
操作、request_id 或 job_id、状态和释放
- 3
在结果处发出
Delivery is best-effort with a two-second fetch timeout. Ingestion failure preserves the application result or original error; verify the event with a query and use a durable outbox when loss is unacceptable.
- 4
核实证据
Exercise a known fixture, then inspect job_completed for one correctly typed terminal row.
开始之前
开始之前
- Winston in a trusted Node.js runtime with fetch and AbortSignal.timeout and a server-side Telemetry key
- 稳定操作名称、相关 ID、错误类别和版本
- 针对 Winston 传输和未完成的 Telemetry 请求的关闭策略
交货设置
安装并初始化服务器端
此服务端示例通过 Log API 发送事件,使用服务端密钥和有时限的 fetch 请求;无需初始化 Telemetry SDK。 将摄取凭据保留在浏览器包、客户端可见的环境变量、源代码控制、日志和异常消息之外。
npm安装
npm install winston- 1准备一个具有有限网络行为的可重用服务器端交付客户端。
- 2在成功、失败、重试或超时边界处添加结果事件。
- 3在启用警报之前发送受控装置并检查存储的行。
片段
从一个结构化事件开始
在工作流程完成、失败或重试的位置添加此形状。然后从真实的字段构建仪表板。
Winston structured event analytics事件
import winston from "winston";
const logger = winston.createLogger({
format: winston.format.json(),
transports: [new winston.transports.Console()],
});
async function recordOutcome(data) {
try {
const receipt = await fetch("https://api.telemetry.sh/log", {
method: "POST",
headers: {
Authorization: process.env.TELEMETRY_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({ table: "job_completed", data }),
signal: AbortSignal.timeout(2_000),
});
if (!receipt.ok) console.warn("Telemetry ingestion failed", receipt.status);
} catch {
console.warn("Telemetry ingestion unavailable");
}
}
const startedAt = performance.now();
try {
const result = await processImport();
const outcome = {
operation: "customer_import",
job_id: jobId,
status: "success",
duration_ms: Math.round(performance.now() - startedAt),
item_count: result.itemCount,
release: process.env.APP_RELEASE,
};
logger.info("customer import completed", outcome);
await recordOutcome(outcome);
return result;
} catch (error) {
const outcome = {
operation: "customer_import",
job_id: jobId,
status: "failed",
duration_ms: Math.round(performance.now() - startedAt),
error_type: "import_error",
release: process.env.APP_RELEASE,
};
logger.error("customer import failed", { ...outcome, error });
await recordOutcome(outcome);
throw error;
}事件结构
操作、request_id 或 job_id、状态和释放
duration_ms、error_type、retry_count 和 item_count
服务、环境、功能和批准的帐户上下文
检查配置
检查站 1
Delivery is best-effort with a two-second fetch timeout. Ingestion failure preserves the application result or original error; verify the event with a query and use a durable outbox when loss is unacceptable.
检查站 2
使用 Winston 传输和诊断日志格式;仅发送定义的 Telemetry 问题所需的终端字段。
检查站 3
不要创建转发任意日志记录的 Winston 传输,除非每个消息和元数据字段都有经过审查的架构和编辑策略。
检查站 4
单独测试传送拒绝和关闭行为,因为完成一个传输不会刷新另一个网络客户端。
验证
证明事件已到达
在演练已知的成功和失败案例后运行此命令。如果您的最终事件契约与代码片段不同,请替换后备表名称。
Winston structured event analytics 验证查询
SELECT *
FROM job_completed
ORDER BY timestamp_utc DESC
LIMIT 20;实施参考
在启用新的生产路径之前,请检查事件合同、数据安全指南和上游主要文档。
在哪里记录事件
保持结果事件小且可恢复
该模式提供了
- 除了上游工作流程之外,还有一个有界的、SQL 就绪的结果。
- 用于仪表板、警报和跨事件关联的稳定字段。
- 用于验证成功、失败、重试和超时行为的夹具驱动路径。
该模式不提供
- OTLP 导出器、自动收集管道或详细跟踪和诊断日志的替代品。
- 仅因为有效负载包含事件 ID,所以仅传送一次。
- 收集原始提供商有效负载、用户内容、凭证或受监管数据的权限。
事件模式示例
此工作流的事件模式
在将查询或代码片段适应生产之前,请检查行粒度、发出边界、所需类型、隐私类、示例有效负载和验证清单。
相关产品功能
继续此工作流程 告警
将经过审查的可靠性查询提升到拥有的阈值和响应工作流程中。
相关 SQL 查询示例
更多 SQL 示例
针对此工作流程中的结构化字段运行查询,检查示例结果,并将有用的答案转换为仪表板或警报。
按实施系列浏览
比较相关集成模式
与此集成配对的模板
更多集成