适用于 Tempo 的 HTTP 插装
注意
此库的源代码可在 grafana/jslib.k6.io GitHub 仓库中找到。
http-instrumentation-tempo
模块允许您对 HTTP 请求进行*插装*,以便在测试运行时它们会发出分布式追踪。使用它可以将追踪上下文包含在 HTTP 请求中,然后可以由分布式追踪后端使用,例如 Grafana Tempo。
从 k6/experimental/tracing
迁移
此 jslib 是一个直接替换,因此您只需将 'k6/experimental/tracing'
导入替换为 'https://jslib.k6.io/http-instrumentation-tempo/1.0.1/index.js'
即可迁移
关于追踪上下文
一个*追踪上下文*是一组添加到请求中的标准化 HTTP 头,它允许追踪系统在请求通过系统导航时将其与其他请求关联起来。追踪上下文规范,例如支持的 W3C Trace Context 和 Jaeger Trace Context,定义了特定的头名称和头值的编码格式。
一个追踪上下文通常至少包含一个 trace_id
、一个 span_id
和一个 sampled
标志。trace_id
是追踪的唯一标识符,span_id
是请求的唯一标识符,而 sampled
标志是一个布尔值,指示是否应对请求进行追踪。例如,W3C Trace Context 定义了 Traceparent
头,其值包含一个 trace_id
、一个 span_id
和一个 sampled
标志,编码为由短划线 (-
) 分隔的十六进制值列表。当追踪上下文头附加到 HTTP 请求时,我们称其被*传播*到下游服务。
API
类/函数 | 描述 |
---|---|
instrumentHTTP | 为 k6 http 模块注入追踪能力。 |
客户端 (Client) | 一个可配置的客户端,它暴露了被插装的 HTTP 操作,并允许选择性地为请求注入追踪。 |
示例
此示例演示了如何使用追踪 API 为脚本中进行的每个 HTTP 请求注入追踪信息。
import { check } from 'k6';
import tempo from 'https://jslib.k6.io/http-instrumentation-tempo/1.0.1/index.js';
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.
tempo.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',
},
});
};