mcpField
C
correlation-tracing

correlation-tracing

github.com/aj-geddes云与 DevOps373426 次安装34 周前更新
代码仓库

关于 correlation-tracing

用于在多服务系统中实现关联 ID 和分布式追踪的技能。它通过 Express、OpenTelemetry 和 Python 指南,帮助开发者追踪请求流、排查错误并了解性能。

主要功能

  1. 01关联 ID 生成与传递
  2. 02跨服务请求追踪
  3. 03Express 中间件指南
  4. 04OpenTelemetry 集成指南
  5. 05Python 追踪与手动传递

适用场景

  1. 01追踪微服务中的请求
  2. 02排查跨服务错误
  3. 03查看请求流转过程
  4. 04分析延迟与依赖关系

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill correlation-tracing

安装后在 Claude Code 中输入/correlation-tracing确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
correlation-id-middleware-express.md
manual-trace-propagation.md
opentelemetry-integration.md
python-distributed-tracing.md
scripts/
health-check.sh
templates/
dashboard-config.yaml

详细介绍

关联与分布式追踪

目录

概述

实现关联 ID 和分布式追踪,以跨多个服务跟踪请求并了解系统行为。

适用场景

  • 微服务架构
  • 调试分布式系统
  • 性能监控
  • 请求流可视化
  • 跨服务错误追踪
  • 依赖关系分析
  • 延迟优化

快速开始

最简可运行示例:

import express from "express";
import { v4 as uuidv4 } from "uuid";

// Async local storage for context
import { AsyncLocalStorage } from "async_hooks";

const traceContext = new AsyncLocalStorage<Map<string, any>>();

interface TraceContext {
  traceId: string;
  spanId: string;
  parentSpanId?: string;
  serviceName: string;
}

function correlationMiddleware(serviceName: string) {
  return (
    req: express.Request,
    res: express.Response,
    next: express.NextFunction,
  ) => {
    // Extract or generate trace ID
    const traceId = (req.headers["x-trace-id"] as string) || uuidv4();
    const parentSpanId = req.headers["x-span-id"] as string;
    const spanId = uuidv4();
// ... (see reference guides for full implementation)

参考指南

references/ 目录中的详细实现:

指南内容
Correlation ID Middleware (Express)Correlation ID Middleware (Express)
OpenTelemetry IntegrationOpenTelemetry Integration
Python Distributed TracingPython Distributed Tracing
Manual Trace PropagationManual Trace Propagation

最佳实践

✅ 应该
  • 在入口点生成追踪 ID
  • 在服务间传播追踪上下文
  • 在日志中包含关联 ID
  • 使用结构化日志
  • 设置适当的 span 属性
  • 在高流量系统中对追踪进行采样
  • 监控追踪收集的开销
  • 实现上下文传播
❌ 不应该
  • 跳过追踪传播
  • 在没有关联上下文的情况下记录日志
  • 创建过多 span
  • 在 span 中存储敏感数据
  • 因追踪上报而阻塞
  • 忘记错误追踪

评论

支持 Markdown