@autotelic/fastify-opentelemetry

中文文档
0.23.0发布于 2025/11/10MITTypeScriptCommonJS近 30 天下载 5.7万

OpenTeleometry 的快取插件

中文使用文档
文档目录7 节
  1. 使用量
  2. 实例
  3. API 密码
  4. 配置
  5. 请求装饰器
  6. 钩子
  7. OpenTeleometry 兼容性

速成 OpenTelection

A 类 快捷化 使用该插件的插件 打开数位 API 提供请求追踪.

使用量

npm i @autotelic/fastify-opentelemetry

实例

插件使用

// index.js
// Load your OpenTelemetry/API configuration first. Now the configured SDK will be available
// to fastify-opentelemetry. (See the example configuration below.)
require('./openTelemetryConfig')
const openTelemetryPlugin = require('@autotelic/fastify-opentelemetry')
const fastify = require('fastify')();

(async () => {
  await fastify.register(openTelemetryPlugin, { wrapRoutes: true })

  fastify.get('/', async function (request, reply) {
    const {
      activeSpan,
      tracer,
      // context,
      // extract,
      // inject,
    } = request.openTelemetry()
    // Spans started in a wrapped route will automatically be children of the activeSpan.
    const childSpan = tracer.startSpan(`${activeSpan.name} - child process`)
    // doSomeWork()
    childSpan.end()
    return 'OK'
  })

  fastify.listen(3000, (err, address) => {
    if (err) {
      fastify.log.error(err)
      process.exit(1)
    }
  })
})()

打开远程 API 配置

// openTelemetryConfig.js
const {
  BatchSpanProcessor,
  ConsoleSpanExporter,
  TraceIdRatioBasedSampler
} = require('@opentelemetry/sdk-trace-base')

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node')

// Configure a tracer provider.
const provider = new NodeTracerProvider({
  sampler: new TraceIdRatioBasedSampler(0.5)
})

// Add a span exporter.
provider.addSpanProcessor(
  new BatchSpanProcessor(new ConsoleSpanExporter())
)

// Register a global tracer provider.
provider.register()

// Note: the above is just a basic example. fastify-opentelemetry is compatible with any
// @opentelemetry/api configuration.

见 实例 对于一个实例应用。 要在本地运行示例应用程序, 首先复制此 repo , 然后 npm i 运行 :

npm run dev

API 密码

配置

此插件将所有跟踪器配置留给 打开数位 API追踪和传播方法分别来自全球追踪提供者和全球传播者。 这使得插件本身的配置最小化.

插件接受以下配置属性 :

  • exposeApi 数字 : boolean - 用来防止插件装饰请求. 默认情况下,请求将被装饰(即默认为 true) (中文(简体) ).

  • formatSpanName 数字 : (FastifyRequest) => string - 跨度名称的自定义物质。 默认格式是 `${req.method} ${req.routeOptions.url}`。 。 。 .

  • formatSpanAttributes 数字 : object - 包含跨度属性的格式函数. 属性数字 :

    • request数字 : (FastifyRequest) => object - 根据请求,返回的对象将被添加到当前跨度的属性中。 默认请求属性为:

      { 'req.method': request.raw.method, 'req.url': request.raw.url }
      
    • reply数字 : (FastifyReply) => object - 答复时,返回对象将被添加到当前跨度的属性中。 默认回复属性为:

      { 'reply.statusCode': reply.statusCode }
      
    • error数字 : (Error) => object - 在出错时,返回的对象将被添加到当前跨度的属性中。 默认错误属性为:

      {
        'error.name': error.name,
        'error.message': error.message,
        'error.stack': error.stack
      }
      
  • wrapRoutes 数字 : boolean | string[] - 当 true,所有路由处理器将使用等于 request.openTelemetry().context。并接受包含所有应当被包裹的路由路径的阵列。 可选 - 默认已禁用 .

    • 路径必须有一个线索 / 不得随从 / 联合国. '/my/exact/route/path') (中文(简体) ).
    • 环绕一个路由允许在路由处理器范围内创建的任何跨度自动成为当前请求的子 activeSpan。这包括自动创建的跨度 开元仪表。 。 。 .
    • 全球背景管理器(例如:. AsyncHooksContextManager)需要为路由处理器提供主动上下文.
  • ignoreRoutes 数字 : string[] | (path: string, method: string) => boolean - 配置 钩子 由此插件添加以忽略某些路由路径(即该路由没有自动跟踪). 胜过 wrapRoutes。 。 。 .

    • 可以是一系列路由路径(遵循与 wrapRoutes) (中文(简体) ).

    • 可以是接收路由路径和方法并返回布尔值的函数( 返回) true 以忽略方式处理)。 例如, 禁用跟踪 OPTIONS 路径 :

      fastify.register(openTelemetryPlugin, {
        ignoreRoutes: (path, method) => method === 'OPTIONS'
      })
      
    • 被忽略的路线仍然可以进入 request.openTelemetry不过 activeSpan 将会是 undefined。 。 。 .

  • propagateToReply 数字 : boolean - 当 true中,当前间距上下文将使用您的已注册的传播器注入回复信头。 默认为 false。 。 。 .

  • spanOptions 数字 : SpanOptions | (FastifyRequest) => SpanOptions - 说吧 SpanOptions 返回函数 SpanOptions这些将被传递到 tracer.startSpan 每个请求。 默认为 { kind: 1 }。 。 。 .

