菜单
开源

instrumentHTTP

注意

实验模块 k6/experimental/tracing 已弃用,其功能完全可用为 jslib。请参考其文档k6/experimental/tracing 将在将来被移除。

instrumentHTTP 函数可以对 k6 http 模块进行跟踪功能。它透明地替换 k6 http 模块中的每个函数,以自动为每个请求附加跟踪上下文。被观测的函数包括 delgetheadoptionspatchpostput、以及 request

instrumentHTTP 自动将跟踪信息添加到使用 k6/http 模块函数(如上所述)执行的 HTTP 请求中。这意味着,要观测 HTTP 请求,您无需重写代码。相反,您只需在 init 上下文中调用一次。从那时起,该 http 模块所发出的所有请求都将添加跟踪上下文头部,并且数据点输出的元数据将包含使用的 trace_id。有关传播的详细信息,请参阅关于跟踪上下文

参数

名称类型描述
options选项使用提供的 Options 对象配置跟踪行为。

示例

此示例演示了在脚本 init 上下文中调用 instrumentHTTP 函数一次如何确保从那时起所有 HTTP 模块发出的请求都将附加跟踪上下文头部。

JavaScript
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
// will be traced. The first argument is a configuration object that
// can be used to configure the tracer.
//
// Currently supported HTTP methods are: get, post, put, patch, head,
// del, options, and request.
tracing.instrumentHTTP({
  // propagator defines the trace context propagation format.
  // Currently supported: w3c and jaeger.
  // Default: w3c
  propagator: 'w3c',
});

export default () => {
  const res = http.get('http://httpbin.org/get', {
    headers: {
      'X-Example-Header': 'instrumented/get',
    },
  });
};