菜单
开源

instrumentHTTP

instrumentHTTP 函数为 k6 的 http 模块提供了链路追踪能力。它透明地将 k6 http 模块的每个函数替换为会自动为每个请求附加链路追踪上下文的版本。被插桩的函数包括 delgetheadoptionspatchpostputrequest

instrumentHTTP 会自动将链路追踪信息添加到使用 k6/http 模块函数(上面提到的)执行的 HTTP 请求中。这意味着,要对 HTTP 请求进行插桩,您无需重写任何代码。相反,在 init 上下文中调用一次即可。从那时起,HTTP 模块发出的所有请求都会被添加一个链路追踪上下文头,并且数据点输出的元数据将包含使用的 trace_id。有关传播的详细信息,请参阅关于链路追踪上下文

参数

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

示例

本例展示了如何在脚本的 init 上下文中调用 instrumentHTTP 函数。从那时起,HTTP 模块发出的所有请求都会被附加链路追踪上下文头。

JavaScript
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
// 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.
tempo.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',
    },
  });
};