速成 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] |