tracing
注意
实验模块k6/experimental/tracing
已弃用,其功能已完全作为jslib提供。请参阅其 文档。将未来移除k6/experimental/tracing
。
使用此实验模块,您可以在测试运行期间对HTTP请求进行 仪表化,以便它们发出跟踪。
关于跟踪上下文
追踪上下文是添加到请求中的一组标准化HTTP头信息,它让追踪系统能够关联请求在系统中的其他请求。追踪上下文规范,例如支持的W3C 追踪上下文和Jaeger 追踪上下文,定义了特定的头名称和头值的编码格式。
追踪上下文通常至少包括一个trace_id
(追踪ID),一个span_id
(跨度ID)和一个sampled
标志。其中trace_id
是追踪的唯一标识符,span_id
是请求的唯一标识符,而sampled
标志是一个布尔值,表示是否应该追踪请求。例如,W3C 追踪上下文定义了Traceparent
头部,其值包含一个trace_id
、一个span_id
和sampled
标志,以十六进制值的连字符('-')分隔列表的形式编码。当追踪上下文头部附加到HTTP请求时,我们称其为已传播到下游服务。
API
类/函数 | 描述 |
---|---|
instrumentHTTP | 为k6 HTTP模块添加了追踪功能。 |
Client | 可配置客户端,公开了已追踪的HTTP操作,并允许选择性地使用追踪信息追踪请求。 |
示例
此示例演示了如何使用追踪API为脚本中生成的每个HTTP请求添加追踪信息。
import { check } from 'k6';
import tracing from 'k6/experimental/tracing';
import http from 'k6/http';
// instrumentHTTP will ensure that all requests made by the http module
// from this point forward will have a trace context attached.
//
// The first argument is a configuration object that
// can be used to configure the tracer.
tracing.instrumentHTTP({
// possible values: "w3c", "jaeger"
propagator: 'w3c',
});
export default () => {
// the instrumentHTTP call in the init context replaced
// the http module with a version that will automatically
// attach a trace context to every request.
//
// Because the instrumentHTTP call was configured with the
// w3c trace propagator, this request will as a result have
// a `traceparent` header attached.
const res = http.get('http://httpbin.org/get', {
headers: {
'X-Example-Header': 'instrumented/get',
},
});
};