@defra/hapi-tracing

中文文档
1.30.2发布于 2026/11/04OGL-UK-3.0ES Module零依赖构建来源已验证近 30 天下载 6.7万

A hapi plugin for propagating trace id headers

原始使用文档
文档目录8 节
  1. What does this do
  2. What this does not do
  3. Installing
  4. Usage
  5. Register the plugin
  6. Common Use-Cases
  7. Propagating headers.
  8. Logging the header

Hapi Tracing

Hapi Tracing is a Hapi plugin to make selected headers available during the lifecycle of the request without having to manually pass them or the request object into every call. The main use-case for this is for propagating tracing headers, allowing them to be logged as well as forwarded on in other calls.

What does this do

Hapi Tracing is registered as a plugin. A specific tracing header should be supplied via the plugin's options. Once registered if an HTTP request is received with a specific tracking header the value of the header is extracted, stored in AsyncLocalStorage and made available throughout the duration of the request. A helper function getTraceId() provides access to the value of the tracking header without having to pass the request object across multiple calls.

What this does not do

  • Automatically forwards the trace ID to other HTTP calls
  • Automatically logs the trace ID

Built in support for this may be added in the future.

Installing

npm install @defra/hapi-tracing

Usage

The library consists of two parts:

  1. The Hapi Plugin
  2. The getTraceId() and withTraceId helpers

Register the plugin

As part of creating the server, register the hapi-tracing plugin.

import { tracing } from '@defra/hapi-tracing'

await server.register({
  plugin: tracing.plugin,
  options: { tracingHeader: 'x-your-trace-id' }
})

Once registered you will be able to call getTraceId() inside your controllers to return the value of the header if it has been passed in.

For example:

import { getTraceId } from '@defra/hapi-tracing'

const healthController = {
  handler: (request, h) => {
    return h.response({ message: `trace-id is ${getTraceId()}` }).code(200)
  }
}

The calls to getTraceId() don't have to originate from the controller. Any function called from the controller, directly or indirectly can use it.

import { getTraceId } from '@defra/hapi-tracing'

const healthController = {
  handler: (request, h) => {
    doSomething()
    return h.response().code(200)
  }
}

const doSomething = () => {
  doSomethingElse()
}

const doSomethingElse = () => {
  console.log(`trace-id is still ${getTraceId()}`)
}

Common Use-Cases

Propagating headers.

In order to track a request across multiple services you will need to forward the trace-id header on to other calls made as part of the request.

For this example we will be using node:fetch. Different HTTP clients may have different API's for settings headers but the concept should be the same.

The hapi-tracing package include a withTraceId helper that takes the name of the tracing header and an object containing the existing headers. If the traceId has been set it returns the original header object with the traceId header and value added. If the traceId is not set then the original header object is return unmodified.

import { withTraceId } from '@defra/hapi-tracing'

async function getSomething() {
  return await fetch('http://localhost:8080/test', {
    method: 'get',
    headers: withTraceId('x-your-trace-id', {
      'Content-Type': 'application/json',
      'x-some-other-header': '1234'
    })
  })
}

Logging the header

Some loggers will automatically include the original HTTP request which will include tracing header as well as any other headers that are not redacted. For this example we will use Pino with the ECS formatter as our logger.

export const loggerOptions = {
  enabled: logConfig.enabled,
  level: 'info', // other setup
  nesting: true,
  mixin: () => {
    const mixinValues = {}
    const traceId = getTraceId()

    if (traceId) {
      mixinValues.trace = { id: traceId }
    }
    return mixinValues
  }
}

// elsewhere
const logger = pino(loggerOptions)

This will set the traceId to appear in the trace.id field in the structure logs.

版本与发布记录

@defra/hapi-tracing 共发布 31 个版本,首次发布于 2024/12/10;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.30.2latest2026/11/048 天前
1.30.12026/10/051 个月前
1.30.02025/11/2811 个月前
1.29.02025/11/071 年前
1.28.02025/09/021.2 年前
1.27.02025/08/261.2 年前

关于 @defra/hapi-tracing 的常见问题

@defra/hapi-tracing 是什么?

A hapi plugin for propagating trace id headers

如何安装 @defra/hapi-tracing?

在项目目录运行 npm install @defra/hapi-tracing,或使用 pnpm add / yarn add / bun add。

@defra/hapi-tracing 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/defra__hapi-tracing 是否存在。

@defra/hapi-tracing 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@defra/hapi-tracing 需要什么版本的 Node.js?

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

@defra/hapi-tracing 可以商用吗?

@defra/hapi-tracing 使用 OGL-UK-3.0 许可。声明的许可为 OGL-UK-3.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@defra/hapi-tracing 的下载量有多少?

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

@defra/hapi-tracing 还在维护吗?

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