instrumentHTTP
添加埋点的函数是透明的instrumentHTTP
函数为 k6 HTTP 模块添加额外的请求头。它透明地替换了 k6 http 模块的每个函数,使其自动为每个请求附加 baggage header(默认情况下)。
已添加埋点的函数包括 del、 get、 head、 options、 patch、 post、 put 和 request。
这意味着,要为 HTTP 请求添加埋点,您无需重写任何代码。只需在 init 上下文中调用一次该模块即可。有关传播的详细信息,请参阅 关于 baggage header。
参数
名称 | 类型 | 描述 |
---|---|---|
generateHeaders | function(method, body, params) headersMap | 一个函数,接受请求方法、请求体和参数,并返回一个将要附加的请求头映射。默认情况下,根据 [关于 baggage header](https://grafana.org.cn/docs/k6/ |
示例
本示例演示了如何使用此库为脚本中的每个 HTTP 请求添加 baggage header 埋点。
import { check } from 'k6';
import pyroscope from 'https://jslib.k6.io/http-instrumentation-pyroscope/1.0.2/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 baggage context attached.
pyroscope.instrumentHTTP();
export default () => {
// the instrumentHTTP call in the init context replaced
// the `http` module with a version that will automatically
// attach a baggage header to every request.
const res = http.get('http://httpbin.org/get', {
headers: {
'X-Example-Header': 'instrumented/get',
},
});
};