请求装饰器

此插件以一个 openTelemetry 函数返回具有下列属性的对象:

  • context数字 : Context - 包含活动段段的上下文以及提取出的任何上下文.

  • activeSpan数字 : Span - 活动跨度(可通过以下途径获得): context我们只是提供一条捷径.

  • extract数字 : Propagation.extract - 包装传播API的取出方法,并作为当前请求的背景通过 context 参数。 这将返回一个新的上下文, 并将 没有 影响请求的跟踪上下文。 接受下列论点:

    • carrier数字 : object - 包含要取出的内容的对象.
    • getter数字 : TextMapGetter - 包含 get 财务报告和审定财务报表 keys 方法。 。 。 用于从载体中提取值. OpenTeleometry 的默认值 defaultTextMapGetter。 。 。 .
  • inject数字 : Propagation.inject - 包装传播API的注射方法,并在当前请求的上下文中通过 context 参数。 接受下列论点:

    • carrier数字 : object - 上下文将注入对象.
    • setter数字 : TextMapSetter - 包含 set 方法。 用来给载体注入值. OpenTeleometry 的默认值 defaultTextMapSetter。 。 。 .
  • tracer数字 : Tracer - 插件创建并使用的追踪器.

钩子

此插件注册以下快取钩:

  • onRequest: 开始跨度.

  • onReply:停止横跨.

  • onError: 添加错误信息以覆盖属性.

  • onRoute: 仅在 wrapRoutes 已启用.

  • onSend: 仅在 propagateToReply 已启用.

OpenTeleometry 兼容性

作为 打开数位 API 使用a 变量在 global 对象存储全局 API,需要注意确保所有模块兼容.

每个版本的OpenTeleometry API将要求专门发布自来自来自来自来自来自去自来自去自来自去自来自去自去自来自去自去自去自来自来自去自来自去自来自去自来自来自来自去自去自来自来自来自去自来自来自来自来自来自来自去自来自来自来自来自来自来自来自去自来自来自来自来自来自来自来自来自来自自自来自来自自来自来自来自来自来自来自来自自来自来自自来自自自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自来自.

OpenTeleometry API 版本 : 开源软件最小快取 OpenTelection 版本
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]
@opentelemetry/[email protected]@autotelic/[email protected]

版本与发布记录

@autotelic/fastify-opentelemetry 共发布 32 个版本,首次发布于 2020/07/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.23.0latest2025/11/1011 个月前
0.22.12025/01/291.8 年前
0.22.02024/09/212 年前
0.21.02024/04/102 年前
0.20.02024/03/062 年前
0.19.02024/02/072 年前

关于 @autotelic/fastify-opentelemetry 的常见问题

@autotelic/fastify-opentelemetry 是什么?

OpenTeleometry 的快取插件

如何安装 @autotelic/fastify-opentelemetry?

在项目目录运行 npm install @autotelic/fastify-opentelemetry,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@opentelemetry/api(^1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@autotelic/fastify-opentelemetry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@autotelic/fastify-opentelemetry 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@autotelic/fastify-opentelemetry 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@autotelic/fastify-opentelemetry 可以商用吗?

@autotelic/fastify-opentelemetry 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@autotelic/fastify-opentelemetry 的下载量有多少?

根据 npm 官方下载统计,@autotelic/fastify-opentelemetry 在 2026/10/07 – 2026/11/05 这 30 天内被下载 57,444 次,日均约 1,915 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@autotelic/fastify-opentelemetry 还在维护吗?

最新版本 0.23.0 发布于 2025/11/10(11 个月前);至今共发布 32 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/07/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。